/**
* 打水印 Imagename_biao是水印文件 ImgName是原图
* @param Imagename_biao
* @param ImgName
*/
public static void imgYin(String Imagename_biao, String ImgName) {
//System.out.println("11111111111111111111111111111111 inin");
FileImageOutputStream out=null;
File _file=null;
BufferedImage image=null;
File _filebiao=null;
try {
_file = new File(ImgName);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null); //宽大于300才打水印
if (wideth > 300) {
image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// g.drawImage(src.getScaledInstance(wideth,
// height,Image.SCALE_SMOOTH), 0, 0,wideth, height, null); // 水印文件
_filebiao = new File(Imagename_biao);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
// 水印文件显示在中央
g.drawImage(src_biao, (int) (0.5 * (wideth - wideth_biao)),
(int) (0.5 * (height - height_biao)), wideth_biao,
height_biao, null);
// g.drawImage(src_biao.getScaledInstance(wideth_biao,
// height_biao,Image.SCALE_SMOOTH), (int)(0.5*(wideth
// -wideth_biao)) , (int)(0.5*(height -height_biao)),
// wideth_biao,
// height_biao, null);
// 水印文件在原图片文件的位置,原图片文件的右下角为wideth-0,height-0
g.dispose();
// FileOutputStream out = new FileOutputStream(ImgName);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// JPEGEncodeParam param =
// encoder.getDefaultJPEGEncodeParam(image);
// param.setQuality(0.9f, false); //不压缩图像fals
// encoder.setJPEGEncodeParam(param);
// encoder.encode(image);
// out.flush();
// out.close(); // ImageIO.write(image, "jpg", new File(ImgName)); Iterator<ImageWriter> iter = ImageIO
.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
ImageWriter writer = iter.next();
ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); //MODE_COPY_FROM_METADATA ,MODE_EXPLICIT
param.setCompressionQuality(0.9f);
//System.out.println("Quality:"+1.0);
out = new FileImageOutputStream(
new File(ImgName));
writer.setOutput(out);
// writer.write(bi);
writer.write(null, new IIOImage(image, null, null), param);
out.close();
writer.dispose(); } } } catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}finally{ if(out!=null){
try
{
out.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}

宝贝网址:

最新文章

  1. .NET Core采用的全新配置系统[6]: 深入了解三种针对文件(JSON、XML与INI)的配置源
  2. nodejs事件模块
  3. 给定时器settimeout、setInterval调用传递参数
  4. String和string的区别
  5. mybatis 关联查询 association
  6. 报这个错 unrecognized selector sent to instance
  7. ejabberd 的框架
  8. MBTiles地图瓦片管理工具
  9. 2015第28周六SVN和Git
  10. dom4j解析xml字符串
  11. SqlCommandBuilder类是如何构建T-Sql语句
  12. 谈谈浏览器http缓存
  13. Android ImgView属性
  14. 项目总结二:人脸识别项目(Face Recognition for the Happy House)
  15. CH2101 可达性统计(算竞进阶习题)
  16. python 之面向对象的三大特性
  17. Optaplanner - 从探究示例中的hello world,初步认识规划引擎的运行步骤。
  18. bootstrap教程,SQL
  19. Go linux 实践3
  20. 尚硅谷面试第一季-15Mysql什么时候建索引

热门文章

  1. Spark 1.6以后的内存管理机制
  2. Unicode 转成中文
  3. ios webview 只能播放带域名的视频连接好奇怪!
  4. store操作
  5. asp.net 4.0+ webform 程序中集成mvc4混合应用
  6. Andriod中textview垂直水平居中及LinearLayout内组件的垂直布局
  7. Linux学习一:安装/配置vi,熟悉gcc/vi
  8. core python applications
  9. 关于C#本质论和CLR via C#中译本,不吐不快
  10. mac上用teamviewer远程windows输入问题