wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Show semi-transparent image on top of other JPEG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Show semi-transparent image on top of other JPEG" Watch "Show semi-transparent image on top of other JPEG" New topic
Author

Show semi-transparent image on top of other JPEG

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Does anyone have an example showing how to put a semi-transparent image (JPEG) on top of another JPEG image?
The effect would be a type of watermark.
Thanks in adavnce.
-- Mike
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Take a look at the code I posted in this thread. I posted a small watermarking application I made. It uses text that can be partially transparent, but the general idea is the same.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Hi Nathan,
Yes, you posted that code to my earlier message. Your code is excellent! However, because I'm new to graphics programming, I found it difficult to extract what I needed from your cool code.
I bought the "Java 2D API" book, but have found it (***VERY***) slow going.
What I'm trying to do (which may actually be in your code)is the following:
1. Open a JPEG file that has a background image.l
2. Open another JPEG file that has a "watermark" image.
3. Write the watermark image onto the background iamge (using some kind of "transparency setting")
If you could take a quick look at the code I've come up with so far and make some suggestions, I would REALLY appreciate it. <s>
I really appreciate your replies.
-- Mike
package combinetwojpegimages;
import java.io.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class combine
{
public static void main (String args[])
{
File f1 = new File(args[0]);
BufferedImage biOne;
BufferedImage biTwo;
BufferedImage bOutput=null;
Graphics goutPut = null;
// create JPEG image encoder to write to the output file to.
OutputStream outStream = new FileOutputStream("outFile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);

// Define background image which we'll put the foreground image on top of
InputStream backGroundImage = new FileInputStream(new File(args[0]));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(backGroundImage);
BufferedImage imageBack = new BufferedImage(40,30,BufferedImage.
TYPE_INT_RGB);
// Define foreground image -- will be partially transparent.
InputStream foreGroundImage = new FileInputStream(new File(args[1]));
JPEGImageDecoder decoder2 = JPEGCodec.createJPEGDecoder(foreGroundImage);
BufferedImage imageFore = new BufferedImage(40,30,BufferedImage.
TYPE_INT_ARGB);
try
{
// getGraphics() creates an offscreen (only) graphics workarea.
Graphics inputBackground = imageBack.getGraphics();
Graphics inputForeground = imageFore.getGraphics();

// create a temp buffered image workspace
BufferedImage tmp = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB);
Graphics gOut = tmp.createGraphics();
// start of writing imageFore onto imageBack using temp file tmp
// making the imageFore semi-transparent.


// End of writing mageFore onto imageBack using temp file tmp
// making the imageFore semi-transparent.

encoder.encode(tmp);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
// Do output of image below.
try
{
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(encoder);
param.setQuality((float)1.0, true);
encoder.encode(encoder, param);
InStream.close();
outStream.flush();
outStream.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Ok... here's some code that works... it uses ImageIO, because it's easier to use, and is now standard with JDK 1.4... the com.sun JPEG stuff was always "unofficial". I put in lots of comments, so it should be easy to follow what I did. There's probably some better way to do this with BufferedImageOp, but this was just a quick solution...

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Thanks!!
-- Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Nate,
I wanted to post another thank you reply for the code you posted above.
Your code worked great and taught me a lot about graphics, which I'm totally new to.
Thank you, again, very, very much!
-- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Show semi-transparent image on top of other JPEG
 
Similar Threads
how to create a semi transparent Div tag
2D animation/transparency question
How to draw a layer with some annotations over a big picture?
alpha filter of an element
How do I watermark an image?