我想把外部通信得到的一个byte[]转成张图片,但有些问题,部分代码如下:Image yourImg = Toolkit.getDefaultToolkit().createImage(ri.data); //ri.data就是个byte数组
BufferedImage img = new BufferedImage(ri.width, ri.height, BufferedImage.TYPE_INT_RGB);
Boolean flag = img.getGraphics().drawImage(yourImg, 0, 0,ri.width,ri.height, null);
FileOutputStream fos = new FileOutputStream("test1.jpg");
ImageIO.write(img, "jpg", fos);
fos.close();首先Image yourImg = Toolkit.getDefaultToolkit().createImage(ri.data);这句是过的
但在Boolean flag = img.getGraphics().drawImage(yourImg, 0, 0,ri.width,ri.height, null); 这句之后发现img里面的data都是0,而且返回的一个false,不知道是什么原因?难道还是得到的ri.data数据不正确?
请各位帮帮忙,谢谢先
BufferedImage img = new BufferedImage(ri.width, ri.height, BufferedImage.TYPE_INT_RGB);
Boolean flag = img.getGraphics().drawImage(yourImg, 0, 0,ri.width,ri.height, null);
FileOutputStream fos = new FileOutputStream("test1.jpg");
ImageIO.write(img, "jpg", fos);
fos.close();首先Image yourImg = Toolkit.getDefaultToolkit().createImage(ri.data);这句是过的
但在Boolean flag = img.getGraphics().drawImage(yourImg, 0, 0,ri.width,ri.height, null); 这句之后发现img里面的data都是0,而且返回的一个false,不知道是什么原因?难道还是得到的ri.data数据不正确?
请各位帮帮忙,谢谢先
FileOutputStream fos = new FileOutputStream("D://test1.jpg");
fos.write(bytes);
fos.close();
那你创建时什么作用呢??
通过和一个socket通信得到了bpp,width,height,和size这是个值,其中size为width*height*2,再用再用个byte[size]进行通讯得到了我的这个ri.data值,这个data应该就是张图片的数据,因为这个代码也是从别处搬来的,不过那里用的是eclipse的ImageData类,而我现在想把这个data数据搞成一张图片
ImageIcon icon=new ImageIcon((字节数组);
这个是在Boolean flag= img.getGraphics().drawImage(yourImg,0,0,ri.width,ri.height,null);
这步是为了写进个BufferedImage再写出的
你上面的
BufferedImage img = new BufferedImage(ri.width, ri.height, BufferedImage.TYPE_INT_RGB);
Boolean flag = img.getGraphics().drawImage(yourImg, 0, 0,ri.width,ri.height, null);
到此img内还是没存储图像的
因为你只是够早了一个Graphics对象
你应该这样
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
FileOutputStream fos = new FileOutputStream("img.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(img);
out.close();
输出的图片是按照你构造的img输出的
大小由width height 规定
当然我上面说的这种方法也要用到那样的方式得到BufferedImage
The type JPEGImageEncoder is not accessible due to restriction on required library
这个问题,所以就按照网上说的用了imageIO,而且我理解上那两句话的意思好像是将img以jpeg的格式写进fos这个流里不知道你的看法?
总之先谢谢了
这句返回个false说明什么,看英文没看懂
如果图像已经完全装入,并且其像素不再发生改变,则 drawImage 返回 true。否则 drawImage 返回 false,并且随着更多的图像可用或者到了绘制动画另一帧的时候,装入图像的进程就会通知指定的图像观察者。