If you draw into another image you'll need a big pile of extra heap space though, which can easily become a major issue if you're dealing with large images.
It's not something I've used, but isn't ColorConvertOp what you're after? I'm sure a quick google will reveal how to use it.
> You can create a [url
> http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image
> /BufferedImage.html]BufferedImage[/url] with
> TYPE_BYTE_BINARY or TYPE_BYTE_GRAY, and draw the
> color image on this BufferedImage.
Thanks for the idea, but I don't understand how to create a binary BufferedImage from a color BufferedImage (or from file, which is better for me). I didn't found such BufferedImage constructor.
I tried to getData() from the color BufferedImage and set this data (using setData()) into (the previously created with TYPE_BYTE_BINARY) binary BufferedImage. Thus results in a very strange picture...
Please Help !
> If you draw into another image you'll need a big pile
> of extra heap space though, which can easily become a
> major issue if you're dealing with large images.
What do you mean by large images ? Is 1000*1000 large ?
> It's not something I've used, but isn't
> ColorConvertOp what you're after? I'm sure a quick
> google will reveal how to use it.
ColorConvertOp really helps to create the a grayscale image, but I didn't found how to create a binary image using this method (I did't found the proper constant which can take place of ColorSpace.CS_GRAY).
Here is my code:
public static BufferedImage getGrayScaleImage(String filename) {
ColorConvertOp grayOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage originalImage = null, grayScaleImage;
File file = new File(filename);
try {
originalImage = ImageIO.read(file);
} catch (IOException e) {
System.out.println("Could not open file: " + filename);
System.exit(1);
}
grayScaleImage = grayOp.filter(originalImage, null);
return grayScaleImage;
}
Can anyone help, please ?
BufferedImage image = ImageIO.read(new File("d:/IMG_5167.JPG"));
BufferedImage bwImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
bwImage.createGraphics().drawImage(image, 0, 0, null);
Were areas of your original image transparent? Remember that by default all int arrays are popuated with zeroes. In the case of an "RGB" format image, that represents opaque black (transparent black in "ARGB"). So when you create a new RGB image, it's opaque black by default.
Otherwise, dunno, if your background was nearly white but not quite (eg 0xfffefefe) maybe the threshold makes any non-white value black. I've not converted to binary images before, only from.
> Were areas of your original image transparent?
> Remember that by default all int arrays are popuated
> with zeroes. In the case of an "RGB" format image,
> that represents opaque black (transparent black in
> "ARGB"). So when you create a new RGB image, it's
> opaque black by default.
>
> Otherwise, dunno, if your background was nearly white
> but not quite (eg 0xfffefefe) maybe the threshold
> makes any non-white value black. I've not converted
> to binary images before, only from.
Thanks !
I think that this is exactly the problem.
The problem is that I can't figure out where exactly I need to set the isOpaque property to true. I thougt maybe to scan the original image with a mask that sets alpha to opaque (is it 0x00 or 0xff ?), but it seems very inefficient to me. Is there another way ? Here is my code:
public static BufferedImage getBinaryImage(String filename) {
BufferedImage originalImage = null, binaryImage;
try {
originalImage = ImageIO.read(new File(filename));
} catch (IOException e) {
System.out.println("Could not open file: " + filename);
System.exit(1);
}
binaryImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
binaryImage.createGraphics().drawImage(originalImage, 0, 0, null);
return binaryImage;
}
you can also try this line of code, this will change your image into gray and back ground will not be affected.
PlanerImage image;
image = JAI.create("bandselect", image, new int[]{ 0, 0, 0 });
Can't you just pre-fill the target image? After you create it, use getGraphics() and then the Graphics methods setColor() and fillRect() passing the size of the image, before rendering the source image into it. Don't forget to dispose() the Graphics object returned by the Image once you're done with it.