package com.img;
/**
* ImageCompress 提供用户将大图片按比例压缩为小图片,支持JPG、PNG,随便改改就能自己用了,绝对的高清
* Please refer to: <BR>
* <P>
* @author feng_francis <1151461406@qq.com>
* @see java.awt.image.BufferedImage
**/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageCompress extends Frame {
private static final long serialVersionUID = 48L;
//原图路径
private static final String OLD_FILE_PATH = "D:/web/titzc/static/upload/images/";
//缩略图保存路径
private static final String THUMB_SAVE_PATH = "D:/web/titzc/static/upload/images/thumb/";
//缩略图文件名后缀
private static final String FILE_EXT = "_thumb";
//缩略图url
private static final String THUM_URL = "http://www.titzc.com/upload/images/thumb/";
//缩略图宽度
private static final int THUMB_WIDTH = 300;
//缩略图高度
private static final int THUMB_HEIGHT = 300;
private static final String version = "ImageCompress v1.0";
public ImageCompress(String fileName) {
super(version);
file = fileName;
createUI();
loadImage(fileName);
setVisible(true);
}
/**
* 压缩图片
* @param oldImagePath 老的图片路径
*/
public static void ImageScale(String oldImagePath) {
try {
String saveThumbPath = THUMB_SAVE_PATH + currenDateFileName()
+ "/" + getToFileName(oldImagePath);
File file = new File(oldImagePath);
Image image = javax.imageio.ImageIO.read(file);
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
if(imageWidth < 300 || imageHeight < 300){//图片不大就不压缩了
File outFile = new File(saveThumbPath);
copyFile(file, outFile);
} else {
doScale(imageWidth, imageHeight, saveThumbPath, image);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
/**
* 压缩图片
* @param path
* @param fileName
* @param toFileName
*/
public static void ImageScale(String path, String fileName,
String toFileName) {
FileOutputStream out = null;
try {
Image image = javax.imageio.ImageIO.read(new File(path + fileName));
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
float scale = getRatio(imageWidth, imageHeight, THUMB_WIDTH, THUMB_HEIGHT);
imageWidth = (int) (scale * imageWidth);
imageHeight = (int) (scale * imageHeight);
image = image.getScaledInstance(imageWidth, imageHeight,
Image.SCALE_AREA_AVERAGING);
// Make a BufferedImage from the Image.
BufferedImage mBufferedImage = new BufferedImage(imageWidth,
imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();
// Map readeringHint = new HashMap();
// readeringHint.put(RenderingHints.KEY_ALPHA_INTERPOLATION,
// RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
// readeringHint.put(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
// readeringHint.put(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
// readeringHint.put(RenderingHints.KEY_DITHERING,
// RenderingHints.VALUE_DITHER_ENABLE);
// readeringHint.put(RenderingHints.KEY_INTERPOLATION,
// RenderingHints.VALUE_INTERPOLATION_BILINEAR);//VALUE_INTERPOLATION_BICUBIC
// readeringHint.put(RenderingHints.KEY_RENDERING,
// RenderingHints.VALUE_RENDER_QUALITY);
// g.setRenderingHints(readeringHint);
g2.drawImage(image, 0, 0, imageWidth, imageHeight, Color.white,
null);
g2.dispose();
float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
-0.125f, -0.125f, -0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, kernelData2);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
mBufferedImage = cOp.filter(mBufferedImage, null);
out = new FileOutputStream(THUMB_SAVE_PATH + toFileName);
// JPEGEncodeParam param =
// encoder.getDefaultJPEGEncodeParam(bufferedImage);
// param.setQuality(0.9f, true);
// encoder.setJPEGEncodeParam(param);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(mBufferedImage);
} catch (FileNotFoundException fnf) {
} catch (IOException ioe) {
} finally {
try {
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 压缩方法
* @param imageWidth
* @param imageHeight
* @param saveThumbPath
* @param image
*/
private static void doScale(int imageWidth, int imageHeight, String saveThumbPath, Image image) {
FileOutputStream out = null;
try{
float scale = getRatio(imageWidth, imageHeight, THUMB_WIDTH, THUMB_HEIGHT);
imageWidth = (int) (scale * imageWidth);
imageHeight = (int) (scale * imageHeight);
image = image.getScaledInstance(imageWidth, imageHeight,
Image.SCALE_AREA_AVERAGING);
BufferedImage mBufferedImage = new BufferedImage(imageWidth,
imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();
g2.drawImage(image, 0, 0, imageWidth, imageHeight, Color.white,
null);
g2.dispose();
float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
-0.125f, -0.125f, -0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, kernelData2);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
mBufferedImage = cOp.filter(mBufferedImage, null);
mkdirFile(THUMB_SAVE_PATH + currenDateFileName());
out = new FileOutputStream(saveThumbPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(mBufferedImage);
out.close();
out.flush();
} catch(Exception e){
try {
out.close();
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
/**
* 获得thumb的url
* @param oldImagePath
* @return
*/
public static String getToThumbUrl(String oldImagePath) {
return THUM_URL + currenDateFileName() + "/" + getToFileName(oldImagePath);
}
/**
* 新文件名称_thumb
* @param oldUrl
* @return
*/
private static String getToFileName(String oldImagePath) {
return getFileName(oldImagePath) + FILE_EXT + "." + getFileExt(oldImagePath).toLowerCase();
}
/**
* 根据imgUrl获得老文件路径: ../20150326/52811427359322671.gif
* @param imgUrl
* @return
*/
private static String getImgPath(String imgUrl) {
return OLD_FILE_PATH + imgUrl.split("images")[1].split("\\/")[1] + "/" + imgUrl.split("images")[1].split("\\/")[2];
}
/**
* 缩放比例
* @param width
* @param height
* @param maxWidth
* @param maxHeight
* @return 缩放比例
*/
private static float getRatio(int width, int height, int maxWidth,
int maxHeight) {
float Ratio = 1.0f;
float widthRa