Java图片处理,压缩,旋转,Base64转码
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: 旋转图片到正常的方向.
*
* @author qiyongkang
* @since JDK 1.6
*/
public static void rotateImage(File file,double angle) {
//计算方向
int orientation =getImageOrientation(file);
// angle = 90d;
if (orientation >ORIENTATION_ONE) {
//进行图片处理
switch (orientation) {
case 3:
//需要旋转180度
angle =180d;
break;
case 6:
//需要旋转270度
angle =270d;
break;
case 8:
//需要旋转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();
}
}
/**
* 将网络图片进行Base64位编码
*
* @param imgUrl
* 图片的url路径,如http://.....xx.jpg
* @return
*/
public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行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();
}
// 对字节数组Base64编码
BASE64Encoder encoder =new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
}
/**
* 将本地图片进行Base64位编码
*
* @param imgUrl
* 图片的url路径,如http://.....xx.jpg
* @return
*/
public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行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();
}
// 对字节数组Base64编码
BASE64Encoder encoder =new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
}
/**
* 将Base64位编码的图片进行解码,并保存到指定目录
*
* @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();
}
}
}