Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

ImageIo watermarking jpeg with png

Thread view: 
the.trav@gmail.com - 03 Aug 2006 07:22 GMT
I've got a little app that loads a jpeg from an input stream, stamps it
with a png it loaded earlier, then writes the modified jpeg to an
output stream.

It's all working and rosy, however I'm not sure it's quite as efficient
as it could be.

The basic technique I've got is to use ImageIO.read(inputStream) to get
a BufferedImage of each of the original files,

Then I create a new BufferedImage that supports alpha transparancy,
getGraphics then drawImage to draw the original, then drawImage again
to draw the watermark on top of the orginal.

Then I create a final BufferedImage that doesn't support any
transparancy and draw the stamped bufferedImage to it.

Finally I write the final bufferedImage to the output stream using
ImageIO.write(image, out);

It strikes me that I'm creating one more BufferedImage than is
necessary here,
however if I try to write the second last buffered image (the one with
alpha) to the outstream it comes out faded and with different colour
information.
If I make that buffered image without alpha to start the colour
information is correct, but the watermark is 100% opaque (binary
transparancy).

I haven't been able to find a way to convert the colour model in a
buffered image yet, and I would have thought that ImageIO could convert
it a lot better than it is doing.

I also looked into imageTranscoders, however my jre tells me that there
are no elements in the Iterator ImageIO.getImageTranscoders(pngReader,
jpegWriter);

I have also had great difficulty finding documentation on the subject
as most of the tutorials google brings back are task specific and
relate to rendering to the screen for applets and games and so forth.
Knute Johnson - 03 Aug 2006 17:27 GMT
> I've got a little app that loads a jpeg from an input stream, stamps it
> with a png it loaded earlier, then writes the modified jpeg to an
[quoted text clipped - 36 lines]
> as most of the tutorials google brings back are task specific and
> relate to rendering to the screen for applets and games and so forth.

There is an issue with colors in some PNG image files.  You'll have to
search around to find it but I do remember a lot of discussions on the
subject.  As to making it more efficient, read both images into the same
type of BufferedImage with transparency and draw the PNG image onto the
other.  Save the the primary image any you are done.  I would be curious
to see your PNG image if you want to send it.

Signature

Knute Johnson
email s/nospam/knute/

dsjoblom@abo.fi - 03 Aug 2006 21:11 GMT
> I've got a little app that loads a jpeg from an input stream, stamps it
> with a png it loaded earlier, then writes the modified jpeg to an
[quoted text clipped - 21 lines]
> alpha) to the outstream it comes out faded and with different colour
> information.

This happens because most jpeg libraries don't support jpegs with an
alpha channel. ImageIO is one of the few libraries capable of correctly
reading/writing jpegs with alpha.

> If I make that buffered image without alpha to start the colour
> information is correct, but the watermark is 100% opaque (binary
> transparancy).

I'm having a little trouble parsing this sentence, but I assume you
mean you draw the watermark directly on top of the original (and the
original does not have an alpha channel). If so, I can't reproduce
this. Can you post a link to the png you are using?

Basically, the only steps you need to do are:

1. Read image (don't add an alpha component to it)
2. Draw watermark over image
3. Write image

You shouldn't need any extra copies at all. Try this program and see if
it works:

public class WaterMarkTest
{
    /**
    * Test driver
    * @param args
    */
    public static void main(String[] args)
    {
        if (args.length != 3)
        {
            System.out.println("Usage: program-name jpeg-in png-in jpeg-out");
        }

        try
        {
            BufferedImage out = ImageIO.read(new File(args[0]));
            BufferedImage waterMark = ImageIO.read(new File(args[1]));

            Graphics2D g = out.createGraphics();
            g.drawRenderedImage(waterMark, new AffineTransform());

            ImageIO.write(out, "jpg", new File(args[2]));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.