views:

212

answers:

1

save an image that is generated by merging an image over another image?

i have an image first and i want to insert some text over this image in a specified position given....that i got coorectly..bt the new task is to place this last generated image over another image template in the location given and save it as a new jpg image in my work directory..

A: 

Here is an example of how to use Image Overlay using java2D. Since you tagged also with [jquery] I'm not sure if you want to do this using jquery or java.

I meant the 2nd Snippet, repaired and runs

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class WaterMark {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://sstatic.net/so/img/logo.png");
        BufferedImage im = ImageIO.read(url);
        URL url2 = new URL("http://sstatic.net/sf/img/logo.png");
        BufferedImage im2 = ImageIO.read(url2);
        Graphics2D g = im.createGraphics();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
        g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight())/2, null);
        g.dispose();

        display(im);
        ImageIO.write(im, "jpeg", new File("sample_output.jpeg"));
    }

    public static void display(BufferedImage image) {
        JFrame f = new JFrame("WaterMark");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JLabel(new ImageIcon(image)));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
stacker
i tried this ..but this is not a solution to my question......this is the code to merge text over an image ..not image over an image..anyway thanks to your answer..
Tijo K Varghese