Java图像处理工具类剖析.docx
- 文档编号:29366171
- 上传时间:2023-07-22
- 格式:DOCX
- 页数:20
- 大小:19.91KB
Java图像处理工具类剖析.docx
《Java图像处理工具类剖析.docx》由会员分享,可在线阅读,更多相关《Java图像处理工具类剖析.docx(20页珍藏版)》请在冰豆网上搜索。
Java图像处理工具类剖析
Java图像处理工具类
这里给大家分享了一个java常用的图像处理工具类,包含缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等,有需要的小伙伴参考下。
本工具类的功能:
缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
复制代码代码如下:
packagenet.kitbox.util;
importjava.awt.AlphaComposite;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.RenderingHints;
importjava.awt.Toolkit;
importjava.awt.color.ColorSpace;
importjava.awt.image.BufferedImage;
importjava.awt.image.ColorConvertOp;
importjava.awt.image.CropImageFilter;
importjava.awt.image.FilteredImageSource;
importjava.awt.image.ImageFilter;
importjava.awt.image.ImagingOpException;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*author:
lldy
*time:
2012-5-6下午6:
37:
18
*图片处理工具类:
*功能:
缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
*/
publicclassImageUtils{
/**
*相对于图片的位置
*/
privatestaticfinalintPOSITION_UPPERLEFT=0;
privatestaticfinalintPOSITION_UPPERRIGHT=10;
privatestaticfinalintPOSITION_LOWERLEFT=1;
privatestaticfinalintPOSITION_LOWERRIGHT=11;
/**
*几种常见的图片格式
*/
publicstaticStringIMAGE_TYPE_GIF="gif";//图形交换格式
publicstaticStringIMAGE_TYPE_JPG="jpg";//联合照片专家组
publicstaticStringIMAGE_TYPE_JPEG="jpeg";//联合照片专家组
publicstaticStringIMAGE_TYPE_BMP="bmp";//英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
publicstaticStringIMAGE_TYPE_PNG="png";//可移植网络图形
privatestaticImageUtilsinstance;
privateImageUtils(){
instance=this;
}
/**
*获取实例
*@return
*/
publicstaticImageUtilsgetInstance(){
if(instance==null){
instance=newImageUtils();
}
returninstance;
}
publicBufferedImageimage2BufferedImage(Imageimage){
System.out.println(image.getWidth(null));
System.out.println(image.getHeight(null));
BufferedImagebufferedImage=newBufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Graphics2Dg=bufferedImage.createGraphics();
g.drawImage(image,null,null);
g.dispose();
System.out.println(bufferedImage.getWidth());
System.out.println(bufferedImage.getHeight());
returnbufferedImage;
}
/**
*缩放并转换格式后保存
*@paramsrcPath源路径
*@paramdestPath目标路径
*@paramwidth:
目标宽
*@paramheight:
目标高
*@paramformat:
文件格式
*@return
*/
publicstaticbooleanscaleToFile(StringsrcPath,StringdestPath,intwidth,intheight,Stringformat){
booleanflag=false;
try{
Filefile=newFile(srcPath);
FiledestFile=newFile(destPath);
if(!
destFile.getParentFile().exists()){
destFile.getParentFile().mkdir();
}
BufferedImagesrc=ImageIO.read(file);//读入文件
Imageimage=src.getScaledInstance(width,height,Image.SCALE_DEFAULT);
BufferedImagetag=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(image,0,0,null);//绘制缩小后的图
g.dispose();
flag=ImageIO.write(tag,format,newFileOutputStream(destFile));//输出到文件流
}catch(IOExceptione){
e.printStackTrace();
}
returnflag;
}
/**
*缩放Image,此方法返回源图像按百分比缩放后的图像
*@paraminputImage
*@parampercentage百分比允许的输入0 *@return */ publicstaticBufferedImagescaleByPercentage(BufferedImageinputImage,intpercentage){ //允许百分比 if(0>percentage||percentage>10000){ thrownewImagingOpException("Error: : 不合法的参数: percentage->"+percentage+",percentage应该大于0~小于10000"); } //获取原始图像透明度类型 inttype=inputImage.getColorModel().getTransparency(); //获取目标图像大小 intw=inputImage.getWidth()*percentage; inth=inputImage.getHeight()*percentage; //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_ANTIALIAS_ON); //使用高质量压缩 renderingHints.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_RENDER_QUALITY); BufferedImageimg=newBufferedImage(w,h,type); Graphics2Dgraphics2d=img.createGraphics(); graphics2d.setRenderingHints(renderingHints); graphics2d.drawImage(inputImage,0,0,w,h,0,0,inputImage .getWidth(),inputImage.getHeight(),null); graphics2d.dispose(); returnimg; /*此代码将返回Image类型 returninputImage.getScaledInstance(inputImage.getWidth()*percentage, inputImage.getHeight()*percentage,Image.SCALE_SMOOTH); */ } /** *缩放Image,此方法返回源图像按给定最大宽度限制下按比例缩放后的图像 *@paraminputImage *@parammaxWidth: 压缩后允许的最大宽度 *@parammaxHeight: 压缩后允许的最大高度 *@throwsjava.io.IOException *return */ publicstaticBufferedImagescaleByPixelRate(BufferedImageinputImage,intmaxWidth,intmaxHeight)throwsException{ //获取原始图像透明度类型 inttype=inputImage.getColorModel().getTransparency(); intwidth=inputImage.getWidth(); intheight=inputImage.getHeight(); intnewWidth=maxWidth; intnewHeight=maxHeight; //如果指定最大宽度超过比例 if(width*maxHeight newWidth=(int)(newHeight*width/height); } //如果指定最大高度超过比例 if(width*maxHeight>height*maxWidth){ newHeight=(int)(newWidth*height/width); } //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_ANTIALIAS_ON); //使用高质量压缩 renderingHints.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_RENDER_QUALITY); BufferedImageimg=newBufferedImage(newWidth,newHeight,type); Graphics2Dgraphics2d=img.createGraphics(); graphics2d.setRenderingHints(renderingHints); graphics2d.drawImage(inputImage,0,0,newWidth,newHeight,0,0,width,height,null); graphics2d.dispose(); returnimg; } /** *缩放Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像 *@paraminputImage *@parammaxWidth: 压缩后宽度 *@parammaxHeight: 压缩后高度 *@throwsjava.io.IOException *return */ publicstaticBufferedImagescaleByPixel(BufferedImageinputImage,intnewWidth,intnewHeight)throwsException{ //获取原始图像透明度类型 inttype=inputImage.getColorModel().getTransparency(); intwidth=inputImage.getWidth(); intheight=inputImage.getHeight(); //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //使用高质量压缩 renderingHints.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); BufferedImageimg=newBufferedImage(newWidth,newHeight,type); Graphics2Dgraphics2d=img.createGraphics(); graphics2d.setRenderingHints(renderingHints); graphics2d.drawImage(inputImage,0,0,newWidth,newHeight,0,0,width,height,null); graphics2d.dispose(); returnimg; } /** *切割图像,返回指定范围的图像 *@paraminputImage *@paramx起点横坐标 *@paramy起点纵坐标 *@paramwidth切割图片宽度: 如果宽度超出图片,将改为图片自x剩余宽度 *@paramheight切割图片高度: 如果高度超出图片,将改为图片自y剩余高度 *@paramfill指定目标图像大小超出时是否补白,如果true,则表示补白;false表示不补白,此时将重置目标图像大小 *@return */ publicstaticBufferedImagecut(BufferedImageinputImage,intx,inty,intwidth,intheight,booleanfill){ //获取原始图像透明度类型 inttype=inputImage.getColorModel().getTransparency(); intw=inputImage.getWidth(); inth=inputImage.getHeight(); intendx=x+width; intendy=y+height; if(x>w) thrownewImagingOpException("起点横坐标超出源图像范围"); if(y>h) thrownewImagingOpException("起点纵坐标超出源图像范围"); BufferedImageimg; //补白 if(fill){ img=newBufferedImage(width,height,type); //宽度超出限制 if((w-x) width=w-x; endx=w; } //高度超出限制 if((h-y) height=h-y; endy=h; } //不补 }else{ //宽度超出限制 if((w-x) width=w-x; endx=w; } //高度超出限制 if((h-y) height=h-y; endy=h; } img=newBufferedImage(width,height,type); } //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_ANTIALIAS_ON); //使用高质量压缩 renderingHints.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_RENDER_QUALITY); Graphics2Dgraphics2d=img.createGraphics(); graphics2d.setRenderingHints(renderingHints); graphics2d.drawImage(inputImage,0,0,width,height,x,y,endx,endy,null); graphics2d.dispose(); returnimg; } /** *切割图像,返回指定起点位置指定大小图像 *@paraminputImage *@paramstartPoint起点位置: 左上: 0、右上: 10、左下: 1、右下: 11 *@paramwidth切割图片宽度 *@paramheight切割图片高度 *@paramfill指定目标图像大小超出时是否补白,如果true,则表示补白;false表示不补白,此时将重置目标图像大小 *@return */ publicstaticBufferedImagecut(BufferedImageinputImage,intstartPoint,intwidth,intheight,booleanfill){ //获取原始图像透明度类型 inttype=inputImage.getColorModel().getTransparency(); intw=inputImage.getWidth(); inth=inputImage.getHeight(); BufferedImageimg; //补白 if(fill){ img=newBufferedImage(width,height,type); if(width>w) width=w; if(height>h) height=h; //不补 }else{ if(width>w) width=w; if(height>h) height=h; img=newBufferedImage(width,height,type); } //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_ANTIALIAS_ON); //使用高质量压缩 renderingHints.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_RENDER_QUALITY); Graphics2Dgraphics2d=img.createGraphics(); graphics2d.setRenderingHints(renderingHints); switch(startPoint){ //右上 casePOSITION_UPPERRIGHT: graphics2d.drawImage(inputImage,w-width,0,w,height,0,0,width,height,null); break; //左下 casePOSITION_LOWERLEFT: graphics2d.drawImage(inputImage,0,h-height,width,h,0,0,width,height,null); break; //右下 casePOSITION_LOWERRIGHT: graphics2d.drawImage(inputImage,w-width,h-height,w,h,0,0,width,height,null); break; //默认左上 casePOSITION_UPPERLEFT: default: graphics2d.drawImage(inputImage,0,0,width,height,0,0,width,height,null); } graphics2d.dispose(); returnimg; } /** *以指定角度旋转图片: 使用正角度theta进行旋转,可将正x轴上的点转向正y轴。 *@paraminputImage *@paramdegree角度: 以度数为单位 *@return */ publicstaticBufferedImagerotateImage(finalBufferedImageinputImage, finalintdegree){ intw=inputImage.getWidth(); inth=inputImage.getHeight(); inttype=inputImage.getColorModel().getTransparency(); BufferedImageimg=newBufferedImage(w,h,type); Graphics2Dgraphics2d=img.createGraphics(); //开启抗锯齿 RenderingHintsrenderingHints=newRenderingHints(RenderingHints.KEY_INTE
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 图像 处理 工具 剖析