Skip to main content

drawimage on a bilevel image does not work

7 replies [Last post]
jiezhang
Offline
Joined: 2005-12-29

I am using drawimage method to add a BufferedImage to another BufferedImage.
The base image is a bilevel (white and black) and the added image is bilevel too.
But in result, the added image just became a black frame in base image.
Anyone has an idea?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dmitri Trembovetski

Have you tried without extra alpha? Just with the default
SrcOver compositing mode?

Thanks,
Dmitri

java2d@JAVADESKTOP.ORG wrote:
> I am trying to add one image above another image.
> And tried alpha from 0 to 1, they all did not work.
> So what's right way to do this?
> [Message sent by forum member 'jiezhang' (jiezhang)]
>
> http://forums.java.net/jive/thread.jspa?messageID=291351
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Dmitri Trembovetski

java2d@JAVADESKTOP.ORG wrote:
> I am using drawimage method to add a BufferedImage to another BufferedImage.
> The base image is a bilevel (white and black) and the added image is bilevel too.
> But in result, the added image just became a black frame in base image.
> Anyone has an idea?

Please post a test reproducing the issue.

Thanks,
Dmitri

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

jiezhang
Offline
Joined: 2005-12-29

This is source code. How do I give images.

import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.awt.FontMetrics;
import java.awt.geom.Rectangle2D;
import java.awt.GraphicsEnvironment;

import java.util.Iterator;
import java.io.File;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.IIOImage;

public class Testdraw {

public static void main(String [] args) {
String inputFileName = "/tmp/tkidifx3.tif";
String addedFileName = "/tmp/tkidiw33g.tif";
String outputFileName = "/tmp/tkidifx3w.tif";
String outputFormat = "tif";

try {
BufferedImage img = ImageIO.read((new File(inputFileName)));
BufferedImage added_image = ImageIO.read((new File(addedFileName)));

Graphics2D g2d = (Graphics2D) img.getGraphics();

//Create an alpha composite of 40%
float opacity = 0.4f;
AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
g2d.setComposite(alpha);

g2d.drawImage(added_image, 200, 200, null);
g2d.dispose();

Iterator writers = ImageIO.getImageWritersByFormatName(outputFormat);
ImageWriter writer = null;
while(writers.hasNext()) {
writer = (ImageWriter)writers.next();
if(writer.getClass().getName().startsWith("com.sun")) {
// Break on finding the core provider.
break;
}
}
if(writer == null) {
throw new Exception("Cannot find core " + outputFormat.toUpperCase() + " writer!");
}

File f = new File(outputFileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);

ImageWriteParam wp = writer.getDefaultWriteParam();

writer.write(null,new IIOImage(img, null, null),wp);

ios.close();
} catch (Throwable t) {
t.printStackTrace();
}

}

}

trembovetski
Offline
Joined: 2003-12-31

Not quite sure what you expected. If you're compositing two 1-bit images with .4 extra alpha, you'll pretty much get 0 in the result since extra alpha is shifting all colors towards index 0 which is black by default.

Dmitri

jiezhang
Offline
Joined: 2005-12-29

I am trying to add one image above another image.
And tried alpha from 0 to 1, they all did not work.
So what's right way to do this?

Harald Kuhr

Hi,

You can't do an alpha-blended composition of two bi-level images, as a
bilevel image has only two levels, either completely white or
completely black. So you first have to create a new temporary "true
color" BufferedImage (any of the TYPE_INT_RGB or TYPE_3/4BYTE_RGB/BGR
will probably do fine), and then draw both your images into that. Use
alpha only for the second image. Good luck! ;-)

--
Harald K

On 4. aug.. 2008, at 17.50, java2d@JAVADESKTOP.ORG wrote:

> I am trying to add one image above another image.
> And tried alpha from 0 to 1, they all did not work.
> So what's right way to do this?
> [Message sent by forum member 'jiezhang' (jiezhang)]
>
> http://forums.java.net/jive/thread.jspa?messageID=291351
>
> =
> =
> =
> =
> =
> ======================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

jiezhang
Offline
Joined: 2005-12-29

That way should work. Another way to make it work is converting the first one to gray image and draw the second on it.
But I am hoping to get thing done without introducing the third image or doing a content conversion.