java对图片添加水印(RGB/CMYK)
对普通RGB图片添加水印
直接贴上代码,可以用
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
* @param logoText 要写入的文字
* @param srcImgPath 源图片路径
* @param newImagePath 新图片路径
* @param degree 旋转角度
* @param color 字体颜色
* @param formaName 图片后缀
*/
public static void markImageByText(String logoText, String srcImgPath,String newImagePath,Integer degree,Color color,String formaName) {
InputStream is = null;
OutputStream os = null;
try {
// 1、源图片
java.awt.Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), buffImg.getWidth()/2,buffImg.getHeight() /2);
}
// 5、设置水印文字颜色
g.setColor(color);
// 6、设置水印文字Font
g.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, buffImg.getHeight() /2));
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.15f));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(logoText, buffImg.getWidth()/2 , buffImg.getHeight()/2);
// 9、释放资源
g.dispose();
// 10、生成图片
os = new FileOutputStream(newImagePath);
ImageIO.write(buffImg, formaName, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
对普通CMYK图片添加水印
RGB色彩格式是电脑可以直接识别的,而CMYK格式是不能被识别的(打印机的识别颜色),我找了很多方法,用java原生的方法效果不大好,会有失真和色差的问题,最后用twelvemonkeys解决的
- 加入依赖
<!--
Optional dependency. Needed only if you deploy `ImageIO` plugins as part of a web app.
Make sure you add the `IIOProviderContextListener` to your `web.xml`, see above.
-->
<dependency>
<groupId>com.twelvemonkeys.servlet</groupId>
<artifactId>servlet</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.4.2</version>
</dependency>
- 贴上代码
修改这句就行
try {
buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
}catch (Exception ex){
//CMYK
buffImg=ImageIO.read(input);
}
工程中的跑通代码
public static byte[] markImageByText(String logoText, byte[] photo, Integer degree, Color color, String formaName) {
Logger logger = LoggerFactory.getLogger(WaterMark.class);
InputStream is = null;
OutputStream os = null;
byte[] data =null;
try {
// 1、源图片
InputStream input = new ByteArrayInputStream(photo);
java.awt.Image srcImg = ImageIO.read(input);
BufferedImage buffImg=null;
try {
buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
}catch (Exception ex){
//CMYK
buffImg=ImageIO.read(input);
}
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
int heightImg=((BufferedImage) srcImg).getHeight();
int widthImg=((BufferedImage)srcImg).getWidth();
int squreX=(widthImg-100)/2;
int squreWidth = 100;
int squreHeight = 20;
int textSize = 18;
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
// 画背景
g.setColor(new Color(0,0,0));
g.fillRect(0,0,widthImg,squreHeight);
g.fillRect(0,srcImg.getHeight(null)-squreHeight,widthImg,squreHeight);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), buffImg.getWidth()/2,buffImg.getHeight() /2);
}
// 5、设置水印文字颜色
g.setColor(color);
// 6、设置水印文字Font
Font font = new Font("宋体", java.awt.Font.BOLD, textSize);
g.setFont(font);
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.98f));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
FontMetrics fm = g.getFontMetrics(font);
int textWidth = fm.stringWidth(logoText);
logger.info(textWidth+"textWith------------");
int widthX = (srcImg.getWidth(null) - textWidth) / 2;
logger.info(widthX+"widthX------------");
g.drawString(logoText, widthX , heightImg);
g.drawString(logoText, widthX , textSize);
// 9、释放资源
g.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( buffImg, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
data=imageInByte;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return data;
}