爱编程的欧巴

让我们成长吧~
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

二维码图片添加水印

Posted on 2018-12-19 10:24  爱编程的欧巴  阅读(558)  评论(0编辑  收藏  举报

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());
}
}