附对比图:
合成后失真严重
代码优化后:
原代码:
参数解释:
1. img为你需要把他合成到A图片上的B图片,以下简称为章
2. srcImagePath为A图片的绝对路径
3. targetPath为合成后的图片输出路径
4. x,y为章需要合成到A图片上的位置坐标,w,h为将章缩小到多少长宽比例
注意这行代码:g.drawImage(img, x, y, w, h, null);
public static void markByIcon(Image img, String srcImagePath, String targetPath, int x, int y,
int w, int h) throws IOException {
OutputStream os = null;
try {
logger.info("图片水印开始添加。。。");
logger.info("图片水印输入路径" + srcImagePath);
logger.info("图片水印输出路径" + targetPath);
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);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
g.drawImage(img, x, y, w, h, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targetPath);
//生成图片
ImageIO.write(buffImg, "JPG", os);
logger.info("图片水印添加成功。。。");
} catch (Exception e) {
logger.error("图片水印添加失败。。。", e);
throw e;
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
优化后代码:
原代码是将章在合成的时候按比例缩小,在缩小过程中失真了,优化后为先将章缩小到想要的比例,然后用缩小后的章进行合成
压缩代码:
/**
* * 将图片按照指定的图片尺寸压缩 * * @param srcImgPath :源图片路径 * @param outImgPath * :输出的压缩图片的路径 * @param new_w
* * :压缩后的图片宽 * @param new_h * :压缩后的图片高
*/
public static BufferedImage compressImage(String srcImgPath, int new_w, int new_h) {
BufferedImage src = inputImage(srcImgPath);
BufferedImage bufferedImage = disposeImage(src, new_w, new_h);
return bufferedImage;
}
/**
* 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h
*/
private synchronized static BufferedImage disposeImage(BufferedImage src, int new_w, int new_h) {
// 得到图片
int old_w = src.getWidth();
// 得到源图宽
int old_h = src.getHeight();
// 得到源图长
BufferedImage newImg = null;
// 判断输入图片的类型
switch (src.getType()) {
case 13:
// png,gifnewImg = new BufferedImage(new_w, new_h,
// BufferedImage.TYPE_4BYTE_ABGR);
break;
default:
newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
break;
}
Graphics2D g = newImg.createGraphics();
// 从原图上取颜色绘制新图
g.drawImage(src, 0, 0, old_w, old_h, null);
g.dispose();
// 根据图片尺寸压缩比得到新图的尺寸
newImg.getGraphics().drawImage(
src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,
null);
// 调用方法输出图片文件
return newImg;
}
/**
* * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param per
*/
private static void OutImage(String outImgPath, BufferedImage newImg) {
// 判断输出的文件夹路径是否存在,不存在则创建
File file = new File(outImgPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}// 输出到文件流
try {
ImageIO.write(newImg, outImgPath.substring(outImgPath
.lastIndexOf(".") + 1), new File(outImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
优化后代码:
先通过compressImage方法将章图片进行比例缩小,然后用缩小后的bufferedImage去进行合成
BufferedImage bufferedImage = ImageUtil.compressImage(signPath, photoConfig.getWidth(), photoConfig.getHeight());
Image signImg = ImageUtil.sign(bufferedImage);
if (signImg == null) {
throw new MyException("图片生成失败,签名异常");
}
ImageUtil.markBySignIcon(signImg, imagePath, targetPath, x, y, photoConfig.getWidth(), photoConfig.getHeight());
此时我将原markByIcon方法进行了修改,修改后代码如下,修改后的方法名为markBySignIcon如上最后一行
public static void markBySignIcon(Image img, String srcImagePath, String targetPath, int x, int y,
int w, int h) throws IOException {
OutputStream os = null;
try {
logger.info("图片水印开始添加。。。");
logger.info("图片水印输入路径" + srcImagePath);
logger.info("图片水印输出路径" + targetPath);
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);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
g.drawImage(img, x, y, img.getWidth(null), img.getHeight(null), null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targetPath);
//生成图片
ImageIO.write(buffImg, "JPG", os);
logger.info("图片水印添加成功。。。");
} catch (Exception e) {
logger.error("图片水印添加失败。。。", e);
throw e;
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
还记得之前标红,要注意的代码么,现在改成如下
g.drawImage(img, x, y, img.getWidth(null), img.getHeight(null), null);
到此,用来合成的小图章在合成后,效果会较原来好很多