您的位置:首页 > 编程语言 > Java开发

图片加文字印章和水印 Java

2017-03-16 22:30 381 查看
package bytext;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
*
*/

/**
* @author LingLee
*
*/
public class MarkLogo {

public static void markByText(String logoText,String srcImagePath,String targetImagePath){
markByText(logoText, srcImagePath, targetImagePath,null);
}

public static void markByText(String logoText,String srcImagePath,String targetImagePath,Integer degree){
InputStream is=null;
OutputStream os=null;

try{
Image srcImage=ImageIO.read(new File(srcImagePath));
BufferedImage buffImg=new BufferedImage(srcImage.getWidth(null),srcImage.getHeight(null),
BufferedImage.TYPE_INT_BGR);
Graphics2D g=buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage.getScaledInstance(srcImage.getWidth(null), srcImage.getHeight(null), Image.SCALE_SMOOTH),
0,0,null);

if(degree!=null){
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth(null)/2, (double) buffImg.getHeight(null)/2);
}

g.setColor(Color.red);
g.setFont(new Font("宋体",Font.BOLD,30));
float alpha=0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
g.drawString(logoText, srcImage.getWidth(null)/4, srcImage.getHeight(null)/2);
g.dispose();//消除Windows图形资源,避免多人使用时出现内存泄漏,及时使用
os=new FileOutputStream(targetImagePath);
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片完成文字印章");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(is!=null) is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
if(os!=null) os.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
String srcImagePath="f:/Music/photo/1.jpg";
String targetImagePath="f:/Music/photo/2.jpg";
String logoText="hello world";
String targetImagePath2="f:/Music/photo/3.jpg";
markByText(logoText, srcImagePath, targetImagePath2);
markByText(logoText, srcImagePath, targetImagePath, -45);
}
}

/**
*
*/
package byIcon;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

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

/**
* @author LingLee
*
*/
public class MarkLogoIcon {
public static void markByIcon(String iconPath,String srcImagePath,String targetPath){
markByIcon(iconPath, srcImagePath, targetPath,null);
}
public static void markByIcon(String iconPath,String srcImagePath,String tagetPath,Integer degree){
OutputStream os=null;
try{
Image srcImage=ImageIO.read(new File(srcImagePath));
BufferedImage buffImg=new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null), BufferedImage.TYPE_INT_BGR);
Graphics2D g=buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage.getScaledInstance(srcImage.getWidth(null), srcImage.getHeight(null), Image.SCALE_SMOO
4000
TH),
0,0,null);

if(degree!=null){
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth(null)/2, (double) buffImg.getHeight(null)/2);
}
ImageIcon imageIcon=new ImageIcon(iconPath);
Image img=imageIcon.getImage();
float alpha=0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
g.drawImage(img, 100, 100, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

g.dispose();

os = new FileOutputStream(tagetPath);

//生成图片
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片水印添加成功。。。");
}catch(Exception e){
e.printStackTrace();
}finally{try{if(null!=os){os.close();}}catch(Exception e){e.printStackTrace();}}
}
public static void main(String[] args){
String srcImagePath="f:/Music/photo/1.jpg";
String targetImagePath="f:/Music/photo/2.jpg";
String logoText="hello world";
String iconPath="f:/Music/photo/4.jpg";
String targetImagePath2="f:/Music/photo/3.jpg";
markByIcon(iconPath, srcImagePath, targetImagePath);
markByIcon(iconPath, srcImagePath, targetImagePath2,-45);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: