我想把一组图片用流写在一个自定义文件里,程序里用到的时候从这个文件里"取",能实现么,
高手帮帮忙,分不够可以再加

解决方案 »

  1.   

    读取:
        
        ImageIcon icon = new ImageIcon(url);    int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        Image image = icon.getImage();
        int[] pixels = new int[w * h];
        PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
        pg.grabPixels();
        
        写入:
        BufferedImage image = new BufferedImage();
        int[] pixels; //read from stream
        
        image.setRGB(0, 0, w, h, pixels, 0, scansize);
      

  2.   

    谢谢 chongkai() ,读取的时候我有点不懂,按照我的需求,是把Image从硬盘上一个类似allImage.dat的文件里读出来,
    另外,我不想用到ImageIcon,这样的需求能实现么,谢谢啊!!
      

  3.   

    我只是提供一个思路:怎么从image得到pixel数组,以及怎样从pixel数组恢复出image,具体实现需要你再作研究。另外保存的时候需要以某种方式把image的长、宽保存下来,才能使用这种方式。
      

  4.   

    String imagePath = "Snap1.gif";//"/f-20.png";
     ImageIcon icon = new ImageIcon(imagePath);      
     int w = icon.getIconWidth();
     int h = icon.getIconHeight();
    这时候已经得到ImageIcon了,但是w和h返回的都是-1,是什么原因啊??
      

  5.   

    public void setRGB(int startX,
                       int startY,
                       int w,
                       int h,
                       int[] rgbArray,
                       int offset,
                       int scansize)还要请教 chongkai() 一下,setRGB没有返回值,这个方法到底做了一件什么事呢,我要把"数据"序列化到磁盘上,这个"数据"到底是哪个呢??
      

  6.   

    /**
         * Sets an array of integer pixels in the default RGB color model
         * (TYPE_INT_ARGB) and default sRGB color space,
         * into a portion of the image data.  Color conversion takes place
         * if the default model does not match the image 
         * <code>ColorModel</code>.  There are only 8-bits of precision for
         * each color component in the returned data when
         * using this method.  With a specified coordinate (x,&nbsp;y) in the   
         * this image, the ARGB pixel can be accessed in this way:
         * <pre>
         *    pixel   = rgbArray[offset + (y-startY)*scansize + (x-startX)];
         * </pre>
         * WARNING: No dithering takes place.
         *
         * @param startX,&nbsp;startY the starting coordinates
         * @param w           width of the region
         * @param h           height of the region
         * @param rgbArray    the rgb pixels
         * @param offset      offset into the <code>rgbArray</code>
         * @param scansize    scanline stride for the <code>rgbArray</code> 
         * @see #getRGB(int, int)
         * @see #getRGB(int, int, int, int, int[], int, int)
         */
        public void setRGB(int startX, int startY, int w, int h,
                            int[] rgbArray, int offset, int scansize) {
            int yoff  = offset;
            int off;
            Object pixel = null;        for (int y = startY; y < startY+h; y++, yoff+=scansize) {
                off = yoff;
                for (int x = startX; x < startX+w; x++) {
                    pixel = colorModel.getDataElements(rgbArray[off++], pixel);
                    raster.setDataElements(x, y, pixel);
                }
            }
        }//这个存储是做什么呢?
    raster.setDataElements(x, y, pixel);WritableRaster,这个对象如何用呢,这是我最终要序列化到磁盘的数据源么??
      

  7.   

    用PixelGrabber把image中的每个象素依次抓下来,存在一个int数组中,存进磁盘文件需要恢复image时,从文件中把数据读到一个int数组中,再调用setRGB,把这个数组中的象素值放在image里。好像很清楚了吧。
      

  8.   

    import javax.swing.ImageIcon;
    import java.awt.Image;
    import java.awt.image.PixelGrabber;
    import java.net.URL;
    import java.io.File;
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBuffer;
    import java.io.DataInputStream;
    import java.io.FileInputStream;public class ImageTest {    public static void main(String[] args) {
            ImageTest t = new ImageTest();        t.saveToFile(t.getPixels(), "E:/test.png");
        }    //Log log = Log.getInstance();
            public int[] getPixels() {
    String imagePath = "/f-20.png";
         ImageIcon icon = new ImageIcon(getClass().getResource(imagePath)); //(imagePath);
            int[] pixels = null;
            int w = icon.getIconWidth();
            int h = icon.getIconHeight();
            Image image = icon.getImage();
            pixels = new int[w * h];
            PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
            //log.debug("w", w);
            //log.debug("h", h);
            //log.debug("image.w", image.getWidth(null));
            //log.debug("image.h", image.getHeight(null));
            //log.debug("image", image);
            //log.debug("pixels.length", pixels.length);
            try {
                pg.grabPixels();
            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            return pixels;
        }    public void saveToFile(int[] data, String filePath) {        File f = new File(filePath);        try {            //ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
                DataOutputStream out = new DataOutputStream(new
                    FileOutputStream(f));            int rows = data.length;            //out.writeInt(rows); //写入数组的行数            //写入数组具体值
                for (int m = 0; m < rows; m++) {
                    out.writeInt(data[m]);
                }
                out.close();
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }    }
        
    }我是这样写的,可是,文件存到磁盘上,文件比原来大了几乎10倍,而且用任何图片工具都打不开是什么原因呢?
      

  9.   

    晕倒!你存成png,它就是png了吗?当然任何图片工具都打不开了。你的文件里只不过是一些int值而已。我不知道你要干什么,把图片文件读出来,又存成另一个文件。。