600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Java使用Graphics2D添加文字水印碰见的坑(给透明底图片加半透明水印)

Java使用Graphics2D添加文字水印碰见的坑(给透明底图片加半透明水印)

时间:2018-06-30 13:04:54

相关推荐

Java使用Graphics2D添加文字水印碰见的坑(给透明底图片加半透明水印)

先上代码

public static void addText(String fileName, String text) throws IOException {BufferedImage bufferedImage1 = ImageIO.read(new File(fileName));int w1 = bufferedImage1.getWidth();int h1 = bufferedImage1.getHeight();// 生成新图片BufferedImage destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);Graphics2D graphics2D = destImage.createGraphics();destImage = graphics2D.getDeviceConfiguration().createCompatibleImage(w1, h1, Transparency.TRANSLUCENT);graphics2D = destImage.createGraphics();//添加白底graphics2D.setColor(Color.white);graphics2D.fillRect(0, 0, w1, h1);graphics2D.drawImage(bufferedImage1, 0, 0, w1, h1, null);//写入字体Font font = new Font("宋体", Font.PLAIN,20);int markWidth = font.getSize() * getTextLength(text);// 字体长度int markHeight = font.getSize();// 字体高度graphics2D.setColor(Color.blue);graphics2D.setFont(font);//设置透明度// graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));//设置选择度//graphics2D.rotate(10,50,50);graphics2D.drawString(text, w1-markWidth-10, markHeight+10);//合成图片//graphics2D.drawImage(backImage, w1/2-50, h1-50-16,null);graphics2D.dispose();ImageIO.write(destImage, "png", new File(fileName));}/*** 获取文本长度。汉字为1:1,英文和数字为2:1*/private static int getTextLength(String text) {int length = text.length();for (int i = 0; i < text.length(); i++) {String s = String.valueOf(text.charAt(i));if (s.getBytes().length > 1) {length++;}}length = length % 2 == 0 ? length / 2 : length / 2 + 1;return length;}

碰见的坑主要还是在透明底上

1.创建createGraphics()方法的时候是不带透明底的必须得使用下面这个方法创建一个带有透明底的Graphics2D 对象

destImage = graphics2D.getDeviceConfiguration().createCompatibleImage(w1, h1, Transparency.TRANSLUCENT);graphics2D = destImage.createGraphics();

2.在带有透明底的图片里添加半透明水印的时候,这两个透明色应该是冲突了,一旦使用了,会直接被原图的透明底覆盖为0,这个不注意真的很难受

解决方法

没有想到更好的办法,只能遍历像素点去解决了

public static void addText(String fileName, String text) throws IOException {BufferedImage bufferedImage1 = ImageIO.read(new File(fileName));int w1 = bufferedImage1.getWidth();int h1 = bufferedImage1.getHeight();// 生成新图片BufferedImage destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);Graphics2D graphics2D = destImage.createGraphics();destImage = graphics2D.getDeviceConfiguration().createCompatibleImage(w1, h1, Transparency.TRANSLUCENT);graphics2D = destImage.createGraphics();//添加白底graphics2D.setColor(Color.white);graphics2D.fillRect(0, 0, w1, h1);//graphics2D.drawImage(bufferedImage1, 0, 0, w1, h1, null);//写入字体Font font = new Font("宋体", Font.PLAIN,20);int markWidth = font.getSize() * getTextLength(text);// 字体长度int markHeight = font.getSize();// 字体高度graphics2D.setColor(Color.blue);graphics2D.setFont(font);//设置透明度// graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));//设置选择度//graphics2D.rotate(10,50,50);graphics2D.drawString(text, w1-markWidth-10, markHeight+10);//合成图片//graphics2D.drawImage(backImage, w1/2-50, h1-50-16,null);graphics2D.dispose();//将水印添加到图片中去for (int i = 0; i < w1; i++) {for (int j = 0; j < h1; j++) {int rgb = destImage .getRGB(i, j);if (new Color(rgb).getRGB() != Color.white.getRGB()) {bufferedImage1 .setRGB(i, j, rgb);}}}ImageIO.write(bufferedImage1 , "png", new File(fileName));}

给一个效果图

这个时候设入的透明文字在图片里也是透明的,没有达到应该融合的效果

这里参考了一个透明度叠加算法

float r = (foreground.r * alpha) + (background.r * (1.0 - alpha));

这是红色。然后绿色 g 和蓝色 b 通道进行一样的计算。最终合成图像的透明通道始终设置为 1。

参考链接:/a/65018.html

public static void addText(String fileName, String text) throws IOException {BufferedImage bufferedImage1 = ImageIO.read(new File(fileName));int w1 = bufferedImage1.getWidth();int h1 = bufferedImage1.getHeight();// 生成新图片BufferedImage destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);Graphics2D graphics2D = destImage.createGraphics();destImage = graphics2D.getDeviceConfiguration().createCompatibleImage(w1, h1, Transparency.TRANSLUCENT);graphics2D = destImage.createGraphics();//添加白底graphics2D.setColor(Color.white);graphics2D.fillRect(0, 0, w1, h1);//graphics2D.drawImage(bufferedImage1, 0, 0, w1, h1, null);//写入字体Font font = new Font("宋体", Font.PLAIN,20);int markWidth = font.getSize() * getTextLength(text);// 字体长度int markHeight = font.getSize();// 字体高度graphics2D.setColor(Color.blue);graphics2D.setFont(font);//设置透明度// graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));//设置选择度//graphics2D.rotate(10,50,50);graphics2D.drawString(text, w1-markWidth-10, markHeight+10);//合成图片//graphics2D.drawImage(backImage, w1/2-50, h1-50-16,null);graphics2D.dispose();Color color = new Color(textColor);//写入的颜色Color textColorA = new Color(((float) color.getRed())/255, ((float)color.getGreen())/255, ((float)color.getBlue())/255, alpha);int outRGB = textColorA.getRGB();//将水印添加到图片中去for (int i = 0; i < w1; i++) {for (int j = 0; j < h1; j++) {int rgb = destImage .getRGB(i, j);//如果是文字水印的像素点的话if (new Color(rgb).getRGB() != Color.white.getRGB()) {bufferedImage1 .setRGB(i, j, rgb);int bufImgRGB = destImage.getRGB(i, j);if (bufImgRGB>>24==0) {//如果该像素点为全透明,则使用半透明的像素点bufferedImage1 .setRGB(i, j, outRGB);}else {//将原图像素点合成到半透明文字上去Color backColor = new Color(bufImgRGB);float red = textColorA.getRed()*alpha+backColor.getRed()*(1-alpha);float green = textColorA.getGreen()*alpha+backColor.getGreen()*(1-alpha);float blue = textColorA.getBlue()*alpha+backColor.getBlue()*(1-alpha);Color colorCompose = new Color(red/255, green/255, blue/255, 1f);bufferedImage1 .setRGB(i, j, colorCompose.getRGB());}}}}ImageIO.write(bufferedImage1 , "png", new File(fileName));}

加水印前

加水印后

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。