java生成缩略图的方法示例
本文实例讲述了java生成缩略图的方法。分享给大家供大家参考,具体如下:
packagecom.util; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; /** *生成压缩图 * */ publicclassImageScale{ privateintwidth; privateintheight; privateintscaleWidth; doublesupport=(double)3.0; doublePI=(double)3.14159265358978; double[]contrib; double[]normContrib; double[]tmpContrib; intstartContrib,stopContrib; intnDots; intnHalfDots; /** *Start:UseLanczosfiltertoreplacetheoriginalalgorithmforimage *scaling.Lanczosimprovesqualityofthescaledimagemodifyby:blade */ publicBufferedImageimageZoomOut(BufferedImagesrcBufferImage,intw,inth){ width=srcBufferImage.getWidth(); height=srcBufferImage.getHeight(); scaleWidth=w; if(DetermineResultSize(w,h)==1){ returnsrcBufferImage; } CalContrib(); BufferedImagepbOut=HorizontalFiltering(srcBufferImage,w); BufferedImagepbFinalOut=VerticalFiltering(pbOut,h); returnpbFinalOut; } /** *决定图像尺寸 */ privateintDetermineResultSize(intw,inth){ doublescaleH,scaleV; //updatebylibra doublewt=w>width?width:w; doubleht=h>height?height:h; scaleH=(double)wt/(double)width; scaleV=(double)ht/(double)height; //需要判断一下scaleH,scaleV,不做放大操作 if(scaleH>=1.0&&scaleV>=1.0){ return1; } return0; }//endofDetermineResultSize() privatedoubleLanczos(inti,intinWidth,intoutWidth,doubleSupport){ doublex; x=(double)i*(double)outWidth/(double)inWidth; returnMath.sin(x*PI)/(x*PI)*Math.sin(x*PI/Support) /(x*PI/Support); }//endofLanczos() // //Assumption:samehorizontalandverticalscalingfactor // privatevoidCalContrib(){ nHalfDots=(int)((double)width*support/(double)scaleWidth); nDots=nHalfDots*2+1; try{ contrib=newdouble[nDots]; normContrib=newdouble[nDots]; tmpContrib=newdouble[nDots]; }catch(Exceptione){ System.out.println("initcontrib,normContrib,tmpContrib"+e); } intcenter=nHalfDots; contrib[center]=1.0; doubleweight=0.0; inti=0; for(i=1;i<=center;i++){ contrib[center+i]=Lanczos(i,width,scaleWidth,support); weight+=contrib[center+i]; } for(i=center-1;i>=0;i--){ contrib[i]=contrib[center*2-i]; } weight=weight*2+1.0; for(i=0;i<=center;i++){ normContrib[i]=contrib[i]/weight; } for(i=center+1;i>16; } privateintGetGreenValue(intrgbValue){ inttemp=rgbValue&0x0000ff00; returntemp>>8; } privateintGetBlueValue(intrgbValue){ returnrgbValue&0x000000ff; } privateintComRGB(intredValue,intgreenValue,intblueValue){ return(redValue<<16)+(greenValue<<8)+blueValue; } //行水平滤波 privateintHorizontalFilter(BufferedImagebufImg,intstartX,intstopX, intstart,intstop,inty,double[]pContrib){ doublevalueRed=0.0; doublevalueGreen=0.0; doublevalueBlue=0.0; intvalueRGB=0; inti,j; for(i=startX,j=start;i<=stopX;i++,j++){ valueRGB=bufImg.getRGB(i,y); valueRed+=GetRedValue(valueRGB)*pContrib[j]; valueGreen+=GetGreenValue(valueRGB)*pContrib[j]; valueBlue+=GetBlueValue(valueRGB)*pContrib[j]; } valueRGB=ComRGB(Clip((int)valueRed),Clip((int)valueGreen), Clip((int)valueBlue)); returnvalueRGB; }//endofHorizontalFilter() //图片水平滤波 privateBufferedImageHorizontalFiltering(BufferedImagebufImage,intiOutW){ intdwInW=bufImage.getWidth(); intdwInH=bufImage.getHeight(); intvalue=0; BufferedImagepbOut=newBufferedImage(iOutW,dwInH, BufferedImage.TYPE_INT_RGB); for(intx=0;x (dwInW-1)){ stopX=dwInW-1; stop=nHalfDots+(dwInW-1-X); }else{ stop=nHalfDots*2; } if(start>0||stop "+Clip((int)valueRed)+"<-"); // //System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-"); //System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->"); } valueRGB=ComRGB(Clip((int)valueRed),Clip((int)valueGreen), Clip((int)valueBlue)); //System.out.println(valueRGB); returnvalueRGB; }//endofVerticalFilter() privateBufferedImageVerticalFiltering(BufferedImagepbImage,intiOutH){ intiW=pbImage.getWidth(); intiH=pbImage.getHeight(); intvalue=0; BufferedImagepbOut=newBufferedImage(iW,iOutH, BufferedImage.TYPE_INT_RGB); for(inty=0;y (int)(iH-1)){ stopY=iH-1; stop=nHalfDots+(iH-1-Y); }else{ stop=nHalfDots*2; } if(start>0||stop 255) return255; returnx; } /** *End:UseLanczosfiltertoreplacetheoriginalalgorithmforimage *scaling.Lanczosimprovesqualityofthescaledimagemodifyby:blade */ publicbooleanscale(Stringsource,Stringtarget,intwidth,intheight){ Filef=newFile(source); try{ BufferedImagebi=ImageIO.read(f); BufferedImageout=null; ImageScalescal=newImageScale(); int_width=bi.getWidth();//add int_height=bi.getHeight();//add int[]_arr=this.getImageWidthAndHeight(_width,_height,width, height);//add //out=scal.imageZoomOut(bi,width,height); out=scal.imageZoomOut(bi,_arr[0],_arr[1]); Filet=newFile(target); ImageIO.write(out,"jpg",t); returntrue; }catch(IOExceptione){ e.printStackTrace(); returnfalse; } } /** *得到放大或者缩小后的比例 * *@paramW *图片原宽 *@paramH *原高 *@paramtarW *转换后的宽 *@paramzoom *放大还是缩小 *@return返回宽和高的数组 */ privatestaticint[]getImageWidthAndHeight(intorgW,intorgH,intavW, intavH){ intwidth=0; intheight=0; if(orgW>0&&orgH>0){ if(orgW/orgH>=avW/avH){ if(orgW>avW){ width=avW; height=(orgH*avW)/orgW; }else{ width=orgW; height=orgH; } System.out.println("++Widht:"+width+"Height"+height); }else{ if(orgH>avH){ height=avH; width=(orgW*avH)/orgH; }else{ width=orgW; height=orgH; } System.out.println("++Widht:"+width+"Height"+height); } } int[]arr=newint[2]; arr[0]=width; arr[1]=height; //longstart=System.currentTimeMillis(); //intwidth=0; //intheight=0; //if((W/tarW)>=(H/tarH)){//宽的缩小比例大于高的 //width=tarW; //height=H*tarW/W; //System.out.println(width+""+height); //}else{ //height=tarH; //width=W*tarH/H; //System.out.println(width+""+height); //} //int[]arr=newint[2]; //arr[0]=width; //arr[1]=height; //longend=System.currentTimeMillis(); //System.out.println("宽高处理:"+(end-start)); returnarr; } publicvoidpicscale(Stringsource,Stringtarget,intw,inth){ Filef=newFile(source); intwidth=0; intheight=0; try{ BufferedImagebi=ImageIO.read(f); int[]arr=getImageWidthAndHeight(bi.getWidth(),bi.getHeight(), w,h); width=arr[0]; height=arr[1]; BufferedImageout=null; ImageScalescal=newImageScale(); out=scal.imageZoomOut(bi,width,height); Filet=newFile(target); ImageIO.write(out,"jpg",t); }catch(IOExceptione){ e.printStackTrace(); } } /** * *调用scale(源文件路径,保存路径,最大宽,最大高) * * */ publicstaticvoidmain(String[]args){ ImageScaleis=newImageScale(); longstart=System.currentTimeMillis(); is.scale("D:/nie.jpg","D:/t6.jpg",250,194); longend=System.currentTimeMillis(); System.out.println("时间:"+(end-start)); } }
PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64
ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img
在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo
在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
更多java相关内容感兴趣的读者可查看本站专题:《Java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》及《Java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。