图片加文字印章和水印 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);
}
}
相关文章推荐
- java-图片加水印,文字水印
- java实现给图片添加水印和文字
- java图片处理 文字水印 图片水印 缩放 补白
- java程序给指定的图片添加水印文字并完美消除文字锯齿
- java图片文字水印、图片水印、缩放、补白处理
- java实现给图片添加水印和文字
- java图片处理 文字水印 图片水印 缩放 补白
- Java 给图片增加 水印图片 水印文字 缩放图片
- jsp,java加图片水印,文字水印
- java给图片添加水印文字
- java实现给图片添加水印和文字
- 使用Java添加图片水印和文字水印
- java图片处理 文字水印 图片水印 缩放 补白
- Java 给图片增加 水印图片 水印文字
- java给图片加水印和文字
- 使用Java添加图片水印和文字水印
- Java实现在图片上添加水印图片和水印文字
- Java给图片添加水印(图标和文字)
- java缩略图、图片水印图、文字水印图
- JAVA给图片添加水印和文字