Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Transparent BG for loaded Image

Reply
Thread Tools

Transparent BG for loaded Image

 
 
Steffen Krause
Guest
Posts: n/a
 
      02-27-2004
Hi all,

I'm pretty new to imaging with java so please excuse if I'm asking a dump
question. My problem is:

I load an image from a BMP file using JAI 1.1.2. So far so good, now I have
it as BufferedImage. Next I want to draw it on top of another image but
with it's background beeing transparent. Just to let you know exactly what
I mean, the background image is a photo and the other pic is a blue filled
circle with a magenta background. Now the blue circle should be drawn over
the photo but of course without the magenta showing. Can anybody help me??

Thanks,
Steffen.
 
Reply With Quote
 
 
 
 
Tom N
Guest
Posts: n/a
 
      02-27-2004
"Steffen Krause" wrote:
> I'm pretty new to imaging with java so please excuse if I'm asking a dump
> question. My problem is:
>
> I load an image from a BMP file using JAI 1.1.2. So far so good, now I

have
> it as BufferedImage. Next I want to draw it on top of another image but
> with it's background beeing transparent. Just to let you know exactly

what
> I mean, the background image is a photo and the other pic is a blue filled
> circle with a magenta background. Now the blue circle should be drawn over
> the photo but of course without the magenta showing. Can anybody help me??


The obvious answer is to convert the BMP to a GIF with a transparent
background (GIF supports transparency). Then it all just works, without any
difficulty. GIF is of course not Windows specific so that's another
advantage

Does it have to be a BMP file?


 
Reply With Quote
 
 
 
 
Steffen Krause
Guest
Posts: n/a
 
      02-27-2004
>
> Does it have to be a BMP file?


Unfortunately yes if I wanna stay compatible to some other programms. But
does the image stay a BMP even when I loaded it as BufferedImage? I mean,
isn't there a way when I draw the BMP on top of the photo to filter out
the background? Somewhere I read that I have to use one of the
BufferedImageOp to do a transformation.


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      02-27-2004

"Steffen Krause" <(E-Mail Removed)> wrote in message
newspr30ui5cl7uaw89@53.47.1.4...
> >
> > Does it have to be a BMP file?

>
> Unfortunately yes if I wanna stay compatible to some other

programms. But
> does the image stay a BMP even when I loaded it as BufferedImage? I

mean,
> isn't there a way when I draw the BMP on top of the photo to filter

out
> the background? Somewhere I read that I have to use one of the
> BufferedImageOp to do a transformation


You need to know the background color of your bitmap
that you want to be transparent.
Then loop through all pixels of your BufferedImage
and replace all pixels of background color
with transparent (new Color(0,0,0,0)).

Adam


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-27-2004
Steffen Krause wrote:
> I load an image from a BMP file using JAI 1.1.2. So far so good, now I
> have it as BufferedImage. Next I want to draw it on top of another image
> but with it's background beeing transparent.


Then that part of the image which should be transparent needs to have
its alpha component set to transparent (0.0). Or (an old trick), you
reverse the upper and lower image: You set all of the underlying image
to transparent, and actually paint the underlying image over the top image.

> Just to let you know
> exactly what I mean, the background image is a photo and the other pic
> is a blue filled circle with a magenta background.


Magenta? Well, what company could that be? )

> Now the blue circle
> should be drawn over the photo but of course without the magenta
> showing. Can anybody help me??


Once you have the alpha of the background set correctly, create an
AlphaComposite, e.g. with SRC_OVER or DST_OVER (depends which image you
paint over which). Get the Graphics2D of one of the BufferedImages, and
set the composite on it. Then paint the other image over the first one,
using the Graphics2D object.

/Thomas

 
Reply With Quote
 
ak
Guest
Posts: n/a
 
      02-27-2004
> I load an image from a BMP file using JAI 1.1.2. So far so good, now I
have
> it as BufferedImage. Next I want to draw it on top of another image but
> with it's background beeing transparent. Just to let you know exactly

what
> I mean, the background image is a photo and the other pic is a blue filled
> circle with a magenta background. Now the blue circle should be drawn over
> the photo but of course without the magenta showing. Can anybody help me??


hmm, if it is just blue circle - you could just draw blue circle to Graphics
of your BufferedImage.

____________

http://reader.imagero.com the best java image reader.


 
Reply With Quote
 
Marco Schmidt
Guest
Posts: n/a
 
      02-27-2004
Tom N:

>The obvious answer is to convert the BMP to a GIF with a transparent
>background (GIF supports transparency).
>Then it all just works, without any difficulty.


No, that may or may not work. GIF can store only images with up to 256
colors, so a truecolor BMP file may lose quality when converted.

PNG may be the format of choice - supported since Java 1.3, can
include different types of transparency information.

BMP does not support transparency, as far as I know.

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
Steffen Krause
Guest
Posts: n/a
 
      02-27-2004

> You need to know the background color of your bitmap
> that you want to be transparent.
> Then loop through all pixels of your BufferedImage
> and replace all pixels of background color
> with transparent (new Color(0,0,0,0)).


Thanks Adam, I'll try that. I can do that with that ColorConvertOp, right?

 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      02-27-2004

"Steffen Krause" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
> > You need to know the background color of your bitmap
> > that you want to be transparent.
> > Then loop through all pixels of your BufferedImage
> > and replace all pixels of background color
> > with transparent (new Color(0,0,0,0)).

>
> Thanks Adam, I'll try that. I can do that with that ColorConvertOp,

right?

I do not know, I have never used ColorConvertOp.
My first guess was to use BufferedImage.getRGB/setRGB,
that will work correctly for sure, but I'm not sure
about performance though.

Adam


 
Reply With Quote
 
Steffen Krause
Guest
Posts: n/a
 
      02-27-2004

> I do not know, I have never used ColorConvertOp.
> My first guess was to use BufferedImage.getRGB/setRGB,
> that will work correctly for sure, but I'm not sure
> about performance though.
>
> Adam


From what I understand exactly what you suggest is done by that
ColorConvertOp. Those Ops are actually pretty cool, you put a BufferedImage
in, tell the Op what to do and a new BufferedImage comes out. I just
havened figured out, how to initialize them right. But I'm sure I'll find
it out.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resizing a transparent image adds gray background Oleg Ogurok ASP .Net 0 01-27-2005 05:18 AM
Place transparent image in crystal report =?Utf-8?B?TGluYQ==?= ASP .Net 0 09-22-2004 09:55 AM
Making Background of an image control transparent to view designed Gif =?Utf-8?B?Um9iZXJ0?= ASP .Net 0 04-06-2004 12:21 PM
java -verbose doesn't show "loaded from" for classes loaded from custom jars in the classpath Udo Corban Java 0 01-23-2004 09:32 AM
Re: how to programatically give assembly loaded from network the same trust as those loaded from local host? Marcelo Birnbach [MS] ASP .Net 0 06-27-2003 11:51 PM



Advertisments