图片加水印后,解决ImageIo.write图片质量变模糊

项目中使用到的是Graphics2D画图工具对图片处理,用ImageIO.write输出图片流的,但是发现如果是人像的话,图片质量前后相差太大,最后用ImageWriter方式会好很多。
修改前代码


	/**
	 * 给图片添加文字水印
	 * 
	 * @param pressText 水印文字
	 * @param srcImageFile 源图像地址
	 * @param destImageFile 目标图像地址
	 * @param fontName 字体名称
	 * @param fontStyle 字体样式
	 * @param color 字体颜色
	 * @param fontSize 字体大小
	 * @param x 修正值
	 * @param y 修正值
	 * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
	 */
	public final static void pressText2(String pressText, String srcImageFile, String destImageFile, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha, int wz) {
		try {
			Image src = toBufferedImage(srcImageFile);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, width, height, null);
			g.setColor(color);
			g.setFont(new Font(fontName, fontStyle, fontSize));
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			Map<String, Integer> map = getPoint(width, (getLength(pressText) * fontSize), height, fontSize, wz, x, y);
			// 在指定坐标绘制水印文字
			g.drawString(pressText, map.get("_x"), map.get("_y") + fontSize);
			g.dispose();
			ImageIO.write((BufferedImage) image, "JPG", new File(destImageFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

Graphics2D 这里已经是优化后的写法,图片不会失真,经过ImageIO.write后保存下来的图片还是有点模糊
优化后的代码

/**
	 * 给图片添加文字水印
	 * 
	 * @param pressText 水印文字
	 * @param srcImageFile 源图像地址
	 * @param destImageFile 目标图像地址
	 * @param fontName 字体名称
	 * @param fontStyle 字体样式
	 * @param color 字体颜色
	 * @param fontSize 字体大小
	 * @param x 修正值
	 * @param y 修正值
	 * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
	 */
	public final static void pressText2(String pressText, String srcImageFile, String destImageFile, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha, int wz) {
		try {
			Image src = toBufferedImage(srcImageFile);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, width, height, null);
			g.setColor(color);
			g.setFont(new Font(fontName, fontStyle, fontSize));
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			Map<String, Integer> map = getPoint(width, (getLength(pressText) * fontSize), height, fontSize, wz, x, y);
			// 在指定坐标绘制水印文字
			g.drawString(pressText, map.get("_x"), map.get("_y") + fontSize);
			g.dispose();
			Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
	        if (iter.hasNext()) {		
	           	ImageWriter writer = iter.next();		
	           	ImageWriteParam param = writer.getDefaultWriteParam(); 			
	            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);		
	           	param.setCompressionQuality(1.0f);	//最高质量	
	            File file=new File(destImageFile);
	           	FileImageOutputStream out = new FileImageOutputStream(file);			
	           	writer.setOutput(out);		
	           	// writer.write(bi);				
	           	writer.write(null, new IIOImage((BufferedImage) image, null, null), param);				
	           	out.close();			
	           	writer.dispose();		
	      	}
			//ImageIO.write((BufferedImage) image, "JPG", new File(destImageFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

替换掉ImageIO.write方式,这下保存下来的图片肉眼上不仔细看,根本看不出来模糊了,哈哈。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值