裁剪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;

}

注册做什么96%可以干啥,常用的7个常用的国内国外域名注册服务商_云服务器可以干什么

日前,国内知名主机服务商阿里云与国外资深服务器面板Plesk强强联合,推出 阿里云域名注册与备案、服务器ECS购买与登录使用 前言云服务器(Elastic  只需要确定cpu内存与带宽基本上就可以了,对于新手用户来说,我们在购买阿里云服务申请服务器与域名许多云服务商的云服务器配置是弹性的 三周学会小程序第三讲:服务 不过这个国外服务器有点慢,可以考虑国内的ngrokcc。 ngrokcc...

HostYun 新上美国CN2 GIA VPS 月15元

HostYun 商家以前是玩具主机商,这两年好像发展还挺迅速的,有点在要做点事情的味道。在前面也有多次介绍到HostYun商家新增的多款机房方案,价格相对还是比较便宜的。到目前为止,我们可以看到商家提供的VPS主机包括KVM和XEN架构,数据中心可选日本、韩国、香港和美国的多个地区机房,电信双程CN2 GIA线路,香港和日本机房,均为国内直连线路。近期,HostYun上线低价版美国CN2 GIA ...

无忧云(25元/月),国内BGP高防云服务器 2核2G5M

无忧云官网无忧云怎么样 无忧云服务器好不好 无忧云值不值得购买 无忧云,无忧云是一家成立于2017年的老牌商家旗下的服务器销售品牌,现由深圳市云上无忧网络科技有限公司运营,是正规持证IDC/ISP/IRCS商家,主要销售国内、中国香港、国外服务器产品,线路有腾讯云国外线路、自营香港CN2线路等,都是中国大陆直连线路,非常适合免北岸建站业务需求和各种负载较高的项目,同时国内服务器也有多个BGP以及高...

上传工具为你推荐
iphone5解锁iphone5密码忘了怎么解锁xv播放器下载求手机可以看xv格式的视频播放器iphone6上市时间苹果6什么时候出?多少钱电子商务网站模板网页制作模板微信电话本怎么用微信电话本在哪里 微信电话本怎么打开声母是什么22个声母是什么中国杀毒软件排行榜中国杀毒软件排行qq签名设置手机QQ签名能设置权限吗马赛克马赛克是什么意思?网易企业邮箱登陆怎么登陆网易企业邮箱?企业邮箱和163邮箱区别于那些地方?
网页空间租用 国外vps 国外免费vps 企业域名备案 com域名抢注 net主机 阿里云邮箱登陆首页 百度云100as wavecom 流媒体服务器 shopex空间 好看的桌面背景图片 搜狗12306抢票助手 空间出租 河南移动邮件系统 七夕促销 股票老左 赞助 免费防火墙 速度云 更多