在實際項目中,咱們常常會遇處處理各類各樣的圖片問題。 好比:圖片的旋轉、縮放、圖片格式轉換、獲取圖片類型、驗證圖片大小、寫入圖片 等。 這裏咱們使用java.awt.Graphics2D來實現經常使用圖像處理的功能,造成咱們的圖像處理工具類。java
1 package com.zhangsx.util.image; 2 import java.util.Iterator; 3 import java.awt.Graphics2D; 4 import java.awt.RenderingHints; 5 import java.awt.image.BufferedImage; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import java.io.ByteArrayInputStream; 9 import java.io.ByteArrayOutputStream; 10 import javax.imageio.ImageIO; 11 import javax.imageio.ImageReader; 12 import javax.imageio.stream.ImageInputStream; 13 /** 14 * 圖像處理工具類。 15 * 16 * @version 1.00 2010-1-15 17 * @since 1.5 18 * @author ZhangShixi 19 */ 20 public class ImageUtil { 21 /** 22 * 旋轉圖像。 23 * @param bufferedImage 圖像。 24 * @param degree 旋轉角度。 25 * @return 旋轉後的圖像。 26 */ 27 public static BufferedImage rotateImage( 28 final BufferedImage bufferedImage, final int degree) { 29 int width = bufferedImage.getWidth(); 30 int height = bufferedImage.getHeight(); 31 int type = bufferedImage.getColorModel().getTransparency(); 32 BufferedImage image = new BufferedImage(width, height, type); 33 Graphics2D graphics2D = image.createGraphics(); 34 graphics2D.setRenderingHint( 35 RenderingHints.KEY_INTERPOLATION, 36 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 37 graphics2D.rotate(Math.toRadians(degree), width / 2, height / 2); 38 graphics2D.drawImage(bufferedImage, 0, 0, null); 39 try { 40 return image; 41 } finally { 42 if (graphics2D != null) { 43 graphics2D.dispose(); 44 } 45 } 46 } 47 /** 48 * 將圖像按照指定的比例縮放。 49 * 好比須要將圖像放大20%,那麼調用時scale參數的值就爲20;若是是縮小,則scale值爲-20。 50 * @param bufferedImage 圖像。 51 * @param scale 縮放比例。 52 * @return 縮放後的圖像。 53 */ 54 public static BufferedImage resizeImageScale( 55 final BufferedImage bufferedImage, final int scale) { 56 if (scale == 0) { 57 return bufferedImage; 58 } 59 int width = bufferedImage.getWidth(); 60 int height = bufferedImage.getHeight(); 61 int newWidth = 0; 62 int newHeight = 0; 63 double nowScale = (double) Math.abs(scale) / 100; 64 if (scale > 0) { 65 newWidth = (int) (width * (1 + nowScale)); 66 newHeight = (int) (height * (1 + nowScale)); 67 } else if (scale < 0) { 68 newWidth = (int) (width * (1 - nowScale)); 69 newHeight = (int) (height * (1 - nowScale)); 70 } 71 return resizeImage(bufferedImage, newWidth, newHeight); 72 } 73 /** 74 * 將圖像縮放到指定的寬高大小。 75 * @param bufferedImage 圖像。 76 * @param width 新的寬度。 77 * @param height 新的高度。 78 * @return 縮放後的圖像。 79 */ 80 public static BufferedImage resizeImage( 81 final BufferedImage bufferedImage, 82 final int width, final int height) { 83 int type = bufferedImage.getColorModel().getTransparency(); 84 BufferedImage image = new BufferedImage(width, height, type); 85 Graphics2D graphics2D = image.createGraphics(); 86 graphics2D.setRenderingHint( 87 RenderingHints.KEY_INTERPOLATION, 88 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 89 graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0, 90 bufferedImage.getWidth(), bufferedImage.getHeight(), null); 91 try { 92 return image; 93 } finally { 94 if (graphics2D != null) { 95 graphics2D.dispose(); 96 } 97 } 98 } 99 /** 100 * 將圖像水平翻轉。 101 * @param bufferedImage 圖像。 102 * @return 翻轉後的圖像。 103 */ 104 public static BufferedImage flipImage( 105 final BufferedImage bufferedImage) { 106 int width = bufferedImage.getWidth(); 107 int height = bufferedImage.getHeight(); 108 int type = bufferedImage.getColorModel().getTransparency(); 109 BufferedImage image = new BufferedImage(width, height, type); 110 Graphics2D graphics2D = image.createGraphics(); 111 graphics2D.drawImage(bufferedImage, 0, 0, width, height, 112 width, 0, 0, height, null); 113 try { 114 return image; 115 } finally { 116 if (graphics2D != null) { 117 graphics2D.dispose(); 118 } 119 } 120 } 121 /** 122 * 獲取圖片的類型。若是是 gif、jpg、png、bmp 之外的類型則返回null。 123 * @param imageBytes 圖片字節數組。 124 * @return 圖片類型。 125 * @throws java.io.IOException IO異常。 126 */ 127 public static String getImageType(final byte[] imageBytes) 128 throws IOException { 129 ByteArrayInputStream input = new ByteArrayInputStream(imageBytes); 130 ImageInputStream imageInput = ImageIO.createImageInputStream(input); 131 Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInput); 132 String type = null; 133 if (iterator.hasNext()) { 134 ImageReader reader = iterator.next(); 135 type = reader.getFormatName().toUpperCase(); 136 } 137 try { 138 return type; 139 } finally { 140 if (imageInput != null) { 141 imageInput.close(); 142 } 143 } 144 } 145 /** 146 * 驗證圖片大小是否超出指定的尺寸。未超出指定大小返回true,超出指定大小則返回false。 147 * @param imageBytes 圖片字節數組。 148 * @param width 圖片寬度。 149 * @param height 圖片高度。 150 * @return 驗證結果。未超出指定大小返回true,超出指定大小則返回false。 151 * @throws java.io.IOException IO異常。 152 */ 153 public static boolean checkImageSize( 154 final byte[] imageBytes, final int width, final int height) 155 throws IOException { 156 BufferedImage image = byteToImage(imageBytes); 157 int sourceWidth = image.getWidth(); 158 int sourceHeight = image.getHeight(); 159 if (sourceWidth > width || sourceHeight > height) { 160 return false; 161 } else { 162 return true; 163 } 164 } 165 /** 166 * 將圖像字節數組轉化爲BufferedImage圖像實例。 167 * @param imageBytes 圖像字節數組。 168 * @return BufferedImage圖像實例。 169 * @throws java.io.IOException IO異常。 170 */ 171 public static BufferedImage byteToImage( 172 final byte[] imageBytes) throws IOException { 173 ByteArrayInputStream input = new ByteArrayInputStream(imageBytes); 174 BufferedImage image = ImageIO.read(input); 175 try { 176 return image; 177 } finally { 178 if (input != null) { 179 input.close(); 180 } 181 } 182 } 183 /** 184 * 將BufferedImage持有的圖像轉化爲指定圖像格式的字節數組。 185 * @param bufferedImage 圖像。 186 * @param formatName 圖像格式名稱。 187 * @return 指定圖像格式的字節數組。 188 * @throws java.io.IOException IO異常。 189 */ 190 public static byte[] imageToByte( 191 final BufferedImage bufferedImage, final String formatName) 192 throws IOException { 193 ByteArrayOutputStream output = new ByteArrayOutputStream(); 194 ImageIO.write(bufferedImage, formatName, output); 195 try { 196 return output.toByteArray(); 197 } finally { 198 if (output != null) { 199 output.close(); 200 } 201 } 202 } 203 /** 204 * 將圖像以指定的格式進行輸出,調用者需自行關閉輸出流。 205 * @param bufferedImage 圖像。 206 * @param output 輸出流。 207 * @param formatName 圖片格式名稱。 208 * @throws java.io.IOException IO異常。 209 */ 210 public static void writeImage(final BufferedImage bufferedImage, 211 final OutputStream output, final String formatName) 212 throws IOException { 213 ImageIO.write(bufferedImage, formatName, output); 214 } 215 /** 216 * 將圖像以指定的格式進行輸出,調用者需自行關閉輸出流。 217 * @param imageBytes 圖像的byte數組。 218 * @param output 輸出流。 219 * @param formatName 圖片格式名稱。 220 * @throws java.io.IOException IO異常。 221 */ 222 public static void writeImage(final byte[] imageBytes, 223 final OutputStream output, final String formatName) 224 throws IOException { 225 BufferedImage image = byteToImage(imageBytes); 226 ImageIO.write(image, formatName, output); 227 } 228 }