java图片处理 文字水印 图片水印 缩放 补白

参考网上的资料 修正了里面的问题 增加了补白的功能 重构了代码

Java代码
  1. import java.awt.AlphaComposite;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.awt.Graphics2D;   
  5. import java.awt.Image;   
  6. import java.awt.geom.AffineTransform;   
  7. import java.awt.image.AffineTransformOp;   
  8. import java.awt.image.BufferedImage;   
  9. import java.io.File;   
  10. import java.io.IOException;   
  11.   
  12. import javax.imageio.ImageIO;   
  13.   
  14. /**  
  15.  * @author Eric Xu  
  16.  *  
  17.  */  
  18. public final class ImageUtils {   
  19.     /**  
  20.      * 图片水印  
  21.      * @param pressImg 水印图片  
  22.      * @param targetImg 目标图片  
  23.      * @param x 修正值 默认在中间  
  24.      * @param y 修正值 默认在中间  
  25.      * @param alpha 透明度  
  26.      */  
  27.     public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {   
  28.         try {   
  29.             File img = new File(targetImg);   
  30.             Image src = ImageIO.read(img);   
  31.             int wideth = src.getWidth(null);   
  32.             int height = src.getHeight(null);   
  33.             BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);   
  34.             Graphics2D g = image.createGraphics();   
  35.             g.drawImage(src, 00, wideth, height, null);   
  36.             //水印文件   
  37.             Image src_biao = ImageIO.read(new File(pressImg));   
  38.             int wideth_biao = src_biao.getWidth(null);   
  39.             int height_biao = src_biao.getHeight(null);   
  40.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));   
  41.             g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);   
  42.             //水印文件结束   
  43.             g.dispose();   
  44.             ImageIO.write((BufferedImage) image, "jpg", img);   
  45.         } catch (Exception e) {   
  46.             e.printStackTrace();   
  47.         }   
  48.     }   
  49.   
  50.     /**  
  51.      * 文字水印  
  52.      * @param pressText 水印文字  
  53.      * @param targetImg 目标图片  
  54.      * @param fontName 字体名称  
  55.      * @param fontStyle 字体样式  
  56.      * @param color 字体颜色  
  57.      * @param fontSize 字体大小  
  58.      * @param x 修正值  
  59.      * @param y 修正值  
  60.      * @param alpha 透明度  
  61.      */  
  62.     public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {   
  63.         try {   
  64.             File img = new File(targetImg);   
  65.             Image src = ImageIO.read(img);   
  66.             int width = src.getWidth(null);   
  67.             int height = src.getHeight(null);   
  68.             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  69.             Graphics2D g = image.createGraphics();   
  70.             g.drawImage(src, 00, width, height, null);   
  71.             g.setColor(color);   
  72.             g.setFont(new Font(fontName, fontStyle, fontSize));   
  73.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));   
  74.             g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);   
  75.             g.dispose();   
  76.             ImageIO.write((BufferedImage) image, "jpg", img);   
  77.         } catch (Exception e) {   
  78.             e.printStackTrace();   
  79.         }   
  80.     }   
  81.   
  82.     /**  
  83.      * 缩放  
  84.      * @param filePath 图片路径  
  85.      * @param height 高度  
  86.      * @param width 宽度  
  87.      * @param bb 比例不对时是否需要补白  
  88.      */  
  89.     public static void resize(String filePath, int height, int width, boolean bb) {   
  90.         try {   
  91.             double ratio = 0.0//缩放比例    
  92.             File f = new File(filePath);   
  93.             BufferedImage bi = ImageIO.read(f);   
  94.             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);   
  95.             //计算比例   
  96.             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {   
  97.                 if (bi.getHeight() > bi.getWidth()) {   
  98.                     ratio = (new Integer(height)).doubleValue() / bi.getHeight();   
  99.                 } else {   
  100.                     ratio = (new Integer(width)).doubleValue() / bi.getWidth();   
  101.                 }   
  102.                 AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);   
  103.                 itemp = op.filter(bi, null);   
  104.             }   
  105.             if (bb) {   
  106.                 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  107.                 Graphics2D g = image.createGraphics();   
  108.                 g.setColor(Color.white);   
  109.                 g.fillRect(00, width, height);   
  110.                 if (width == itemp.getWidth(null))   
  111.                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);   
  112.                 else  
  113.                     g.drawImage(itemp, (width - itemp.getWidth(null)) / 20, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);   
  114.                 g.dispose();   
  115.                 itemp = image;   
  116.             }   
  117.             ImageIO.write((BufferedImage) itemp, "jpg", f);   
  118.         } catch (IOException e) {   
  119.             e.printStackTrace();   
  120.         }   
  121.     }   
  122.   
  123.     public static void main(String[] args) throws IOException {   
  124.         pressImage("G:\\imgtest\\sy.jpg""G:\\imgtest\\test1.jpg"000.5f);   
  125.         pressText("我是文字水印""G:\\imgtest\\test1.jpg""黑体"36, Color.white, 80000.3f);   
  126.         resize("G:\\imgtest\\test1.jpg"500500true);   
  127.     }   
  128.   
  129.     public static int getLength(String text) {   
  130.         int length = 0;   
  131.         for (int i = 0; i < text.length(); i++) {   
  132.             if (new String(text.charAt(i) + "").getBytes().length > 1) {   
  133.                 length += 2;   
  134.             } else {   
  135.                 length += 1;   
  136.             }   
  137.         }   
  138.         return length / 2;   
  139.     }   
  140. }  
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @author Eric Xu
 *
 */
