Reputation: 13600
this is what I've gone so far and I can't seem to go further because I don't understand bitwise operations on the RGB
// Read from a file
File file = new File("src/a.gif");
image = ImageO.read(file);
int[] rgbarr = image.getRGB(0, 0, 13, 15, null, 0, 13);// image is 13*15
System.out.println(rgbarr.length);
for (int i : rgbarr)
{
System.out.println(i);
}
Output: was values such as -16777216 and -1 Because I've already made an image black and white just to ease my understanding
But in our case here let's suppose it would be just random image , how do I get from normal RGB image to binary values (0 or 1) for each pixel.
Upvotes: 1
Views: 5259
Reputation: 12901
I'm betting that each int in the array is a packed ARGB value;
boolean [] output = new boolean[rgbarr.length];
for ( int i=0; i<rgbarr.length; ++i ) {
int a = (rgbarr[i] >> 24 ) & 0x0FF;
int r = (rgbarr[i] >> 16 ) & 0x0FF;
int g = (rgbarr[i] >> 8 ) & 0x0FF;
int b = (rgbarr[i] ) & 0x0FF;
output[i] = (0.30*r + 0.59*g + 0.11*b) > 127;
}
The output equation I choose was taken from a definition of Luminance from wikipedia. You can change the scales so long as the coefficients add up to 1.
Upvotes: 2
Reputation:
You can do some thing like this,
BufferedImage img = (BufferedImage) image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img , "jpg", baos);
byte[] previewByte = baos.toByteArray();
Hope this helps!!
Upvotes: 1
Reputation: 1
You already have RGB values in the int[], that is color of each pixel. You could compare this to a specific background color, like black, to get a 1-bit pixel value. Then maybe set a bit in another appropriately sized byte[] array...
Upvotes: 0