在實際項目中,咱們常常會遇處處理各類各樣的圖片問題。javascript
好比:圖片的旋轉、縮放、圖片格式轉換、獲取圖片類型、驗證圖片大小、寫入圖片 等。 這裏咱們使用java.awt.Graphics2D來實現經常使用圖像處理的功能,造成咱們的圖像處理工具類。java
- package com.zhangsx.util.image;
-
- import java.util.Iterator;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import javax.imageio.ImageIO;
- import javax.imageio.ImageReader;
- import javax.imageio.stream.ImageInputStream;
-
- public class ImageUtil {
-
-
- public static BufferedImage rotateImage(
- final BufferedImage bufferedImage, final int degree) {
- int width = bufferedImage.getWidth();
- int height = bufferedImage.getHeight();
- int type = bufferedImage.getColorModel().getTransparency();
-
- BufferedImage image = new BufferedImage(width, height, type);
- Graphics2D graphics2D = image.createGraphics();
- graphics2D.setRenderingHint(
- RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-
- graphics2D.rotate(Math.toRadians(degree), width / 2, height / 2);
- graphics2D.drawImage(bufferedImage, 0, 0, null);
-
- try {
- return image;
- } finally {
- if (graphics2D != null) {
- graphics2D.dispose();
- }
- }
- }
-
-
- public static BufferedImage resizeImageScale(
- final BufferedImage bufferedImage, final int scale) {
- if (scale == 0) {
- return bufferedImage;
- }
-
- int width = bufferedImage.getWidth();
- int height = bufferedImage.getHeight();
-
- int newWidth = 0;
- int newHeight = 0;
-
- double nowScale = (double) Math.abs(scale) / 100;
- if (scale > 0) {
- newWidth = (int) (width * (1 + nowScale));
- newHeight = (int) (height * (1 + nowScale));
- } else if (scale < 0) {
- newWidth = (int) (width * (1 - nowScale));
- newHeight = (int) (height * (1 - nowScale));
- }
-
- return resizeImage(bufferedImage, newWidth, newHeight);
- }
-
-
- public static BufferedImage resizeImage(
- final BufferedImage bufferedImage,
- final int width, final int height) {
- int type = bufferedImage.getColorModel().getTransparency();
- BufferedImage image = new BufferedImage(width, height, type);
-
- Graphics2D graphics2D = image.createGraphics();
- graphics2D.setRenderingHint(
- RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-
- graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0,
- bufferedImage.getWidth(), bufferedImage.getHeight(), null);
-
- try {
- return image;
- } finally {
- if (graphics2D != null) {
- graphics2D.dispose();
- }
- }
- }
-
-
- public static BufferedImage flipImage(
- final BufferedImage bufferedImage) {
- int width = bufferedImage.getWidth();
- int height = bufferedImage.getHeight();
- int type = bufferedImage.getColorModel().getTransparency();
-
- BufferedImage image = new BufferedImage(width, height, type);
- Graphics2D graphics2D = image.createGraphics();
- graphics2D.drawImage(bufferedImage, 0, 0, width, height,
- width, 0, 0, height, null);
-
- try {
- return image;
- } finally {
- if (graphics2D != null) {
- graphics2D.dispose();
- }
- }
- }
-
-
- public static String getImageType(final byte[] imageBytes)
- throws IOException {
- ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
- ImageInputStream imageInput = ImageIO.createImageInputStream(input);
- Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInput);
- String type = null;
- if (iterator.hasNext()) {
- ImageReader reader = iterator.next();
- type = reader.getFormatName().toUpperCase();
- }
-
- try {
- return type;
- } finally {
- if (imageInput != null) {
- imageInput.close();
- }
- }
- }
-
-
- public static boolean checkImageSize(
- final byte[] imageBytes, final int width, final int height)
- throws IOException {
- BufferedImage image = byteToImage(imageBytes);
- int sourceWidth = image.getWidth();
- int sourceHeight = image.getHeight();
- if (sourceWidth > width || sourceHeight > height) {
- return false;
- } else {
- return true;
- }
- }
-
-
- public static BufferedImage byteToImage(
- final byte[] imageBytes) throws IOException {
- ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
- BufferedImage image = ImageIO.read(input);
-
- try {
- return image;
- } finally {
- if (input != null) {
- input.close();
- }
- }
- }
-
-
- public static byte[] imageToByte(
- final BufferedImage bufferedImage, final String formatName)
- throws IOException {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- ImageIO.write(bufferedImage, formatName, output);
-
- try {
- return output.toByteArray();
- } finally {
- if (output != null) {
- output.close();
- }
- }
- }
-
-
- public static void writeImage(final BufferedImage bufferedImage,
- final OutputStream output, final String formatName)
- throws IOException {
- ImageIO.write(bufferedImage, formatName, output);
- }
-
-
- public static void writeImage(final byte[] imageBytes,
- final OutputStream output, final String formatName)
- throws IOException {
- BufferedImage image = byteToImage(imageBytes);
- ImageIO.write(image, formatName, output);
- }
- }
- http://zhangshixi.iteye.com/blog/641733