裁剪java上传图片,对图片进行等比例缩放,及局部裁剪的工具类代码

上传工具  时间:2021-02-23  阅读:()

Java上传图片对图片进行等比例缩放及局部裁剪的工具类代码import c om.sun.image.c odec.jpeg.JPEGCodec;import c om.sun.image.c odec.jpeg.JPEGEnc odeParam;import c om.sun.image.c odec.jpeg.JPEGImageEnc oder;public class FileUploadUtils {

*裁剪图片

*@param input

*@param basepath

*@param uid

*@param x

*@param y

*@param w idth

*@param height

*@r eturn绝对路径

*@throws IOExc eptionpublic static String cutImg(String input,String basepath,int x,int y,int width,int height) throwsIOException{

S tring path2=bas epath+/+Cons tantUtils.US ERFAC ET EMPPAT H;

String postfix=getPostfix(input);

String dst=path2+/+UUID.randomUUID().toString()+.+postfix;createDir(path2);img Cut(b as ep ath+input,ds t,p o stfix,x,y,w idth,height);return dst;

*裁剪图片

*@param input

*@param src

*@param x

*@param y

*@param w idth

*@param height

*@throws IOExc eptionpublic static void imgCut(String input,String dst,String type,int x,int y,int width,int height)throws IOExc eption

BufferedImage fromimg=ImageIO.read(new File(input));

ImageFilter cropFilter=new CropImageFilter(x,y,width,height);

Image img = Toolkit.getDefaultToolkit().createImage(newFilteredImageSourc e(fromimg.getSourc e(),cropFilter));

BufferedImage tag=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphic s g=tag.getGraphic s();g.draw Imag e(img,0,0,null); //绘制小图g.dis p o s e();

//输出为文件

//dir=d:\\temp\\c ut_image_+i+_+j+.jp g

File f=new File(dst);

I mag eI O.write(tag,typ e, f);public static String uploadImg(File src,String basepath,String filename) throws Exception{

String daypath = DateTools.getYear() + + DateTools.getMonth() + +D ateTo o ls.getMonthWeek();

String temppath = ConstantUtils.BASEUPLOADPATH+ /+Cons tantUt ils.ORI GIN ALIMGPAT H+/+daypath;

String thumbnailpath = ConstantUtils.BASEUPLOADPATH+ /+Cons tantUt ils.T HUM BN AILI MGPAT H+/+dayp ath;

String postfix=getPostfix(filename);

String uuid=UUID.randomUUID().toString();

String dsttempname=uuid+.+postfix;createDir(bas epath+/+temppath);cr eateD ir (b as ep ath+/+thumbnailp ath);

String dstallpath=bas epath+/+temppath+/+dsttempname;

String ds tthumbnailpath=bas epath+/+thumbnailpath+/+ds ttempnam e;copy(src,new File(dstallpath));

//对上传的文件进行等比例裁剪。 按照前段要展现的height w idth

T humb n ail(d s t al lp ath,d s tthumb n ailp ath,350,300,100);

//返回裁剪后的路径r eturn thumbnailp ath+/+ds ttempnam e;

*上传文件

*@param src

*@param dst

*@throws Exc eptionpublic static void copy(File src,File dst) throws Exception{

try{

InputStream in=null;

OutputStream out=null;try{in=new BufferedInputStream(new FileInputStream(src),ConstantUtils.BUFF ER_SIZE);out=new BufferedOutputStream(new FileOutputStream(dst),ConstantUtils.BUFFER_SIZE);byte[]buffer=new byte[ConstantUtils.BUFFER_SIZE];while(in.read(buffer)0) {out.write(buffer);

}final ly{if(nu ll !=in) {in.close();if(nu ll !=o ut) {out.c lo s e();

} catch(Exc eption e) {e.printStackTrac e();throw e;

*得到文件后缀jpg

*@param fileName

*@r eturnpublic static String getPostfix(String fileName){if(fileName.equa ls( ))

r eturnint pos=fileName.lastIndexOf( .if(pos 0) {return fileName.substring(fileName.length() - 3).toLow erCas e();} els e {return fileName.substring(pos+1).toLow erCas e();

*创建目录

*@param filePathpublic static void createDir(String filePath) {

File myFile=new File(filePath);if(!myFile.ex ists()) {if(!myF ile.mkd irs())

Sys tem.out.println(创建目录failels e

System.out.println(创建目录succ es smyF ile=null;

*等比例缩放图片

*@param infile

*@param outfile

*@param w idth

*@param height

*@param quality

*@throws IOExc eption

*@throw s InterruptedExc eptionpublic static void Thumbnail(String infile, String outfile, int width, int height, int quality)throws IOExc eption, InterruptedExc eption{

//save thumbnail imag e to OUTFILE

//System.out.println( infile:+infile);

Buffer edI mag e thumb Imag e=null;

BufferedOutputS tream out=null;

Image image=null;image=Toolkit.getDefaultToo lkit().createImage(infile);

MediaTracker mediaTracker=new MediaTracker(new Container());mediaTrac ker.addImage(image,0);mediaTrac ker.w aitF orID(0);int thumb Width=w idth;int thumbHe ight=height;doub le thumbRatio=(doub le) thumbWidth/ (doub le) thumbHe ight;int image Width=image.get Width(null);int imageHe ight=image.getHe ight(null);double image Rat io=(double) imageWidth/ (double) image Height;if(thumbRatio imageRatio) {thumb He ight=(int) (thumb Width/ im ag eRat io);

} els e {

thumb Width=(int) (thumb He ight * im ag eR atio);thumbImage = new BufferedImage(thumbWidth, thumbHe ight,BufferedImage.TYPE_INT_RGB);

Graphic s2D graphic s2D=thumbImage.createGraphic s();graphic s 2D.s etRenderingHint(Rend ering Hints.KEY_INTERPOLATION,Rendering Hints.VALUE_INTERP O LATION_BILINEAR);graphic s 2 D.draw I mag e(imag e,0,0, thumbWidth, thumbHe ight,null);out=new BufferedOutputStream(new FileOutputStream(outfile));

JPEGImageEnc oder enc oder=JPEGCodec.createJPEGEnc oder(out);

JPEGEnc odeParam param=enc oder.getDefaultJPEGEnc odeParam(thumbImage);quality=Math.max(0,Math.min(qual ity, 100));param.s etQuality((float)quality/ 100.0 f,fals e);enc oder.s etJPEGEnc odeParam(param);enc oder.enc ode(thumbImage);out.c lo s e();thumb I m ag e=null;o ut=nu ll;imag e=null;

}

MineServer:洛杉矶CN2 GIA VPS/512MB内存/20GB NVME/800GB流量/200Mbps/KVM,58元/季

mineserver怎么样?mineserver是一家国人商家,主要提供香港CN2 KVM VPS、香港CMI KVM VPS、日本CN2 KVM VPS、洛杉矶cn2 gia端口转发等服务,之前介绍过几次,最近比较活跃。这家新推出了洛杉矶CN2 GIA VPS,512MB内存/20GB NVME/800GB流量/200Mbps/KVM,58元/季,并且进行了带宽升级,同时IP更改为美国IP。点击...

SpinServers(月89美元) 2*e5-2630L v2,美国独立服务器

SpinServers服务商也不算是老牌的服务商,商家看介绍是是2018年成立的主机品牌,隶属于Majestic Hosting Solutions LLC旗下。商家主要经营独立服务器租用和Hybrid Dedicated服务器等,目前包含的数据中心在美国达拉斯、圣何塞机房,自有硬件和IP资源等,商家还自定义支持用户IP广播到机房。看到SpinServers推出了美国独服的夏季优惠促销活动,最低月...

极光KVM(限时16元),洛杉矶三网CN2,cera机房,香港cn2

极光KVM创立于2018年,主要经营美国洛杉矶CN2机房、CeRaNetworks机房、中国香港CeraNetworks机房、香港CMI机房等产品。其中,洛杉矶提供CN2 GIA、CN2 GT以及常规BGP直连线路接入。从名字也可以看到,VPS产品全部是基于KVM架构的。极光KVM也有明确的更换IP政策,下单时选择“IP保险计划”多支付10块钱,可以在服务周期内免费更换一次IP,当然也可以不选择,...

上传工具为你推荐
快递打印如何快递打印快递单网络明星想知道3大网络歌手是谁?缓冲区溢出教程如何防止高手使用缓冲区溢出?怎么在qq空间里添加背景音乐如何在qq空间中添加背景音乐镜像文件是什么什么叫镜像文件,作用是什么?彩信中心移动的短信中心号码是多少开机滚动条开机滚动条太多怎么办?雅虎天盾有没有用用雅虎天盾的啊?怎么升级ios6苹果iPhone6怎么升级系统安装迅雷看看播放器怎样安装迅雷看看播放器
俄罗斯vps enzu 我爱水煮鱼 老左正传 qq云端 稳定免费空间 t云 免费网页申请 香港亚马逊 中国linux 带宽测试 七牛云存储 cdn服务 聚惠网 塔式服务器 免费服务器 vim 硬防 大硬盘补丁 主机之家 更多