package com.app.wii.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.app.wii.action.WiiNonQrcodeScanAction;
/**
* @Description: (二维码)
* @author:huangcan @date:2018-9-14
*/
public class ZXingCode {
private static Logger logger = LogManager.getLogger(ZXingCode.class.getName());
public static void main(String[] args) throws Exception {
Font font = new Font("微软雅黑", Font.PLAIN, 20); // 水印字体
String srcImgPath = "D:\\tools\\eclipse-jee-mars-1-win32-x86_64\\eclipse\\tempFile\\source.png"; // 源图片地址
String tarImgPath = "D:\\tools\\eclipse-jee-mars-1-win32-x86_64\\eclipse\\tempFile\\qrcode.png"; // 待存储的地址
String waterMarkContent = "港福珠宝(深圳)有限公司罗湖分公司"; // 水印内容
Color color = new Color(250, 0, 0, 128); // 水印图片色彩以及透明度
int rotate = 0;
addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font,rotate);
}
/**
* @param srcImgPath
* 源图片路径
* @param tarImgPath
* 保存的图片路径
* @param waterMarkContent
* 水印内容
* @param markContentColor
* 水印颜色
* @param font
* 水印字体
* @param rotate 倾斜角度
*/
public static void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent,
Color markContentColor, Font font,int rotate) {
try {
// 读取原图片信息
File srcImgFile = new File(srcImgPath);// 得到文件
Image srcImg = null;
try {
srcImg = ImageIO.read(srcImgFile);
logger.error("srcImg"+srcImg);
} catch (Exception e1) {
logger.error("读取原图失败:" + e1);
}
int srcImgWidth = srcImg.getWidth(null);// 获取图片的宽
int srcImgHeight = srcImg.getHeight(null);// 获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
logger.error("bufImg"+bufImg);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(markContentColor); // 根据图片的背景设置水印颜色
g.setFont(font); // 设置字体
g.rotate(Math.toRadians(rotate));//设置水印旋转
// 设置水印的坐标
// int x = (srcImgWidth /100) - (getWatermarkLength(waterMarkContent, g) / 2);
// int y = srcImgHeight/5;//左上角
// int x = (srcImgWidth /2) - (getWatermarkLength(waterMarkContent, g) / 2);
// int y = srcImgHeight/2;//中间
// int x = (srcImgWidth /2) - (getWatermarkLength(waterMarkContent, g) / 2);
// int y = srcImgHeight/15;
int x = (srcImgWidth /2) - (getWatermarkLength(waterMarkContent, g) / 2);
int y = srcImgHeight-3;
try {
g.drawString(waterMarkContent, x, y); // 画出水印
} catch (Exception e1) {
logger.error("drawString error"+e1);
}
g.dispose();
// 输出图片
FileOutputStream outImgStream = null;
try {
outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "png", outImgStream);
logger.error("添加水印完成");
} catch (Exception e) {
logger.error("ImageIO.write"+e);
}
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
logger.error("add 水印 fail"+e);
}
}
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
}
作者:候鸟 出处:http://www.cnblogs.com/swite/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |