我想把外部通信得到的一个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数据不正确?
请各位帮帮忙,谢谢先

解决方案 »

  1.   

    byte[] bytes = ri.data;
    FileOutputStream   fos   =   new   FileOutputStream("D://test1.jpg");
    fos.write(bytes);
    fos.close();
      

  2.   

    这个我有试过提示说不是个jpg格式的,其实我也不知道这个图片应该是什么类型的
      

  3.   

    ImageIcon icon=new ImageIcon(new String(字节数组));
      

  4.   

    这个byte[]的大小是width*height*2,不知道应该是什么格式的
      

  5.   

    我怎么觉得你的那个myImage没有用啊???
    那你创建时什么作用呢??
      

  6.   

    csdn不能编辑吗?我详细的讲下是如何得到这个byte[]的。
    通过和一个socket通信得到了bpp,width,height,和size这是个值,其中size为width*height*2,再用再用个byte[size]进行通讯得到了我的这个ri.data值,这个data应该就是张图片的数据,因为这个代码也是从别处搬来的,不过那里用的是eclipse的ImageData类,而我现在想把这个data数据搞成一张图片
      

  7.   

    写错了
    ImageIcon icon=new ImageIcon((字节数组);
      

  8.   

    回7L。你说的是yourImg?
    这个是在Boolean flag= img.getGraphics().drawImage(yourImg,0,0,ri.width,ri.height,null);  
    这步是为了写进个BufferedImage再写出的
      

  9.   

    顺便求助下,之后该如何处理这个ImageIcon,谢谢
      

  10.   

    不好意思  没看清你问的问题
    你上面的
    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 规定
      

  11.   

    你的那种方法是可行的!!也许别的地方出错了再好好看看 
    当然我上面说的这种方法也要用到那样的方式得到BufferedImage
      

  12.   

    按照你的说法确实有道理,不过JPEGImageEncoder这个类eclipse好像有
    The type JPEGImageEncoder is not accessible due to restriction on required library
    这个问题,所以就按照网上说的用了imageIO,而且我理解上那两句话的意思好像是将img以jpeg的格式写进fos这个流里不知道你的看法?
    总之先谢谢了
      

  13.   

    其实我比较想知道的是Boolean flag = img.getGraphics().drawImage(yourImg,  0,  0,ri.width,ri.height, null);
    这句返回个false说明什么,看英文没看懂
      

  14.   


    如果图像已经完全装入,并且其像素不再发生改变,则 drawImage 返回 true。否则 drawImage 返回 false,并且随着更多的图像可用或者到了绘制动画另一帧的时候,装入图像的进程就会通知指定的图像观察者。