• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Show semi-transparent image on top of other JPEG

 
Bartender
Posts: 1973
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike London
Bartender
Posts: 1973
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1973
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!
-- Mike
 
Mike London
Bartender
Posts: 1973
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic