字节数组是通过这种方式获得的——
BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenDimension));
byte[] array = ((DataBufferByte)getGraycaleImage(image).getRaster().getDataBuffer()).getData();
// Method getGraycaleImage returns a grayscaled BufferedImage, it works fine
现在我如何从字节数组重建这个灰度图像?
我不太了解 ARGB、RGB 或灰度图像。我试过这个 -
private Image getGrayscaleImageFromArray(byte[] pixels, int width, int height)
{
int[] pixels2=getIntArrayFromByteArray(pixels);
MemoryImageSource mis = new MemoryImageSource(width, height, pixels2, 0, width);
Toolkit tk = Toolkit.getDefaultToolkit();
return tk.createImage(mis);
}
private int[] getIntArrayFromByteArray(byte[] pixelsByte)
{
int[] pixelsInt=new int[pixelsByte.length];
int i;
for(i=0;i<pixelsByte.length;i++)
pixelsInt[i]=pixelsByte[i]<<24 | pixelsByte[i]<<16
| pixelsByte[i]<<8 | pixelsByte[i]; // I think this line creates the problem
return pixelsInt;
}
当我画这个图像时,它不是黑白的,而是橙色和灰色的。