java圖片處理(水印 縮放 補白)
阿新 • • 發佈:2019-01-29
1 package com.hmw.picMark; 2 3 import java.awt.AlphaComposite; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics2D; 7 import java.awt.Image; 8 import java.awt.geom.AffineTransform; 9 import java.awt.image.AffineTransformOp; 10 import java.awt.image.BufferedImage; 11 import java.io.File; 12 import java.io.IOException; 13 14 import javax.imageio.ImageIO; 15 16 /** 17 * 圖片工具類, 圖片水印,文字水印,縮放,補白等 18 * @author Carl He 19 */ 20 public final class ImageUtils { 21 /**圖片格式:JPG*/ 22 private static final String PICTRUE_FORMATE_JPG = "jpg"; 23 24 private ImageUtils(){} 25 /** 26 * 新增圖片水印 27 * @param targetImg 目標圖片路徑,如:C://myPictrue//1.jpg 28 * @param waterImg 水印圖片路徑,如:C://myPictrue//logo.png 29 * @param x 水印圖片距離目標圖片左側的偏移量,如果x<0, 則在正中間 30 * @param y 水印圖片距離目標圖片上側的偏移量,如果y<0, 則在正中間 31 * @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) 32 */ 33 public final static void pressImage(String targetImg, String waterImg, int x, int y, float alpha) { 34 try { 35 File file = new File(targetImg); 36 Image image = ImageIO.read(file); 37 int width = image.getWidth(null); 38 int height = image.getHeight(null); 39 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 40 Graphics2D g = bufferedImage.createGraphics(); 41 g.drawImage(image, 0, 0, width, height, null); 42 43 Image waterImage = ImageIO.read(new File(waterImg)); // 水印檔案 44 int width_1 = waterImage.getWidth(null); 45 int height_1 = waterImage.getHeight(null); 46 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); 47 48 int widthDiff = width - width_1; 49 int heightDiff = height - height_1; 50 if(x < 0){ 51 x = widthDiff / 2; 52 }else if(x > widthDiff){ 53 x = widthDiff; 54 } 55 if(y < 0){ 56 y = heightDiff / 2; 57 }else if(y > heightDiff){ 58 y = heightDiff; 59 } 60 g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印檔案結束 61 g.dispose(); 62 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file); 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 } 67 68 /** 69 * 新增文字水印 70 * @param targetImg 目標圖片路徑,如:C://myPictrue//1.jpg 71 * @param pressText 水印文字, 如:中國證券網 72 * @param fontName 字型名稱, 如:宋體 73 * @param fontStyle 字型樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC) 74 * @param fontSize 字型大小,單位為畫素 75 * @param color 字型顏色 76 * @param x 水印文字距離目標圖片左側的偏移量,如果x<0, 則在正中間 77 * @param y 水印文字距離目標圖片上側的偏移量,如果y<0, 則在正中間 78 * @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) 79 */ 80 public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha) { 81 try { 82 File file = new File(targetImg); 83 84 Image image = ImageIO.read(file); 85 int width = image.getWidth(null); 86 int height = image.getHeight(null); 87 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 88 Graphics2D g = bufferedImage.createGraphics(); 89 g.drawImage(image, 0, 0, width, height, null); 90 g.setFont(new Font(fontName, fontStyle, fontSize)); 91 g.setColor(color); 92 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); 93 94 int width_1 = fontSize * getLength(pressText); 95 int height_1 = fontSize; 96 int widthDiff = width - width_1; 97 int heightDiff = height - height_1; 98 if(x < 0){ 99 x = widthDiff / 2; 100 }else if(x > widthDiff){ 101 x = widthDiff; 102 } 103 if(y < 0){ 104 y = heightDiff / 2; 105 }else if(y > heightDiff){ 106 y = heightDiff; 107 } 108 109 g.drawString(pressText, x, y + height_1); 110 g.dispose(); 111 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file); 112 } catch (Exception e) { 113 e.printStackTrace(); 114 } 115 } 116 117 /** 118 * 獲取字元長度,一個漢字作為 1 個字元, 一個英文字母作為 0.5 個字元 119 * @param text 120 * @return 字元長度,如:text="中國",返回 2;text="test",返回 2;text="中國ABC",返回 4. 121 */ 122 public static int getLength(String text) { 123 int textLength = text.length(); 124 int length = textLength; 125 for (int i = 0; i < textLength; i++) { 126 if (String.valueOf(text.charAt(i)).getBytes().length > 1) { 127 length++; 128 } 129 } 130 return (length % 2 == 0) ? length / 2 : length / 2 + 1; 131 } 132 133 /** 134 * 圖片縮放 135 * @param filePath 圖片路徑 136 * @param height 高度 137 * @param width 寬度 138 * @param bb 比例不對時是否需要補白 139 */ 140 public static void resize(String filePath, int height, int width, boolean bb) { 141 try { 142 double ratio = 0; //縮放比例 143 File f = new File(filePath); 144 BufferedImage bi = ImageIO.read(f); 145 Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); 146 //計算比例 147 if ((bi.getHeight() > height) || (bi.getWidth() > width)) { 148 if (bi.getHeight() > bi.getWidth()) { 149 ratio = (new Integer(height)).doubleValue() / bi.getHeight(); 150 } else { 151 ratio = (new Integer(width)).doubleValue() / bi.getWidth(); 152 } 153 AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); 154 itemp = op.filter(bi, null); 155 } 156 if (bb) { 157 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 158 Graphics2D g = image.createGraphics(); 159 g.setColor(Color.white); 160 g.fillRect(0, 0, width, height); 161 if (width == itemp.getWidth(null)) 162 g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); 163 else 164 g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null); 165 g.dispose(); 166 itemp = image; 167 } 168 ImageIO.write((BufferedImage) itemp, "jpg", f); 169 } catch (IOException e) { 170 e.printStackTrace(); 171 } 172 } 173 174 public static void main(String[] args) throws IOException { 175 pressImage("C://pic//jpg", "C://pic//test.gif", 5000, 5000, 0f); 176 pressText("C://pic//jpg", "旺仔之印", "宋體", Font.BOLD|Font.ITALIC, 20, Color.BLACK, 0, 0, 8f); 177 resize("C://pic//4.jpg", 1000, 500, true); 178 } 179 }