Creating 8 bit image from byte array - java

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.

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

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

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

Related

BufferedImage from 4-bit data buffer

Hello I have a problem with converting my 4-bit data buffer to WritableRaster.
Image resolution: 1024x768 (786432)
Here is description what I'm doing.
1) Create 4-bit BufferedImage
bit4Image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
Graphics graphics = bit4Image.getGraphics();
graphics.drawImage(originalImage, 0, 0, null);
graphics.dispose();
//4-bit BufferedImage created. 4-bit BufferedImage is properly made cause it can be saved to hdd and looks good
2) Get byte array from DataBuffer from 4-bit
byte[] pixelData = ((DataBufferByte) bit4Image.getRaster().getDataBuffer()).getData();
// pixelData length is 393216
3) Now I want to create BufferedImage from this byte array pixelData
BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
DataBufferByte buffer = new DataBufferByte(pixelData, pixelData.length);
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, width, 1, new int[]{0}, new Point(0, 0));
dest.setData(raster);
Problem is when I call Raster.createInterleavedRaster. Exception:
java.awt.image.RasterFormatException: Data array too small (should be > 786431 )
I also tried something like this
BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
dest.getRaster().setDataElements(0, 0, width, height, pixelData);
But this one gives me similar failure:
java.lang.ArrayIndexOutOfBoundsException: 393216
Could someone give me a hint or show the proper way of setting this 4-bit pixelData to WritableRaster?
Resolved.
I just had to create WritableRaster with giving it SampleModel:
SampleModel sm = MY_BIT_4_COLOR_MODEL.createCompatibleSampleModel(width, height);
WritableRaster wr = Raster.createWritableRaster(sm, buffer, new Point(0,0));
The initial code you had almost worked, the only problem is you tried to create an "interleaved" raster. For palette images (IndexColorModel) you typically have only one sample (the palette index) per pixel, so there's no samples to interleave.
Instead, your pixel data is 4 bit/pixel, stored as two pixels per byte. Storing more samples per storage unit, is often referred to as "packed". This means you need to create a "packed" raster, using one of the Raster.createPackedRaster methods.
Here's a full, runnable sample:
public static void main(String[] args) {
int width = 100;
int height = 100;
// Create initial 4 bit image
IndexColorModel icm = new IndexColorModel(4, 16, new int[16], 0, false, -1, DataBuffer.TYPE_BYTE);
BufferedImage bit4Image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, icm);
// ...you probably do something in between here
// Get the pixel data
byte[] pixelData = ((DataBufferByte) bit4Image.getRaster().getDataBuffer()).getData();
// ...you probably do something in between here
// Create a data buffer around the array, wrap in raster
DataBufferByte buffer = new DataBufferByte(pixelData, pixelData.length);
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, 4, null);
// Finally create a copy of the image, sharing pixel data
BufferedImage copy = new BufferedImage(icm, raster, icm.isAlphaPremultiplied(), null);
System.out.println("copy: " + copy);
}

Convert color image into black and white using java created from scratch

I want to convert my picture from colored to Black and white which seems to be created from scratch.
Here is the code which i tried as described on the different post:
BufferedImage bi = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
ImageIO.write(bi, "PNG", new File("/Users/bng/Documents/rendered2.png"));
op.filter(bi, bi);
But still my image is not converted to the Black and white. Additionally, this code is increasing the rendered2.png image size to 10 folds.
Also, it would be great if i could find some Java 8 way of doing this.
Any suggestions?
Here is the code which worked for me:
BufferedImage input = ImageIO.read(new File("/Users/bng/Documents/Photograph.jpg"));
// Create a black-and-white image of the same size.
BufferedImage im = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
// Get the graphics context for the black-and-white image.
Graphics2D g2d = im.createGraphics();
// Render the input image on it.
g2d.drawImage(input, 0, 0, null);
// Store the resulting image using the PNG format.
ImageIO.write(im, "PNG", new File("/Users/bng/Documents/rendered.png"));
It was BufferedImage.TYPE_BYTE_BINARY which provided me the exact solution.
Lokking for the Java 8 Version for above code.
You have to find RGB of the existing colors of the image you want to change it.
Fyi, you want to change it as white RGB value is (255,255,255) and for black RGB value is (0,0,0)
Following method easily do the color change if you apply correct way of your requirement
private BufferedImage changeColor(BufferedImage image, int srcColor, int replaceColor)
{
BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = destImage.createGraphics();
g.drawImage(image, null, 0, 0);
g.dispose();
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
if (destImage.getRGB(width, height) == srcColor)
{
destImage.setRGB(width, height, replaceColor);
}
}
}
return destImage;
}
you have to use the ColorConvertOp in a proper way:
create Source image
apply filter
save dest
example:
BufferedImage src = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage dest = op.filter(src, null);
ImageIO.write(dest, "PNG", new File("/Users/bng/Documents/rendered2.png"));
src:
dest:

