java项目实现图片等比缩放-mile米乐体育
本文实例为大家分享了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; /** * 初始化参数 * @param is 图片输入流 * @param file 图片 * @param filename 图片名称 * @param width 高 * @param 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; } /** * 图片等比缩放 *@param image 图片输入缓存流 *@param outputwidth 图片压缩到的宽 *@param outputheight 图片压缩到的高 *@return bufferedimage * */ 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
创建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:\\x恰卡编程网ie\\xxx.jpg"); try { inputstream input = new fileinputstream(f); imagecompressiontask r=new imagecompressiontask(input, imgname, 520, 320); /* * 方法一: * thread thread1 = new thread(r); thread1.start(); // 启动线程 */ /* * 方法二:使用threadpoolexecutor创建线程池,并不提倡我们直接使用threadpoolexecutor * */ /* threadpoolexecutor executor = new threadpoolexecutor( 5, //核心池的大小(即线程池中的线程数目大于这个参数时,提交的任务会被放进任务缓存队列) 10, //线程池最大能容忍的线程数 200, //线程存活时间 timeunit.milliseconds, //参数keepalivetime的时间单位 new arrayblockingqueue
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。