无法将java.awt.image转换为int [] [] []

时间:2009-10-10 17:50:17

标签: java image-processing

我在Java程序中编写了一个用于图像处理的类。该类无法正确加载图像,'data'字段的格式为[row] [column] [a = 0,r = 1,g = 2,b = 3]。当我尝试加载图像时,数据填充为0。

public class Image {
    public int[][][] data;

    public int width;
    public int height;
    Image(int width, int height, java.awt.Color color) {
        this.data = new int [height][width][4];
    }
    Image(java.lang.String path) {
        this.load(path);
    }
    public void load(java.lang.String path) {
        java.awt.Image image = new javax.swing.ImageIcon(path).getImage();
        int[][][] data = new int[image.getHeight(null)][image.getWidth(null)][4];
        int[] oneDPix = new int[image.getHeight(null) * image.getWidth(null)];

        this.width = image.getWidth(null);
        this.height = image.getHeight(null);

        java.awt.image.PixelGrabber pixelGrabber = new java.awt.image.PixelGrabber(image, 0, 0, image.getWidth(null), image.getHeight(null), oneDPix, 0, image.getWidth(null));

        for(int row = 0; row < image.getHeight(null); row++) {
            int[] aRow = new int[image.getWidth(null)];
            for(int col = 0; col < image.getWidth(null);col++) {
                int element = row * image.getWidth(null) + col;
                aRow[col] = oneDPix[element];
            }
            for(int col = 0;col < image.getWidth(null);col++){
                data[row][col][0] = (aRow[col] >> 24)& 0xFF;
                data[row][col][1] = (aRow[col] >> 16) & 0xFF;
                data[row][col][2] = (aRow[col] >> 8) & 0xFF;
                data[row][col][3] = (aRow[col]) & 0xFF;
            }
        }
        this.data = data;
    }
    public void save(java.lang.String path) throws java.io.IOException{
        int[] oneDPix = new int[this.width * this.height * 4];
        for(int row = 0, cnt = 0; row < this.height; row++){
            for(int col = 0; col < this.width; col++) {
                oneDPix[cnt] = ((this.data[row][col][0] << 24)
                                       & 0xFF000000)
                         | ((this.data[row][col][1] << 16)
                                       & 0x00FF0000)
                          | ((this.data[row][col][2] << 8)
                                       & 0x0000FF00)
                               | ((this.data[row][col][3])
                                       & 0x000000FF);
                cnt++;
            }
        }
        java.awt.Image image = java.awt.Toolkit.getDefaultToolkit()
                .createImage(new java.awt.image.MemoryImageSource
                (width, height,oneDPix, 0, width));

        java.io.File imagefile = new java.io.File(path);
        String ext = ".png";
        java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage
                (this.width, this.height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        bufferedImage.getGraphics().drawImage(image, 0, 0, null);
        javax.imageio.ImageIO.write(bufferedImage, ext,imagefile);

    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

你永远不会告诉pixelGrabber grabPixels()。有关详细信息,请阅读PixelGrabber documentation

答案 1 :(得分:1)

您的第一个问题是您没有启动PixelGrabber,因此请在创建PixeGrabber后添加此内容:

try {
    pixelGrabber.grabPixels();
} catch (InterruptedException e) {
    e.printStackTrace();
}

第二个问题是当你调用createImage()时,它会创建宽度和高度为-1的图像。所以输出是零长度文件。

EDIT。要编写图像,请使用“png”而不是“.png”作为扩展名