在writableRaster类中,有一个方法:

public void setPixels(int x,
             int y,
             int w,
             int h,
             int[] iArray)


iArray中应存储像素信息的位置
对于像素:233(r)244(g)10(b),应如何存储在iArray中?

iArray[0] = 233iArray[1] = 244iArray[2] = 10
还是iArray[0] = [24424410]

最佳答案

简短的答案,应该是iArray[0] = 233, iArray[1] = 244, iArray[2] = 10而不是iArray[0] = [24424410]

但是,数据量将取决于WritableRaster支持的内容。考虑下面的示例,其中我们检索两个WriteableRasterBufferedImages。一个包含RGB数据,另一个包含ARGB数据。如果检索像素值,则可以看到该数组对于RGB数据的长度为3,对于ARGB数据的长度为4。

码:

public static void main(String[] args) {
    BufferedImage rgbImage = new BufferedImage(8, 8,
            BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();

    BufferedImage argbImage = new BufferedImage(8, 8,
            BufferedImage.TYPE_INT_ARGB);
    WritableRaster argbRaster = argbImage.getRaster();

    rgbImage.setRGB(0, 0, new Color(255, 125, 1, 16).getRGB());
    argbImage.setRGB(0, 0, new Color(255, 125, 1, 16).getRGB());

    int[] rgb = rgbRaster.getPixel(0, 0, (int[]) null);
    int[] argb = argbRaster.getPixel(0, 0, (int[]) null);
    System.out.print("rgb:");
    for (int i = 0; i < rgb.length; ++i)
        System.out.print(" "+rgb[i]);

    System.out.print("\nargb:");
    for (int i = 0; i < argb.length; ++i)
        System.out.print(" "+argb[i]);
}


输出:

rgb: 255 125 1
argb: 255 125 1 16

关于java - writableRaster中的像素字段格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33783808/

10-12 22:33