package com.common.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
/**
* @param im 原始图像
* @param resizeTimes 倍数,比如0.5就是缩小一半,0.98等等double类型
* @return 返回处理后的图像
*/
private static BufferedImage zoomImage(BufferedImage im, float resizeTimes) {
/*原始图像的宽度和高度*/
int width = im.getWidth();
int height = im.getHeight();
/*调整后的图片的宽度和高度*/
int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);
int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);
/*新生成结果图片*/
BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
return result;
}
/**
* 把图片写到磁盘上
* @param im
* @param path eg: C://home// 图片写入的文件夹地址
* @param fileName DCM1987.jpg 写入图片的名字
* @return
*/
private static boolean writeNormalQuality(BufferedImage im, String fileFullPath) {
File f = new File(fileFullPath);
String fileType = getExtension(fileFullPath);
if (fileType == null)
return false;
try {
ImageIO.write(im, fileType, f);
im.flush();
return true;
} catch (IOException e) {
return false;
}
}
public boolean writeHighQuality(BufferedImage im, String fileFullPath) {
try {
/*输出到文件流*/
FileOutputStream newimage = new FileOutputStream(fileFullPath+System.currentTimeMillis()+".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
/* 压缩质量 */
jep.setQuality(1f, true);
encoder.encode(im, jep);
/*近JPEG编码*/
newimage.close();
return true;
} catch (Exception e) {
return false;
}
}
/**
* 返回文件的文件后缀名
* @param fileName
* @return
*/
private static String getExtension(String fileName) {
try {
return fileName.split("\\.")[fileName.split("\\.").length - 1];
} catch (Exception e) {
return null;
}
}
public static boolean doThumbnail(String srcImagePaht,String destImagePath,int width)
{
boolean doOk = false;
File srcFile = new File(srcImagePaht);
if(srcFile.exists())
{
try {
BufferedImage im = javax.imageio.ImageIO.read(srcFile);
int oldWidth = im.getWidth();
//如果原图比要求的宽度还小,不进行处理,直接复制
if(oldWidth<width)
{
doOk = writeNormalQuality(im,destImagePath);
}
else
{
float resizeTimes = (float)(width*1.0)/oldWidth;
BufferedImage om = zoomImage(im,resizeTimes);
doOk = writeNormalQuality(om,destImagePath);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return doOk;
}
public static boolean doThumbnail(String srcImagePaht,String destImagePath,float resizeTimes)
{
boolean doOk = false;
File srcFile = new File(srcImagePaht);
if(srcFile.exists())
{
try {
BufferedImage im = javax.imageio.ImageIO.read(srcFile);
BufferedImage om = zoomImage(im,resizeTimes);
doOk = writeNormalQuality(om,destImagePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return doOk;
}
public static void main(String[] args) throws Exception{
String srcImagePaht = "e:\\3156409_141828016911_2.jpg" ;
/*这儿填写你存放要缩小图片的文件夹全地址*/
String destImagePath = "e:\\3156409_141828016911_1.jpg";
/*这儿填写你转化后的图片存放的文件夹*/
ImageUtil.doThumbnail(srcImagePaht, destImagePath, 40);
}
}