/**
* 图片加水印,可设置透明度,默认水印图片放在右下角
* @param iconFile 水印文件
* @param srcImgFile 源文件
* @param alpha 透明度 0-1
* @return 生成的图片字节数组
*/
public static byte[] markImageByIcon(File iconFile, File srcImgFile,float alpha) {
byte[] imgBytes = null;
try {
Image srcImg = ImageIO.read(srcImgFile);
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
Image mark_img = ImageIO.read(iconFile);
int mark_width = mark_img.getWidth(null);
int mark_height = mark_img.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(mark_img, width - mark_width, height - mark_height, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(buffImg, "JPG", out);
imgBytes = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return imgBytes;
}
JAVA给图片加水印,可设置透明度
最新推荐文章于 2024-09-06 19:01:55 发布