自己封装的一个Java版图片工具,具备压缩,伸缩变换,透明处理,格式

自己封装的一个Java版图片工具,具备压缩,伸缩变换,透明处理,格式转换等功能.

网络传输过程中,为什么要对图像和视频流进行压缩第一,压缩的必要性:

  图象和视频通常在计算机中表示后会占用非常大的空间,而出于节省硬盘空间的考虑,往往要进行压缩。同时,传输过程中,美国服务器,为了节省珍贵的带宽资源和节省时间,也迫切要求压缩。压缩之后,传输过程中的误码率也会相应地减少。

第二,压缩的可能性:

  人眼对颜色只有一定的感应能力,当某些颜色十分相近时,香港服务器租用,人是感觉不出差异的(或者很小)。这一点为压缩提供了机会。我们把相近的颜色用一种颜色表示,从而减少了图象的存储空间,实现压缩。同时,通过解压缩,虚拟主机,我们可以根据之前采取的压缩方法(有损压缩、无损压缩等)进行相应的解压缩措施,保证图象的真度恢复。

下面是个人封装的工具,具有用户自定义图片压缩水印(图片水印,文字水印)、图片透明处理、灰白处理、格式获取、格式转换、物理存储、处理中介BufferedImage等模块.

代码全贴,含注释, 个人娱乐之作,不喜勿喷! 菜鸟一枚,求指点.

