另外,能把Image对象转化成byte数组吗?

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/1111/1111432.xml?temp=.9418299
      

  2.   

    可以把Image 对象得到一个BufferedImage对象
    然后访问这个BufferedImage对象的方法getSubimage(int x, int y, int w, int h)得到指定子区域的BufferIamge ,由于BufferIamge 是Image的子类,就可以使用任何Image的方法了。
      

  3.   

    另外,能把Image对象转化成byte数组吗?
    在转化为BufferedImage 后可以得到int 数组,具体的是
    int[] BufferedImage.getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize);
    至于怎么Image转化为BufferedImage 
    // This method returns a buffered image with the contents of an image
        public static BufferedImage toBufferedImage(Image image) {
            if (image instanceof BufferedImage) {
                return (BufferedImage)image;
            }
        
            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();
        
            // Determine if the image has transparent pixels; for this method's
            // implementation, see e665 Determining If an Image Has Transparent Pixels
            boolean hasAlpha = hasAlpha(image);
        
            // Create a buffered image with a format that's compatible with the screen
            BufferedImage bimage = null;
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            try {
                // Determine the type of transparency of the new buffered image
                int transparency = Transparency.OPAQUE;
                if (hasAlpha) {
                    transparency = Transparency.BITMASK;
                }
        
                // Create the buffered image
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();
                bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);
            } catch (HeadlessException e) {
                // The system does not have a screen
            }
        
            if (bimage == null) {
                // Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;
                if (hasAlpha) {
                    type = BufferedImage.TYPE_INT_ARGB;
                }
                bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            }
        
            // Copy image to buffered image
            Graphics g = bimage.createGraphics();
        
            // Paint the image onto the buffered image
            g.drawImage(image, 0, 0, null);
            g.dispose();
        
            return bimage;
        }
      

  4.   

    可是转化成int数组后,怎么转回来呢?
      

  5.   

    public void setRGB(int startX,
                       int startY,
                       int w,
                       int h,
                       int[] rgbArray,
                       int offset,
                       int scansize)
    就可以了呀。你可以看看java.awt.image.BufferedImage
    的document!!!
      

  6.   

    多谢,
    我现在的问题是,流里面传的只能是byte[],不能是int[].
    能在帮帮忙吗?
      

  7.   

    需要进行JPEG压缩吗?
    如果需要的话可以这样
    JPEGImageEncoder ecod=JPEGCodec.createJPEGImageEncoder(out);
    //out是一个OutputStream,这样就可以传输了
    ecod.ecode(yourimage);
    由一个JPEGImageDecoder在流的接受端解压,就可以了
      

  8.   

    你可以去国外的网站上查找一下过于JPEG的影象格式,然后用操作文件方式读入影象文件,进行处理,一劳永逸。