code:
package imageShuiying;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageMarkLogoByIcon {
public static void main(String[] args) {
String srcImgPath = "/home/huoshan/eclipseworkspace/imageShuiying/src/20091026094930-1870243638.jpg";
String iconPath = "/home/huoshan/eclipseworkspace/imageShuiying/src/icon.png";
String targerPath = "/home/huoshan/eclipseworkspace/imageShuiying/src/"+new Date().toString()+".jpg" ;
ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath , 0);
}
public static void markImageByIcon(String iconPath, String srcImgPath,
String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null) ;
}
public static void markImageByIcon(String iconPath, String srcImgPath,
String targerPath, Integer degree) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2, (double) buffImg
.getHeight() / 2);
}
ImageIcon imgIcon = new ImageIcon(iconPath);
Image img = imgIcon.getImage();
float alpha = 0.9f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(img, 300, 300, null);
float thick=3.0f;
g.setStroke(new BasicStroke(thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
g.setColor(Color.black);
g.drawLine(300, 300, 200, 100);
g.drawImage(img,200, 100, null);
g.drawLine(200, 100, 0, 0);
g.drawImage(img, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
