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

}

UCloud云服务器香港临时补货,(Intel)CN2 GIA优化线路,上车绝佳时机

至今为止介绍了很多UCLOUD云服务器的促销活动,UCLOUD业者以前看不到我们的个人用户,即使有促销活动,续费也很少。现在新用户的折扣力很大,包括旧用户在内也有一部分折扣。结果,我们的用户是他们的生存动力。没有共享他们的信息的理由是比较受欢迎的香港云服务器CN2GIA线路产品缺货。这不是刚才看到邮件注意和刘先生的通知,而是补充UCLOUD香港云服务器、INTELCPU配置的服务器。如果我们需要他...

丽萨主机:美国CN2 GIA精品网/KVM/9折,美国原生IP,最低27元/月

丽萨主机怎么样?丽萨主机,团队于2017年成立。成立之初主要做的是 CDN 和域名等相关业务。最近开辟新领域,新增了独立服务器出租、VPS 等业务,为了保证业务质量从一开始就选择了中美之间的 CN2 GIA 国际精品网络,三网回程 CN2 GIA,电信去程 CN2 GIA + BGP 直连智能路由,联通移动去程直连,原生IP。适合对网络要求较高的用户,同时价格也比较亲民。点击进入:丽萨主机官方网站...

享有云:美国BGP云服务器低至20元/月起,首月打折;香港2核2G2M仅50元/月起

享有云怎么样?享有云是一家新的国内云服务器商家,目前提供国内、香港及海外地区的云服务器,拥有多线路如:BGP线路、CN2线路、高防等云服务器,并且提供稳定、安全、弹性、高性能的云端计算服务,实时满足您的多样性业务需求。目前,美国bgp云服务器,5M带宽,低至20元/月起,270元/年起,首月打折;香港2核2G2M仅50元/月起,450元/年起!点击进入:享有云官方网站地址享有云优惠活动:一、美国B...

上传工具为你推荐
有趣的广告有趣的广告主页改不了浏览器主页改不了伪静态如何设置伪静态规则数码资源网安卓有没有可以离线刷题的软件?中小企业信息化什么是中小企业信息化途径开机滚动条如何关闭开机滚动条?怎么点亮qq空间图标怎么点亮QQ空间的图标怎么升级ios6苹果6怎么升级最新系统电子商务网站模板电子商务网站策划书小米手柄小米蓝牙游戏手柄怎么连接游戏
免费国内空间 韩国虚拟主机 tk域名注册 已备案未注册域名 raksmart realvnc 一点优惠网 e蜗牛 中国智能物流骨干网 全站静态化 免空 最好的免费空间 cdn联盟 adroit 搜索引擎提交入口 如何安装服务器系统 银盘服务是什么 江苏双线服务器 1元域名 工信部网站备案查询 更多