public final class ImageUtils {
	/**
	 * 图片水印
	 * @param pressImg 水印图片
	 * @param targetImg 目标图片
	 * @param x 修正值 默认在中间
	 * @param y 修正值 默认在中间
	 * @param alpha 透明度
	 */
	public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
		try {
			File img = new File(targetImg);
			Image src = ImageIO.read(img);
			int wideth = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, wideth, height, null);
			//水印文件
			Image src_biao = ImageIO.read(new File(pressImg));
			int wideth_biao = src_biao.getWidth(null);
			int height_biao = src_biao.getHeight(null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
			//水印文件结束
			g.dispose();
			ImageIO.write((BufferedImage) image, "jpg", img);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文字水印
	 * @param pressText 水印文字
	 * @param targetImg 目标图片
	 * @param fontName 字体名称
	 * @param fontStyle 字体样式
	 * @param color 字体颜色
	 * @param fontSize 字体大小
	 * @param x 修正值
	 * @param y 修正值
	 * @param alpha 透明度
	 */
	public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
		try {
			File img = new File(targetImg);
			Image src = ImageIO.read(img);
			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));
			g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
			g.dispose();
			ImageIO.write((BufferedImage) image, "jpg", img);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 缩放
	 * @param filePath 图片路径
	 * @param height 高度
	 * @param width 宽度
	 * @param bb 比例不对时是否需要补白
	 */
	public static void resize(String filePath, int height, int width, boolean bb) {
		try {
			double ratio = 0.0; //缩放比例 
			File f = new File(filePath);
			BufferedImage bi = ImageIO.read(f);
			Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
			//计算比例
			if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
				if (bi.getHeight() > bi.getWidth()) {
					ratio = (new Integer(height)).doubleValue() / bi.getHeight();
				} else {
					ratio = (new Integer(width)).doubleValue() / bi.getWidth();
				}
				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
				itemp = op.filter(bi, null);
			}
			if (bb) {
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g = image.createGraphics();
				g.setColor(Color.white);
				g.fillRect(0, 0, width, height);
				if (width == itemp.getWidth(null))
					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
				else
					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
				g.dispose();
				itemp = image;
			}
			ImageIO.write((BufferedImage) itemp, "jpg", f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		pressImage("G:\\imgtest\\sy.jpg", "G:\\imgtest\\test1.jpg", 0, 0, 0.5f);
		pressText("我是文字水印", "G:\\imgtest\\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
		resize("G:\\imgtest\\test1.jpg", 500, 500, true);
	}

	public static int getLength(String text) {
		int length = 0;
		for (int i = 0; i < text.length(); i++) {
			if (new String(text.charAt(i) + "").getBytes().length > 1) {
				length += 2;
			} else {
				length += 1;
			}
		}
		return length / 2;
	}
}



getLength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好 不知道有没有更好地方法~

posted @ 2009-06-24 09:30  小白熊  阅读(342)  评论(0编辑  收藏  举报