1 package org.dennisit.org.util; 2 import java.awt.AlphaComposite; 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics2D; 6 import java.awt.Image; 7 import java.awt.color.ColorSpace; 8 import java.awt.image.BufferedImage; 9 import java.awt.image.ColorConvertOp; 10 import java.io.ByteArrayInputStream; 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.IOException; 14 import java.util.Iterator; javax.imageio.ImageIO; 17 import javax.imageio.ImageReader; 18 import javax.imageio.stream.MemoryCacheImageInputStream; com.sun.imageio.plugins.bmp.BMPImageReader; 21 import com.sun.imageio.plugins.gif.GIFImageReader; 22 import com.sun.imageio.plugins.jpeg.JPEGImageReader; 23 import com.sun.imageio.plugins.png.PNGImageReader; * : 1.1 28 * : 苏若年 <a href=”mailto:DennisIT@163.com”>发送邮件</a> 30 *: 1.0创建时间: 2012-12-28上午11:15:03 32 * 33 * @function: 图片工具类 34 * 35 * 36 * PictureUtil {DEFAULT_QUALITY = 0.2125f ;* 46 * 添加图片水印操作(物理存盘,使用默认格式) 47 * imgPath 49 *待处理图片 markPath 51 *水印图片 x 53 *水印位于图片左上角的 x 坐标值 y 55 *水印位于图片左上角的 y 坐标值 alpha 57 *水印透明度 0.1f ~ 1.0f destPath 59 *文件存放路径 Exception 61 * addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String destPath) throws Exception{ 64try { 65BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha); 66ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath)); 67} catch (Exception e) {RuntimeException(“添加图片水印异常”); 69 } 70 }* 75 * 添加图片水印操作(物理存盘,自定义格式) 76 * imgPath 78 *待处理图片 markPath 80 *水印图片 x 82 *水印位于图片左上角的 x 坐标值 y 84 *水印位于图片左上角的 y 坐标值 alpha 86 *水印透明度 0.1f ~ 1.0f format 88 *添加水印后存储的格式 destPath 90 *文件存放路径 Exception 92 * addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String format,String destPath) throws Exception{ 95try { 96BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha); 97ImageIO.write(bufferedImage,format , new File(destPath)); 98} catch (Exception e) {RuntimeException(“添加图片水印异常”);100 }101 }* 106 * 添加图片水印操作,返回BufferedImage对象107 * imgPath109 *待处理图片 markPath111 *水印图片 x113 *水印位于图片左上角的 x 坐标值 y115 *水印位于图片左上角的 y 坐标值 alpha117 *水印透明度 0.1f ~ 1.0f*处理后的图片对象 Exception121 * BufferedImage addWaterMark(String imgPath, String markPath, int x, int y, float alpha) throws Exception{124BufferedImage targetImage = null;125try {Image img = ImageIO.read(new File(imgPath));targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);131Graphics2D g = targetImage.createGraphics();132g.drawImage(img, 0, 0, null);Image markImg = ImageIO.read(new File(markPath));136 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));137g.drawImage(markImg, x, y, null);138 g.dispose();139} catch (Exception e) {RuntimeException(“添加图片水印操作异常”);141 }142return targetImage;143144 }* 150 * 添加文字水印操作(物理存盘,使用默认格式)151 * imgPath153 *待处理图片 text155 *水印文字 font157 *水印字体信息 不写默认值为宋体 color159 *水印字体颜色 x161 *水印位于图片左上角的 x 坐标值 y163 *水印位于图片左上角的 y 坐标值 alpha165 *水印透明度 0.1f ~ 1.0f format167 *添加水印后存储的格式 destPath169 *文件存放路径 ExceptionaddTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String destPath) throws Exception{173try {174BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);175ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));176} catch (Exception e) {RuntimeException(“图片添加文字水印异常”);178 }179 }* 183 * 添加文字水印操作(物理存盘,自定义格式)184 * imgPath186 *待处理图片 text188 *水印文字 font190 *水印字体信息 不写默认值为宋体 color192 *水印字体颜色 x194 *水印位于图片左上角的 x 坐标值 y196 *水印位于图片左上角的 y 坐标值 alpha198 *水印透明度 0.1f ~ 1.0f format200 *添加水印后存储的格式 destPath202 *文件存放路径 ExceptionaddTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String format,String destPath) throws Exception{206try {207BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);208ImageIO.write(bufferedImage, format, new File(destPath));209} catch (Exception e) {RuntimeException(“图片添加文字水印异常”);211 }212 }* 216 * 添加文字水印操作,返回BufferedImage对象217 * imgPath219 *待处理图片 text221 *水印文字 font223 *水印字体信息 不写默认值为宋体 color225 *水印字体颜色 x227 *水印位于图片左上角的 x 坐标值 y229 *水印位于图片左上角的 y 坐标值 alpha231 *水印透明度 0.1f ~ 1.0f*处理后的图片对象 ExceptionBufferedImage addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha) throws Exception{238BufferedImage targetImage = null;239try {240Font Dfont = (font == null) ? new Font(“宋体”, 20, 13) : font;241Image img = ImageIO.read(new File(imgPath));targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);244Graphics2D g = targetImage.createGraphics();245g.drawImage(img, 0, 0, null);246 g.setColor(color);247 g.setFont(Dfont);248 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));249 g.drawString(text, x, y);250 g.dispose();251} catch (Exception e) {RuntimeException(“添加文字水印操作异常”);253 }254return targetImage;255 }* 261 * 262 * 263 * 压缩图片操作(文件物理存盘,使用默认格式)264 * imgPath266 *待处理图片 quality268 *图片质量(0-1之間的float值) width270 *输出图片的宽度 输入负数参数表示用原来图片宽 height272 *输出图片的高度 输入负数参数表示用原来图片高 autoSize274 *是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放 format276 *压缩后存储的格式 destPath278 *文件存放路径279 * ExceptioncompressImage(String imgPath,float quality,int width, int height, boolean autoSize,String destPath)throws Exception{283try {284BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);285ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));286} catch (Exception e) {RuntimeException(“图片压缩异常”);288 }289290 }* 295 * 压缩图片操作(文件物理存盘,可自定义格式)296 * imgPath298 *待处理图片 quality300 *图片质量(0-1之間的float值) width302 *输出图片的宽度 输入负数参数表示用原来图片宽 height304 *输出图片的高度 输入负数参数表示用原来图片高 autoSize306 *是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放 format308 *压缩后存储的格式 destPath310 *文件存放路径311 * ExceptioncompressImage(String imgPath,float quality,int width, int height, boolean autoSize,String format,String destPath)throws Exception{315try {316BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);317ImageIO.write(bufferedImage, format, new File(destPath));318} catch (Exception e) {RuntimeException(“图片压缩异常”);320 }321 }* 326 * 压缩图片操作,返回BufferedImage对象327 * imgPath329 *待处理图片 quality331 *图片质量(0-1之間的float值) width333 *输出图片的宽度 输入负数参数表示用原来图片宽 height335 *输出图片的高度 输入负数参数表示用原来图片高 autoSize337 *是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放*处理后的图片对象 ExceptionBufferedImage compressImage(String imgPath,float quality,int width, int height, boolean autoSize)throws Exception{343BufferedImage targetImage = null;344if(quality<0F||quality>1F){345quality = DEFAULT_QUALITY;346 }347try {348Image img = ImageIO.read(new File(imgPath));newwidth =( width > 0 ) ? width : img.getWidth(null);newheight = ( height > 0 )? height: img.getHeight(null);(autoSize){Widthrate = ((double) img.getWidth(null)) / (double) width + 0.1;357double heightrate = ((double) img.getHeight(null))/ (double) height + 0.1;358double rate = Widthrate > heightrate ? Widthrate : heightrate;359newwidth = (int) (((double) img.getWidth(null)) / rate);360newheight = (int) (((double) img.getHeight(null)) / rate);361 }targetImage = new BufferedImage(newwidth,newheight,BufferedImage.TYPE_INT_RGB);364Graphics2D g = targetImage.createGraphics();365g.drawImage(img, 0, 0, newwidth, newheight, null);g.dispose();368369} catch (Exception e) {RuntimeException(“图片压缩操作异常”);371 }372return targetImage;373 }* 图片黑白化操作(文件物理存盘,使用默认格式)379 * bufferedImage381 *处理的图片对象 destPath383 *目标文件地址 Exception 385 *imageGray(String imgPath, String destPath)throws Exception{388 imageGray(imgPath, imageFormat(imgPath), destPath);389 }* 图片黑白化操作(文件物理存盘,可自定义格式)394 * bufferedImage396 *处理的图片对象 format398 *图片格式 destPath400 *目标文件地址 Exception 402 * imageGray(String imgPath,String format, String destPath)throws Exception{405try {406BufferedImage bufferedImage = ImageIO.read(new File(imgPath));407ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);408ColorConvertOp op = new ColorConvertOp(cs, null); 409bufferedImage = op.filter(bufferedImage, null);410ImageIO.write(bufferedImage, format , new File(destPath));411} catch (Exception e) {RuntimeException(“图片灰白化异常”);413 }414 }* 图片透明化操作(文件物理存盘,使用默认格式)420 * imgPath422 *图片路径 destPath424 *图片存放路径 ExceptionimageLucency(String imgPath,String destPath)throws Exception{428try {429BufferedImage bufferedImage = imageLucency(imgPath);430ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));431} catch (Exception e) {RuntimeException(“图片透明化异常”);433 }434 }* 图片透明化操作(文件物理存盘,可自定义格式)439 * imgPath441 *图片路径 format443 *图片格式 destPath445 *图片存放路径 ExceptionimageLucency(String imgPath,String format,String destPath)throws Exception{449try {450BufferedImage bufferedImage = imageLucency(imgPath);451ImageIO.write(bufferedImage, format, new File(destPath));452} catch (Exception e) {RuntimeException(“图片透明化异常”);454 }455 }* 图片透明化操作返回BufferedImage对象459 * imgPath461 *图片路径*透明化后的图片对象 Exception BufferedImage imageLucency(String imgPath)throws Exception{467BufferedImage targetImage = null;468try {BufferedImage img = ImageIO.read(new FileInputStream(imgPath));alpha = 0;executeRGB(img, alpha);475targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);476Graphics2D g = targetImage.createGraphics();477g.drawImage(img, 0, 0, null);478 g.dispose();479} catch (Exception e) {RuntimeException(“图片透明化执行异常”);481 }482return targetImage;483 }* 执行透明化的核心算法487 * img489 *图片对象 alpha491 *透明度 Exception executeRGB(BufferedImage img, int alpha) throws Exception{495int rgb = 0;//RGB值(int x=img.getMinX();x<img.getWidth();x++){498for(int y=img.getMinY();y<img.getHeight();y++){rgb = img.getRGB(x, y); 501int R =(rgb & 0xff0000 ) >> 16 ; 502int G= (rgb & 0xff00 ) >> 8 ; 503int B= (rgb & 0xff ); 504if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){ 505rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff); 506 img.setRGB(x, y, rgb);507 }508 }509 }510 }* 图片格式转化操作(文件物理存盘)515 * imgPath517 *原始图片存放地址 format519 *待转换的格式 jpeg,gif,png,bmp等 destPath521 *目标文件地址 ExceptionformatConvert(String imgPath, String format, String destPath)throws Exception{525try {526BufferedImage bufferedImage = ImageIO.read(new File(imgPath));527ImageIO.write(bufferedImage, format, new File(destPath));528} catch (IOException e) {RuntimeException(“文件格式转换出错”);530 }531 }* 图片格式转化操作返回BufferedImage对象537 * bufferedImage539 *BufferedImage图片转换对象 format541 *待转换的格式 jpeg,gif,png,bmp等 destPath543 *目标文件地址 ExceptionformatConvert(BufferedImage bufferedImag, String format, String destPath)throws Exception{547try {548ImageIO.write(bufferedImag, format, new File(destPath));549} catch (IOException e) {RuntimeException(“文件格式转换出错”);551 }552 }* 获取图片文件的真实格式信息557 * imgPath559 *图片原文件存放地址*图片格式 ExceptionString imageFormat(String imgPath)throws Exception{565File file = new File(imgPath);[] bt=new byte[(int) file.length()];568MemoryCacheImageInputStream mcis = new MemoryCacheImageInputStream(new ByteArrayInputStream(bt));569Iterator<ImageReader> it = ImageIO.getImageReaders(mcis);570while(it.hasNext()){571ImageReader imgReader = (ImageReader)it.next();572if(imgReader instanceof GIFImageReader){573format=”gif”;574}else if(imgReader instanceof JPEGImageReader){575format=”jpeg”;576}else if(imgReader instanceof PNGImageReader){577format=”png”;578}else if(imgReader instanceof BMPImageReader){579format=”bmp”;580 }581 }582return format;583 }584 585 }

转载请注明出处[]

posted on

在时间里面我们什么也不能留下,包括痛苦,快乐和生命。

自己封装的一个Java版图片工具,具备压缩,伸缩变换,透明处理,格式

相关文章:

你感兴趣的文章:

标签云: