package com.zsx.servlets;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import net.coobird.thumbnailator.Thumbnails;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageUtil {
//寬
? ? public static int IMAGE_WIDTH =1;
//高
? ? public static int IMAGE_HEIGHT =2;
//方向
? ? public static int ORIENTATION_ONE =1;//正常
? ? public static int ORIENTATION_THREE =3;//180
? ? public static int ORIENTATION_SIX =6;//順時針90
? ? public static int ORIENTATION_EIGHT =8;//逆時針90,順時針270
/**
*
? ? * getImageSize: 獲取圖片大小.
*
? ? * @author qiyongkang
? ? * @return
? ? * @since JDK 1.6
*/
? ? public static int getImageSize(File file,int flag) {
int size =0;
if (file ==null || !file.exists())
return size;
try {
if (IMAGE_WIDTH == flag) {
size = ImageIO.read(file).getWidth();
}else if (IMAGE_HEIGHT == flag) {
size = ImageIO.read(file).getHeight();
}
}catch (IOException e) {
e.printStackTrace();
}
return size;
}
/**
*
? ? * getImageOrientation: 獲取圖片的方向
*
? ? * @author qiyongkang
? ? * @param file
? ? * @return
? ? * @since JDK 1.6
*/
? ? public static int getImageOrientation(File file) {
int orientation =ORIENTATION_ONE;
try {
Metadata metadata = ImageMetadataReader.readMetadata(file);
Directory dr = metadata.getDirectory(ExifIFD0Directory.class);
if (dr ==null) {
return orientation;
}
orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}catch (Exception e) {
e.printStackTrace();
}
return orientation;
}
/**
*
? ? * rotateImage: 旋轉(zhuǎn)圖片到正常的方向.
*
? ? * @author qiyongkang
? ? * @since JDK 1.6
*/
? ? public static void rotateImage(File file,double angle) {
//計算方向
? ? ? ? int orientation =getImageOrientation(file);
//? ? ? angle = 90d;
? ? ? ? if (orientation >ORIENTATION_ONE) {
//進(jìn)行圖片處理
? ? ? ? ? ? switch (orientation) {
case 3:
//需要旋轉(zhuǎn)180度
? ? ? ? ? ? ? ? ? ? angle =180d;
break;
case 6:
//需要旋轉(zhuǎn)270度
? ? ? ? ? ? ? ? ? ? angle =270d;
break;
case 8:
//需要旋轉(zhuǎn)90度
? ? ? ? ? ? ? ? ? ? angle =90d;
break;
}
}
System.out.println("orientation="+orientation+"angle="+angle);
try {
Thumbnails.of(file).scale(1).rotate(angle).toFile(file);;
}catch (IOException e) {
e.printStackTrace();
}
}
/**
*
? ? * impressImage: 壓縮圖片.
*
? ? * @author qiyongkang
? ? * @param file
? ? * @param width
? ? * @param height
? ? * @since JDK 1.6
*/
? ? public static void impressImage(File file,int width,int height) {
try {
Thumbnails.of(file).size(width, height).toFile(file);
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 將網(wǎng)絡(luò)圖片進(jìn)行Base64位編碼
*
? ? * @param imgUrl
? ? *? ? ? ? ? ? 圖片的url路徑,如http://.....xx.jpg
? ? * @return
? ? */
? ? public static String encodeImgageToBase64(URL imageUrl) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
? ? ? ? ByteArrayOutputStream outputStream =null;
try {
BufferedImage bufferedImage = ImageIO.read(imageUrl);
outputStream =new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg", outputStream);
}catch (MalformedURLException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
// 對字節(jié)數(shù)組Base64編碼
? ? ? ? BASE64Encoder encoder =new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
? ? }
/**
* 將本地圖片進(jìn)行Base64位編碼
*
? ? * @param imgUrl
? ? *? ? ? ? ? ? 圖片的url路徑,如http://.....xx.jpg
? ? * @return
? ? */
? ? public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
? ? ? ? ByteArrayOutputStream outputStream =null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream =new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg", outputStream);
}catch (MalformedURLException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
// 對字節(jié)數(shù)組Base64編碼
? ? ? ? BASE64Encoder encoder =new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
? ? }
/**
* 將Base64位編碼的圖片進(jìn)行解碼,并保存到指定目錄
*
? ? * @param base64
? ? *? ? ? ? ? ? base64編碼的圖片信息
? ? * @return
? ? */
? ? public static void decodeBase64ToImage(String base64, String path,
String imgName) {
BASE64Decoder decoder =new BASE64Decoder();
try {
FileOutputStream write =new FileOutputStream(new File(path
+ imgName));
byte[] decoderBytes = decoder.decodeBuffer(base64);
write.write(decoderBytes);
write.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}