How to obtain a resized BufferedImage

Say I have 2 TIF images and I read one of them into a BufferedImage instance:
ImageReader reader = ImageIO.getImageReadersByFormatName("tif").next();
reader.setInput(inputStream, false); // inputStream is the first image.
BufferedImage bufferedImage = reader.read(0);
Now I want to create a new BufferedImage without reading the other image. It should be the same as the previous one, but only different in size. imageType seems to be 0 for TIF images, but the following doesn't work.
BufferedImage largeBufferedImage = new BufferedImage(newWidth, newHeight, 0);
Is there any way to clone the existing BufferedImage and only change its size?
BTW I want to be able to do it for any image format. I don't want to deal with details like imageType if possible.
BufferedImage deepCopy(BufferedImage bi)/*method to clone BufferedImage*/ {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
BufferedImage newImg = deepCopy(oldImg);//clone it
Graphics2D g = newImg.createGraphics();
g.drawImage(newImg, 0, 0, width, height, null);//newImg will be resized
When you draw in your paint method, you can add more parameters to stretch and scale image, see g.drawImage at this link.
After some trial and error, I found a working solution for my problem.
private BufferedImage copyAndResize(BufferedImage source, int width, int height)
{
ColorModel cm = source.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = source.copyData(null);
SampleModel sm = raster.getSampleModel().createCompatibleSampleModel(width, height);
WritableRaster newRaster = WritableRaster.createWritableRaster(sm, null);
BufferedImage newBi = new BufferedImage(cm, newRaster, isAlphaPremultiplied, null);
return newBi;
}

Convert colored image to back/white image

I am working on converting colored image to black and white image. I am using BufferedImage for this with the type of TYPE_BYTE_BINARY. But output image is not converted correctly. For example, if image contains blue letters on black background, result image for this part is totaly black. Can anybody help me? My code is below.
//Invert the colormodel
byte[] map = new byte[] { (byte) (255), (byte) (0) };
IndexColorModel colorModel = new IndexColorModel(1, 2, map,
map, map);
BufferedImage bufferedImage = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_BYTE_BINARY, colorModel);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
Blue has a very low intensity, so blue (like RGB(0, 0, 255)) turning out black when converted to b/w with 50% threshold is to be expected. Try brightening the original image before converting to b/w, to increase the parts of the image coming out white.
You can use RescaleOp to brighten the image prior to conversion, or pass an instance along with your image to the drawImage method that takes a BufferedImageOp as parameter. Note that you can scale the R, G and B values independently.
BufferedImage bufferedImage= new BufferedImage(img.getWidth(null), img.getHeight(null);
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(bufferedImage, bufferedImage);
check this link

Problem converting bytes from a Bing Map request into a BufferedImage

I have very little experience with Java IO and images, and I've been unsuccessful at converting an aerial image saved as a byte array into a BufferedImage.
Here's my code:
int width = scaledImage.getWidth();
int height = scaledImage.getHeight();
DataBuffer buffer = new DataBufferByte(scaledImage.getImage(), scaledImage.getImage().length, 0);
SampleModel sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, new int[]{(byte)0xf});
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);
ColorModel colorModel = imageManager.generateColorModel();
BufferedImage image = new BufferedImage(colorModel, raster, false, null);
Most of this code is borrowed from http://www.exampledepot.com/egs/java.awt.image/Mandelbrot2.html.
This code throws the following exception
java.awt.image.RasterFormatException: Data array too small (should be 122499 )
the actual length of the data array is 52341.
The dimensions are 350px X 350px
Here is the line that is killing you:
DataBuffer buffer = new DataBufferByte(scaledImage.getImage(), scaledImage.getImage().length, 0);
The example you show does width * height instead of scaledImage.getImage().length. In the model you've chosen, you need a byte per pixel, which is 350x350 or 122500.

Categories