java实现创建缩略图、伸缩图片比例生成的方法
本文实例讲述了java实现创建缩略图、伸缩图片比例生成的方法。分享给大家供大家参考。具体实现方法如下:
该实例支持将Image的宽度、高度缩放到指定width、height,并保存在指定目录通过目标对象的大小和标准(指定)大小计算出图片缩小的比例,可以设置图片缩放质量,并且可以根据指定的宽高缩放图片。
具体代码如下所示:
packagecom.hoo.util;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjava.net.URL;
importjavax.imageio.ImageIO;
importcom.sun.image.codec.jpeg.ImageFormatException;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGEncodeParam;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*<b>function:</b>缩放图片工具类,创建缩略图、伸缩图片比例
*@authorhoojo
*@createDate2012-2-3上午10:08:47
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@version1.0
*/
publicabstractclassScaleImageUtils{
privatestaticfinalfloatDEFAULT_SCALE_QUALITY=1f;
privatestaticfinalStringDEFAULT_IMAGE_FORMAT=".jpg";//图像文件的格式
privatestaticfinalStringDEFAULT_FILE_PATH="C:/temp-";
/**
*<b>function:</b>设置图片压缩质量枚举类;
*Someguidelines:0.75highquality、0.5 mediumquality、0.25lowquality
*@authorhoojo
*@createDate2012-2-7上午11:31:45
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@projectJQueryMobile
*@version1.0
*/
publicenumImageQuality{
max(1.0f),high(0.75f),medium(0.5f),low(0.25f);
privateFloatquality;
publicFloatgetQuality(){
returnthis.quality;
}
ImageQuality(Floatquality){
this.quality=quality;
}
}
privatestaticImageimage;
/**
*<b>function:</b>通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
*@authorhoojo
*@createDate2012-2-6下午04:41:48
*@paramtargetWidth目标的宽度
*@paramtargetHeight目标的高度
*@paramstandardWidth标准(指定)宽度
*@paramstandardHeight标准(指定)高度
*@return最小的合适比例
*/
publicstaticdoublegetScaling(doubletargetWidth,doubletargetHeight,doublestandardWidth,doublestandardHeight){
doublewidthScaling=0d;
doubleheightScaling=0d;
if(targetWidth>standardWidth){
widthScaling=standardWidth/(targetWidth*1.00d);
}else{
widthScaling=1d;
}
if(targetHeight>standardHeight){
heightScaling=standardHeight/(targetHeight*1.00d);
}else{
heightScaling=1d;
}
returnMath.min(widthScaling,heightScaling);
}
/**
*<b>function:</b>将Image的宽度、高度缩放到指定width、height,并保存在savePath目录
*@authorhoojo
*@createDate2012-2-6下午04:54:35
*@paramwidth缩放的宽度
*@paramheight缩放的高度
*@paramsavePath保存目录
*@paramtargetImage即将缩放的目标图片
*@return图片保存路径、名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,intheight,StringsavePath,ImagetargetImage)throwsImageFormatException,IOException{
width=Math.max(width,1);
height=Math.max(height,1);
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage,0,0,width,height,null);
if(savePath==null||"".equals(savePath)){
savePath=DEFAULT_FILE_PATH+System.currentTimeMillis()+DEFAULT_IMAGE_FORMAT;
}
FileOutputStreamfos=newFileOutputStream(newFile(savePath));
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
image.flush();
fos.flush();
fos.close();
returnsavePath;
}
/**
*<b>function:</b>可以设置图片缩放质量,并且可以根据指定的宽高缩放图片
*@authorhoojo
*@createDate2012-2-7上午11:01:27
*@paramwidth缩放的宽度
*@paramheight缩放的高度
*@paramquality图片压缩质量,最大值是1;使用枚举值:{@linkImageQuality}
* Someguidelines:0.75highquality、0.5 mediumquality、0.25lowquality
*@paramsavePath保存目录
*@paramtargetImage即将缩放的目标图片
*@return图片保存路径、名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,intheight,Floatquality,StringsavePath,ImagetargetImage)throwsImageFormatException,IOException{
width=Math.max(width,1);
height=Math.max(height,1);
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage,0,0,width,height,null);
if(savePath==null||"".equals(savePath)){
savePath=DEFAULT_FILE_PATH+System.currentTimeMillis()+DEFAULT_IMAGE_FORMAT;
}
FileOutputStreamfos=newFileOutputStream(newFile(savePath));
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParamencodeParam=JPEGCodec.getDefaultJPEGEncodeParam(image);
if(quality==null||quality<=0){
quality=DEFAULT_SCALE_QUALITY;
}
/**设置图片压缩质量*/
encodeParam.setQuality(quality,true);
encoder.encode(image,encodeParam);
image.flush();
fos.flush();
fos.close();
returnsavePath;
}
/**
*<b>function:</b>通过指定大小和图片的大小,计算出图片缩小的合适大小
*@authorhoojo
*@createDate2012-2-6下午05:53:10
*@paramwidth指定的宽度
*@paramheight指定的高度
*@paramimage图片文件
*@return返回宽度、高度的int数组
*/
publicstaticint[]getSize(intwidth,intheight,Imageimage){
inttargetWidth=image.getWidth(null);
inttargetHeight=image.getHeight(null);
doublescaling=getScaling(targetWidth,targetHeight,width,height);
longstandardWidth=Math.round(targetWidth*scaling);
longstandardHeight=Math.round(targetHeight*scaling);
returnnewint[]{Integer.parseInt(Long.toString(standardWidth)),Integer.parseInt(String.valueOf(standardHeight))};
}
/**
*<b>function:</b>通过指定的比例和图片对象,返回一个放大或缩小的宽度、高度
*@authorhoojo
*@createDate2012-2-7上午10:27:59
*@paramscale缩放比例
*@paramimage图片对象
*@return返回宽度、高度
*/
publicstaticint[]getSize(floatscale,Imageimage){
inttargetWidth=image.getWidth(null);
inttargetHeight=image.getHeight(null);
longstandardWidth=Math.round(targetWidth*scale);
longstandardHeight=Math.round(targetHeight*scale);
returnnewint[]{Integer.parseInt(Long.toString(standardWidth)),Integer.parseInt(String.valueOf(standardHeight))};
}
publicstaticint[]getSize(intwidth,Imageimage){
inttargetWidth=image.getWidth(null);
inttargetHeight=image.getHeight(null);
longheight=Math.round((targetHeight*width)/(targetWidth*1.00f));
returnnewint[]{width,Integer.parseInt(String.valueOf(height))};
}
publicstaticint[]getSizeByHeight(intheight,Imageimage){
inttargetWidth=image.getWidth(null);
inttargetHeight=image.getHeight(null);
longwidth=Math.round((targetWidth*height)/(targetHeight*1.00f));
returnnewint[]{Integer.parseInt(String.valueOf(width)),height};
}
/**
*
*<b>function:</b>将指定的targetFile图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savePath目录
*@authorhoojo
*@createDate2012-2-6下午04:57:02
*@paramwidth缩小的宽度
*@paramheight缩小的高度
*@paramsavePath保存目录
*@paramtargetImage改变的目标图片
*@return图片保存路径、名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,intheight,StringsavePath,FiletargetFile)throwsImageFormatException,IOException{
image=ImageIO.read(targetFile);
int[]size=getSize(width,height,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*
*<b>function:</b>将指定的targetURL网络图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savePath目录
*@authorhoojo
*@createDate2012-2-6下午04:57:07
*@paramwidth缩小的宽度
*@paramheight缩小的高度
*@paramsavePath保存目录
*@paramtargetImage改变的目标图片
*@return图片保存路径、名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,intheight,StringsavePath,URLtargetURL)throwsImageFormatException,IOException{
image=ImageIO.read(targetURL);
int[]size=getSize(width,height,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>将一个本地的图片文件按照指定的比例进行缩放
*@authorhoojo
*@createDate2012-2-7上午10:29:18
*@paramscale缩放比例
*@paramsavePath保存文件路径、名称
*@paramtargetFile本地图片文件
*@return新的文件名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(floatscale,StringsavePath,FiletargetFile)throwsImageFormatException,IOException{
image=ImageIO.read(targetFile);
int[]size=getSize(scale,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>将一个网络图片文件按照指定的比例进行缩放
*@authorhoojo
*@createDate2012-2-7上午10:30:56
*@paramscale缩放比例
*@paramsavePath保存文件路径、名称
*@paramtargetFile本地图片文件
*@return新的文件名称
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(floatscale,StringsavePath,URLtargetURL)throwsImageFormatException,IOException{
image=ImageIO.read(targetURL);
int[]size=getSize(scale,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>按照固定宽度进行等比缩放本地图片
*@authorhoojo
*@createDate2012-2-7上午10:49:56
*@paramwidth固定宽度
*@paramsavePath保存路径、名称
*@paramtargetFile本地目标文件
*@return返回保存路径
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,StringsavePath,FiletargetFile)throwsImageFormatException,IOException{
image=ImageIO.read(targetFile);
int[]size=getSize(width,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>按照固定宽度进行等比缩放网络图片
*@authorhoojo
*@createDate2012-2-7上午10:50:52
*@paramwidth固定宽度
*@paramsavePath保存路径、名称
*@paramtargetFile本地目标文件
*@return返回保存路径
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresize(intwidth,StringsavePath,URLtargetURL)throwsImageFormatException,IOException{
image=ImageIO.read(targetURL);
int[]size=getSize(width,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*
*<b>function:</b>按照固定高度进行等比缩放本地图片
*@authorhoojo
*@createDate2012-2-7上午10:51:17
*@paramheight固定高度
*@paramsavePath保存路径、名称
*@paramtargetFile本地目标文件
*@return返回保存路径
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresizeByHeight(intheight,StringsavePath,FiletargetFile)throwsImageFormatException,IOException{
image=ImageIO.read(targetFile);
int[]size=getSizeByHeight(height,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>按照固定高度进行等比缩放网络图片
*@authorhoojo
*@createDate2012-2-7上午10:52:23
*@paramheight固定高度
*@paramsavePath保存路径、名称
*@paramtargetFile本地目标文件
*@return返回保存路径
*@throwsImageFormatException
*@throwsIOException
*/
publicstaticStringresizeByHeight(intheight,StringsavePath,URLtargetURL)throwsImageFormatException,IOException{
image=ImageIO.read(targetURL);
int[]size=getSizeByHeight(height,image);
returnresize(size[0],size[1],savePath,image);
}
/**
*<b>function:</b>
*@authorhoojo
*@createDate2012-2-3上午10:08:47
*@paramargs
*@throwsIOException
*@throwsMalformedURLException
*@throwsImageFormatException
*/
publicstaticvoidmain(String[]args)throwsImageFormatException,MalformedURLException,IOException{
System.out.println(ScaleImageUtils.resize(140,140,null,newURL("http://www.open-open.com/lib/images/logo.jpg")));
ScaleImageUtils.resize(100,100,ImageQuality.high.getQuality(),null,ImageIO.read(newURL("http://www.open-open.com/lib/images/logo.jpg")));
}
}
希望本文所述对大家的Java程序设计有所帮助。