猿问

请问sun.awt.image.ToolkitImage@73486eef该怎么转换为图片?

img=Toolkit.getDefaultToolkit().createImage(xp.asBytes());
System.out.println("图片:"+img);
结果为
图片:sun.awt.image.ToolkitImage@73486eef
这样的该怎么转换为图片?
噜噜哒
浏览 499回答 2
2回答

Helenr

publicstaticBufferedImagetoBufferedImage(Imageimage){if(imageinstanceofBufferedImage){return(BufferedImage)image;}image=newImageIcon(image).getImage();BufferedImagebimage=null;GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();try{inttransparency=Transparency.OPAQUE;GraphicsDevicegs=ge.getDefaultScreenDevice();GraphicsConfigurationgc=gs.getDefaultConfiguration();bimage=gc.createCompatibleImage(image.getWidth(null),image.getHeight(null),transparency);}catch(HeadlessExceptione){}if(bimage==null){inttype=BufferedImage.TYPE_INT_RGB;bimage=newBufferedImage(image.getWidth(null),image.getHeight(null),type);}Graphicsg=bimage.createGraphics();g.drawImage(image,0,0,null);g.dispose();returnbimage;}Imageimage=Toolkit.getDefaultToolkit().createImage(byte[]bytes);BufferedImagebufferedImage=toBufferedImage(image);try{ImageIO.write(bufferedImage,"JPG",newFile("路径"));}catch(IOExceptione){e.printStackTrace();}

子衿沉夜

输出到控制台当然是只能显示为文本(调用对象的toString()方法并将其输出),如果需要显示Image,考虑使用Swing中JLabel这样的组件。img=Toolkit.getDefaultToolkit().createImage(xp.asBytes());JLabellblImg=newJLabel(newImageIcon(img));//...将JLabel加入到JFrame,并将JFrame显示的代码
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答