本文实例为大家分享了java项目实现图片等比缩放的具体代码,供大家参考,具体内容如下
package common;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ImageCompressionTask implements Runnable{
private InputStream is;
private String fileName;
private int width;
private int height;
public ImageCompressionTask(InputStream is,String fileName,int width,int height) {
this.is=is;
this.fileName=fileName;
this.width=width;
this.height=height;
}
public void run() {
// TODO Auto-generated method stub
try{
this.compressPic();
}catch(Exception e){
System.out.println("文件压缩失败"+e);
}
}
private String compressPic() throws Exception{
String path = "E:\\xie\\";//新图片存放路径
String urlPath = path + fileName;
BufferedImage buffImage;
FileOutputStream output=null;
BufferedImage compressPic=null;
try {
String imagetype = "";
if(fileName.lastIndexOf(".") != -1){
imagetype = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
}
imagetype = imagetype.toLowerCase(); //文件后缀名
output=new FileOutputStream(urlPath);
buffImage=ImageIO.read(is);
//图片缩放
compressPic=compressPicMin(buffImage,width,height);
//输出图片
ImageIO.write(compressPic, imagetype, output);
} finally {
if(output!=null){
try{
output.close();
}catch(IOException e){
e.getStackTrace();
}
}
if(is!=null){
is.close();
}
}
return fileName;
}
private BufferedImage compressPicMin(BufferedImage image,
int outputWidth, int outputHeight) {
// TODO Auto-generated method stub
if(image==null){
return null;
}
//如果图片本身的宽和高均小于要压缩到的宽和高,则不压缩直接返回
if(outputWidth>image.getWidth(null)&&outputHeight>image.getHeight(null)){
return image;
}
int newWidth;
int newHeight;
//宽和高等比缩放的率
double rate1=(double)image.getWidth(null)/(double)outputWidth;
double rate2=(double)image.getHeight(null)/(double)outputHeight;
//控制缩放大小
double rate=rate1<rate2 ? rate1:rate2;
newWidth=(int) (image.getWidth(null)/rate);
newHeight=(int) (image.getHeight(null)/rate);
BufferedImage newImage=new BufferedImage(newWidth, newHeight,BufferedImage.TYPE_INT_RGB);
newImage.createGraphics().drawImage(image.getScaledInstance(newWidth, outputHeight, Image.SCALE_SMOOTH), 0, 0, null);
return newImage;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
创建ImageTest写一个main()
package test1;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import common.ImageCompressionTask;
public class ImageTest {
public static void main(String[] args){
String imgName = System.currentTimeMillis() + "_" + ((int) (Math.random() * 900) + 100) + "." + "jpg";
File f=new File("E:\\xie\\xxx.jpg");
try {
InputStream input = new FileInputStream(f);
ImageCompressionTask r=new ImageCompressionTask(input, imgName, 520, 320);
newCachedThreadPool().execute(r);
//newSingleThreadExecutor().execute(r);
//newFixedThreadPool(10).execute(r);
System.out.println("图片上传成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {
return new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。