resizemanager.java
来自「MyUploader 是一款使用 http 协议(RFC 1867)用于上传文件」· Java 代码 · 共 180 行
JAVA
180 行
/*
* Copyright 2006-2007 JavaAtWork All rights reserved.
* Use is subject to license terms.
*/
package javaatwork.myuploader.utils;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javaatwork.myuploader.net.HTTPUploadTask;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
/**
* Class who is responsible for creating a scaled instance in a
* temp directory. The temp directory is the System property
* "java.io.tmpdir".
*
* @author Johannes Postma
*/
public class ResizeManager {
private File tempDirectory = null;
private HTTPUploadTask task = null;
/**
* Creates a new Resizer.
*
* @throws Exception If an error occurred.
*/
public ResizeManager(HTTPUploadTask task) throws Exception {
this.task = task;
createTempDirectory();
}
/**
* Creates the temp directory.
*
* @throws Exception If the temp directory cannot be created.
*/
private void createTempDirectory() throws Exception {
File dir = new File(System.getProperty("java.io.tmpdir"));
tempDirectory = new File(dir, "myuploader-" + System.currentTimeMillis());
if (tempDirectory.exists()) {
return;
}
boolean created = tempDirectory.mkdir();
if (created == false) {
throw new Exception ("Temp directory could not be created");
}
}
/**
* Deletes the temp directory.
*/
public void deleteTempDirectory() {
deleteDirectory(tempDirectory);
}
/**
* Deletes a directory with all the files and subdirectories.
*
* @param file The directory.
*/
private void deleteDirectory(File file) {
File [] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
file.delete();
}
/**
* Scales an image.
*
* @param file The image to be scaled.
* @param uploadPath The uploadPath.
* @param size The max size.
* @return The scaled image.
* @throws Exception If an error occurred.
*/
public File getScaleImage(File file, String uploadPath, int size, float compressionQuality) throws Exception {
ImageWriter writer = null;
try {
Image image = (Image) ImageIO.read(file);
int fileSize = (int)file.length();
// divide the fileSize in three parts, this is necessary for a correct progressdialog.
int partOne = fileSize / 3;
int partTwo = partOne;
int partThree = fileSize - partOne - partTwo;
// needed for the progressbar
task.addBytesProcessed(partOne);
int thumbHeight = 0;
int thumbWidth = 0;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (imageWidth > imageHeight){
thumbWidth = size;
thumbHeight = (int)(size / imageRatio);
} else {
thumbHeight = size;
thumbWidth = (int)(size * imageRatio);
}
// check if the new size is not larger than the original one.
if (thumbWidth > imageWidth) {
thumbWidth = imageWidth;
thumbHeight = imageHeight;
}
//ImageIcon ic= new ImageIcon(image.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_AREA_AVERAGING));
// Draw the scaled image
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
//graphics2D.drawImage(ic.getImage(), 0, 0, thumbWidth, thumbHeight, null);
// needed for the progressbar
task.addBytesProcessed(partTwo);
File scaleInstance = new File(tempDirectory, uploadPath);
scaleInstance.mkdirs();
scaleInstance = new File(scaleInstance, file.getName());
writer = (ImageWriter) ImageIO.getImageWritersByFormatName("JPEG").next();
ImageWriteParam param = writer.getDefaultWriteParam();
//set quality
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(compressionQuality);
//write jpeg image
writer.setOutput(ImageIO.createImageOutputStream(scaleInstance));
writer.write(null, new IIOImage(thumbImage, null, null), param);
// needed for the progressbar
task.addBytesProcessed(partThree);
return scaleInstance;
} finally {
if (writer != null) {
writer.dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?