img=Toolkit.getDefaultToolkit().createImage(xp.asBytes());System.out.println("图片:"+img);结果为图片:sun.awt.image.ToolkitImage@73486eef这样的该怎么转换为图片?
2 回答
Helenr
TA贡献1780条经验 获得超4个赞
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();}
子衿沉夜
TA贡献1828条经验 获得超3个赞
输出到控制台当然是只能显示为文本(调用对象的toString()方法并将其输出),如果需要显示Image,考虑使用Swing中JLabel这样的组件。img=Toolkit.getDefaultToolkit().createImage(xp.asBytes());JLabellblImg=newJLabel(newImageIcon(img));//...将JLabel加入到JFrame,并将JFrame显示的代码
添加回答
举报
0/150
提交
取消