利用JPG图片生成高质量的缩略图

	利用JPG图片生成高质量的缩略图

;;;;//生成等比例高质量缩略图publicclassThumbnailUtil{privatestaticintwidth;privatestaticintheight;privatestaticintscaleWidth;staticdoublesupport=(double);staticdoublePI=(double);staticdouble[]contrib;staticdouble[]normContrib;staticdouble[]tmpContrib;staticintstartContrib,stopContrib;staticintnDots;staticintnHalfDots;publicstaticvoidsaveImage(StringfromFileStr,StringsaveToFileStr,intformatWidth,intformatHeight)throwsException{FilesaveFile=newFile(saveToFileStr);FilefromFile=newFile(fromFileStr);saveImage(fromFile,saveFile,formatWidth,formatHeight);}publicstaticvoidsaveImage(FilefromFile,FilesaveFile,intformatWidth,intformatHeight)throwsException{BufferedImagesrcImage;srcImage=(fromFile);//constructimageintimageWidth=(null);intimageHeight=(null);intchangeToWidth=0;intchangeToHeight=0;if(formatWidth0formatHeight0){changeToWidth=formatWidth;changeToHeight=formatHeight;}else{if(imageWidth0imageHeight0){if(imageWidth/imageHeight=formatWidth/formatHeight){if(imageWidthformatWidth){changeToWidth=formatWidth;changeToHeight=(imageHeight*formatWidth)/imageWidth;}else{changeToWidth=imageWidth;changeToHeight=imageHeight;}}else{if(imageHeightformatHeight){changeToHeight=formatHeight;changeToWidth=(imageWidth*formatHeight)/imageHeight;}else{changeToWidth=imageWidth;changeToHeight=imageHeight;}}}}srcImage=imageZoomOut(srcImage,changeToWidth,changeToHeight);(srcImage,"JPEG",saveFile);}publicstaticBufferedImageimageZoomOut(BufferedImagesrcBufferImage,intw,inth){width=();height=();scaleWidth=w;if(DetermineResultSize(w,h)==1){returnsrcBufferImage;}CalContrib();BufferedImagepbOut=HorizontalFiltering(srcBufferImage,w);BufferedImagepbFinalOut=VerticalFiltering(pbOut,h);returnpbFinalOut;}/***决定图像尺寸*/privatestaticintDetermineResultSize(intw,inth){doublescaleH,scaleV;scaleH=(double)w/(double)width;scaleV=(double)h/(double)height;//需要判断一下scaleH,scaleV,不做放大操作if(scaleH==){return1;}return0;}privatestaticdoubleLanczos(inti,intinWidth,intoutWidth,doubleSupport){doublex;x=(double)i*(double)outWidth/(double)inWidth;(x*PI)/(x*PI)*(x*PI/Support)/(x*PI/Support);}privatestaticvoidCalContrib(){nHalfDots=(int)((double)width*support/(double)scaleWidth);nDots=nHalfDots*2+1;try{contrib=newdouble[nDots];normContrib=newdouble[nDots];tmpContrib=newdouble[nDots];}catch(Exceptione){("initcontrib,normContrib,tmpContrib"+e);}intcenter=nHalfDots;contrib[center]=;doubleweight=;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+;for(i=0;i=center;i++){normContrib[i]=contrib[i]/weight;}for(i=center+1;inDots;i++){normContrib[i]=normContrib[center*2-i];}}//处理边缘privatestaticvoidCalTempContrib(intstart,intstop){doubleweight=0;inti=0;for(i=start;i=stop;i++){weight+=contrib[i];}for(i=start;i=stop;i++){tmpContrib[i]=contrib[i]/weight;}}privatestaticintGetRedValue(intrgbValue){inttemp=rgbValue0x00ff0000;returntemp16;}privatestaticintGetGreenValue(intrgbValue){inttemp=rgbValue0x0000ff00;returntemp}privatestaticintGetBlueValue(intrgbValue){returnrgbValue0x000000ff;}privatestaticintComRGB(intredValue,intgreenValue,intblueValue){return(redValue16)+(greenValue8)+blueValue;}//行水平滤波privatestaticintHorizontalFilter(BufferedImagebufImg,intstartX,intstopX,intstart,intstop,inty,double[]pContrib){doublevalueRed=;doublevalueGreen=;doublevalueBlue=;intvalueRGB=0;inti,j;for(i=startX,j=start;i=stopX;i++,j++){valueRGB=(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;}//图片水平滤波privatestaticBufferedImageHorizontalFiltering(BufferedImagebufImage,intiOutW){intdwInW=();intdwInH=();intvalue=0;BufferedImagepbOut=newBufferedImage(iOutW,dwInH,_INT_RGB);for(intx=0;xiOutW;x++){intstartX;intstart;intX=(int)(((double)x)*((double)dwInW)/((double)iOutW)+);inty=0;startX=X-nHalfDots;if(startX0){startX=0;start=nHalfDots-X;}else{start=0;}intstop;intstopX=X+nHalfDots;if(stopX(dwInW-1)){stopX=dwInW-1;stop=nHalfDots+(dwInW-1-X);}else{stop=nHalfDots*2;}if(start0||stopnDots-1){CalTempContrib(start,stop);for(y=0;ydwInH;y++){value=HorizontalFilter(bufImage,startX,stopX,start,stop,y,tmpContrib);(x,y,value);}}else{for(y=0;ydwInH;y++){value=HorizontalFilter(bufImage,startX,stopX,start,stop,y,normContrib);(x,y,value);}}}returnpbOut;}privatestaticintVerticalFilter(BufferedImagepbInImage,intstartY,intstopY,intstart,intstop,intx,double[]pContrib){doublevalueRed=;doublevalueGreen=;doublevalueBlue=;intvalueRGB=0;inti,j;for(i=startY,j=start;i=stopY;i++,j++){valueRGB=(x,i);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;}privatestaticBufferedImageVerticalFiltering(BufferedImagepbImage,intiOutH){intiW=();intiH=();intvalue=0;BufferedImagepbOut=newBufferedImage(iW,iOutH,_INT_RGB);for(inty=0;yiOutH;y++){intstartY;intstart;intY=(int)(((double)y)*((double)iH)/((double)iOutH)+);startY=Y-nHalfDots;if(startY0){startY=0;start=nHalfDots-Y;}else{start=0;}intstop;intstopY=Y+nHalfDots;if(stopY(int)(iH-1)){stopY=iH-1;stop=nHalfDots+(iH-1-Y);}else{stop=nHalfDots*2;}if(start0||stopnDots-1){CalTempContrib(start,stop);for(intx=0;xiW;x++){value=VerticalFilter(pbImage,startY,stopY,start,stop,x,tmpContrib);(x,y,value);}}else{for(intx=0;xiW;x++){value=VerticalFilter(pbImage,startY,stopY,start,stop,x,normContrib);(x,y,value);}}}returnpbOut;}staticintClip(intx){if(x0)return0;if(x255)return255;returnx;}}postedon2011-07-2609:49阅读(702)所属分类:。