上一篇是固定了坐标位置,而这种方式的限制性是很大的,当所上传的图片像素不固定时,很可能所定位的签字位置已经无法找到,所以有了2.0的改进版,即:根据像素大小动态寻找所要印的目标图片位置。
注意:在过程中遇见一个问题,(手机竖着拍一张图)所上传的图片有低概率宽是大于高的,这种情况下图片是会横过来,所以在这种情况需要把签字图片也横过来(即旋转-90度)。
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @Author zhuqing
* @Date 2021/9/10 15:09
*/
public class PicUtil {
/**
* 给图片添加水印图片、可设置水印图片旋转角度
*
* @param iconPaths 水印图片路径
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
* @param degree 水印图片旋转角度
* @param markAlpha
*/
public static void markImageByIcon(List<String> iconPaths, String srcImgPath,
String targerPath, Integer degree, float markAlpha) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
int srcWidth = srcImg.getWidth(null);
int srcHeight = srcImg.getHeight(null);
// 1、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 2、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
if (srcWidth > srcHeight){
degree = -90;
}
// 3、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
// 6、水印图片的位置
int size = iconPaths.size();
int x;
int y;
String watermarkPos = "bottomLeft";
for (String s : iconPaths) {
ImageIcon imgIcon = new ImageIcon(s);
Image img = imgIcon.getImage();
int watermarkImageWidth = img.getWidth(null);
int watermarkImageHeight = img.getHeight(null);
double dmarkWidth = srcWidth * 0.25;// 设置水印的宽度为图片宽度的0.25倍
double dmarkHeight = dmarkWidth * ((double) watermarkImageHeight / (double) watermarkImageWidth);//强转为double计算宽高比例
int imarkWidth = (int) dmarkWidth;
int imarkHeight = (int) dmarkHeight;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, markAlpha));
//右下
if (size == 1){
x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
}
//左下 右下
if (size == 2){
if (Objects.equals(watermarkPos, "bottomLeft")) {
x = degree == null ? 0 : 0+500;
y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
watermarkPos = "bottomRight";
}else if (Objects.equals(watermarkPos, "bottomRight")) {
x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
y = degree == null ? srcHeight - imarkHeight - 200 : srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
}
}
//左中右
if (size == 3){
if (Objects.equals(watermarkPos, "bottomLeft")) {
x = degree == null ? 0 : 0+500;
y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
watermarkPos = "bottomCenter";
} else if (Objects.equals(watermarkPos, "bottomCenter")) {
x = (srcWidth - imarkWidth) / 2;
y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
watermarkPos = "bottomRight";
} else if (Objects.equals(watermarkPos, "bottomRight")) {
x = degree == null ? srcWidth - imarkWidth : srcWidth - imarkWidth-500;
y = degree == null ? srcHeight - imarkHeight - 200: srcHeight - imarkHeight+200;
g.drawImage(img, x, y, imarkWidth, imarkHeight, null);
}
}
//左右 左右(两行)
if (size == 4){
}
//左边一竖排
if (size == 5){
}
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 7、释放资源
g.dispose();
// 8、生成图片
os = new FileOutputStream(targerPath);
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();
}
}
}
}
在此记录。