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

}

HostYun全场9折,韩国VPS月付13.5元起,日本东京IIJ线路月付22.5元起

HostYun是一家成立于2008年的VPS主机品牌,原主机分享组织(hostshare.cn),商家以提供低端廉价VPS产品而广为人知,是小成本投入学习练手首选,主要提供基于XEN和KVM架构VPS主机,数据中心包括中国香港、日本、德国、韩国和美国的多个地区,大部分机房为国内直连或者CN2等优质线路。本月商家全场9折优惠码仍然有效,以KVM架构产品为例,优惠后韩国VPS月付13.5元起,日本东京...

Virtono:圣何塞VPS七五折月付2.2欧元起,免费双倍内存

Virtono是一家成立于2014年的国外VPS主机商,提供VPS和服务器租用等产品,商家支持PayPal、信用卡、支付宝等国内外付款方式,可选数据中心共7个:罗马尼亚2个,美国3个(圣何塞、达拉斯、迈阿密),英国和德国各1个。目前,商家针对美国圣何塞机房VPS提供75折优惠码,同时,下单后在LET回复订单号还能获得双倍内存的升级。下面以圣何塞为例,分享几款VPS主机配置信息。Cloud VPSC...

ZJI:台湾CN2/香港高主频服务器7折每月595元起,其他全场8折

ZJI原名维翔主机,是原来Wordpress圈知名主机商家,成立于2011年,2018年9月更名为ZJI,提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。ZJI今年全新上架了台湾CN2线路服务器,本月针对香港高主频服务器和台湾CN2服务器提供7折优惠码,其他机房及产品提供8折优惠码,优惠后台湾CN2线路E5服务器月付595元起。台湾一型CPU:Inte...

上传工具为你推荐
雅虎社区雅虎是中国的吗无线路由器限速设置无线路由器能设置限速吗?或者说那个牌子的能。百度抢票浏览器百度手机浏览器怎么抢票 手机百度浏览器抢票方法bluestacksBluestacks安卓模拟器是什么机型的?二叉树遍历怎么正确理解二叉树的遍历微信如何建群微信怎么建群?照片转手绘如何把真人图片用photoshop做成手绘图片商标注册查询官网商标注册查询官方网站?宕机宕机是什么意思?怎么上传音乐怎样可以上传本地音乐到网上?
长春域名注册 免费申请网站域名 krypt 海外服务器 双11抢红包攻略 线路工具 网通代理服务器 e蜗牛 福建天翼加速 anylink 国外免费全能空间 已备案删除域名 免费网页空间 重庆电信服务器托管 智能dns解析 秒杀品 iki 双十二促销 nnt google搜索打不开 更多