Creating 8 bit image from byte array

狂风中的少年 提交于 2019-12-02 02:43:54

问题


The byte array is obtained this way -

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

now how do i reconstruct this grayscale image from the byte array?

I don't know much about ARGB, RGB or grayscale images. I tried this -

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;
}

When I draw this image it's not black and white, rather something like orange and gray.


回答1:


I hope it'll help you if I explain to you how to convert from ARGB/RGB 2 gray cause there are too many unknown functions and classes :P

ARGB is 32 bit/pixel so 8 bit for every channel. the alpha channel is the opacity so the opposite of transparency so 0 is transparent.

RGB is 24 bit/pixel. to convert from ARGB to RGB you have to dismiss the alpha channel.

to convert from RGB to grayscale u have to use this formula:

0.2989 * R + 0.5870 * G + 0.1140 * B

so you have to figure out which byte belongs to which channel ;)




回答2:


You have to specify the correct ColorSpace corresponding to a grayscale image.

Here's an example, as found on http://technojeeves.com/joomla/index.php/free/89-create-grayscale-image-on-the-fly-in-java:

public static BufferedImage getGrayscale(int width, byte[] buffer) {
    int height = buffer.length / width;
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    int[] nBits = { 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, false, true,
            Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    SampleModel sm = cm.createCompatibleSampleModel(width, height);
    DataBufferByte db = new DataBufferByte(buffer, width * height);
    WritableRaster raster = Raster.createWritableRaster(sm, db, null);
    BufferedImage result = new BufferedImage(cm, raster, false, null);

    return result;
}



回答3:


This will work. Just make sure you tweak the image type the way you need:

Image img = new ImageIcon(array).getImage();
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
image.createGraphics().drawImage(img, 0, 0, null);


来源:https://stackoverflow.com/questions/12154090/creating-8-bit-image-from-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!