图像到byte []到图像

时间:2014-04-18 21:29:44

标签: java eclipse image

我尝试使用Java进行一些图像处理。 首先,在做任何过滤器或任何事情之前,我在我的图像上进行转换过程到字节数组,然后将其转换回图像并保存以查看结果如何。

我没有将输出图像作为输入图像,有一些丢失的信息/数据,导致输出颜色看起来不同。

请告诉我这是什么问题;我错过了什么。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Ola {

    BufferedImage img = null;

    public void loadImage() {
        try {
            img = ImageIO.read(new File("/home/a/Pictures/Tux-vegeta.png"));
           } catch (IOException e) {
               System.out.println("image(s) could not load correctly, try changing the path");
           }
    }

    public byte[] convertToArray() {
        int w = img.getWidth();
        int h = img.getHeight();

        int bands = img.getSampleModel().getNumBands();
        System.out.print(bands);

        if (bands != 4) {
            System.out.println("The image does not have 4 color bands");            
        }

        byte bytes[] = new byte[4 * w * h];
        int index = 0;

        for(int y = 0; y < h; y++) {
            for(int x = 0; x < w; x++) {
                int pixel = img.getRGB(x, y);

                int alpha = (pixel >> 24) & 0xFF;
                int red = (pixel >> 16) & 0xFF;
                int green = (pixel >> 8) & 0xFF;
                int blue = pixel & 0xFF;

                bytes[index++] = (byte) alpha;
                bytes[index++] = (byte) red;
                bytes[index++] = (byte) green;
                bytes[index++] = (byte) blue;
           }
        }
        return bytes;   
    }
    public void convertToImage(byte[] bytes) {
        try {
            int w = 300;
            int h = 300;
            int index = 0;
            BufferedImage resultPNG = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j ++) {
                    int pixel = (bytes[index] << 24) | (bytes[index + 1] << 16) | (bytes[index + 2] << 8) | (bytes[index + 3]);
                    resultPNG.setRGB(j, i, pixel);
                    index += 4;
                }
            }
            File outputImage = new File("/home/a![enter image description here][1]/Pictures/test.png");
            ImageIO.write(resultPNG, "png", outputImage);
        } catch (IOException e) {
            System.out.println("image write error");
        }
    }

    public static void main(String[] args) {
        Ola ola = new Ola();
        ola.loadImage();
        ola.convertToImage(ola.convertToArray());

    }
}

2 个答案:

答案 0 :(得分:1)

您缺少的是将已签名的字节恢复为无符号:

更改行

int pixel = (bytes[index] << 24) | (bytes[index + 1] << 16) | (bytes[index + 2] << 8) | (bytes[index + 3]);

以下内容:

int pixel = ((bytes[index] & 0xFF) << 24) | ((bytes[index + 1] & 0xFF) << 16) | ((bytes[index + 2] & 0xFF) << 8) | (bytes[index + 3]  & 0xFF);

答案 1 :(得分:-1)

由于你想要alpha通道,你的目的地应该使用TYPE_INT_ARGB而不是TYPE_INT_RGB,使用RGB将导致缓冲的图像忽略alpha字节。

由于PNG未加载到TYPE_INT_ARGB颜色模型中,您可以使用图形对象将加载的bufferedimage绘制到使用TYPE_INT_ARGB创建的bufferedimage对象中。

public void loadImage() {
    try {
         BufferedImage tempimg = ImageIO.read(new File("/home/a/Pictures/Tux-vegeta.png"));
         img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
         Graphics2D g2 = img.createGraphics();
         g2.drawImage(tempimg,null,0,0);
       } catch (IOException e) {
           System.out.println("image(s) could not load correctly, try changing the path");
       }
}