一位高手的上传内容中学习到的,并将其代码根据我的需求进行了修改,参考位置:http://jiangpin1987.javaeye.com/blog/749690
?
参考代码:
[java]class="Apple-converted-space">?view plaincopyprint?
- import?java.io.*;??
- import?java.util.logging.Level;??
- import?java.util.logging.Logger;??
- import?java.awt.*;??
- import?com.sun.image.codec.jpeg.*;??
- import?java.awt.image.BufferedImage;??
- ??
- public?class?Img_Middle?{??
- ????public?void?img_change(String?url,?String?name)?{??
- ????????Tosmallerpic(url,?new?File(url?+?name),?"_middle",?name,?188,?165,??
- ????????????????(float)?0.7);??
- ????????Tosmallerpic(url,?new?File(url?+?name),?"_small",?name,?45,?45,??
- ????????????????(float)?0.7);??
- ????????Tosmallerpic(url,?new?File(url?+?name),?"_smaller",?name,?116,?100,??
- ????????????????(float)?0.7);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????private?static?void?Tosmallerpic(String?f,?File?filelist,?String?ext,??
- ????????????String?n,?int?w,?int?h,?float?per)?{??
- ????????Image?src;??
- ????????try?{??
- ????????????src?=?javax.imageio.ImageIO.read(filelist);??
- ??????????????
- ????????????String?img_midname?=?f?+?n.substring(0,?n.indexOf("."))?+?ext??
- ????????????????????+?n.substring(n.indexOf("."));??
- ????????????int?old_w?=?src.getWidth(null);???
- ????????????int?old_h?=?src.getHeight(null);??
- ????????????int?new_w?=?0;??
- ????????????int?new_h?=?0;???
- ????????????double?w2?=?(old_w?*?1.00)?/?(w?*?1.00);??
- ????????????double?h2?=?(old_h?*?1.00)?/?(h?*?1.00);??
- ??????????????
- ????????????BufferedImage?oldpic;??
- ????????????if?(old_w?>?old_h)?{??
- ????????????????oldpic?=?new?BufferedImage(old_w,?old_w,??
- ????????????????????????BufferedImage.TYPE_INT_RGB);??
- ????????????}?else?{??
- ????????????????if?(old_w?<?old_h)?{??
- ????????????????????oldpic?=?new?BufferedImage(old_h,?old_h,??
- ????????????????????????????BufferedImage.TYPE_INT_RGB);??
- ????????????????}?else?{??
- ????????????????????oldpic?=?new?BufferedImage(old_w,?old_h,??
- ????????????????????????????BufferedImage.TYPE_INT_RGB);??
- ????????????????}??
- ????????????}??
- ????????????Graphics2D?g?=?oldpic.createGraphics();??
- ????????????g.setColor(Color.white);??
- ????????????if?(old_w?>?old_h)?{??
- ????????????????g.fillRect(0,?0,?old_w,?old_w);??
- ????????????????g.drawImage(src,?0,?(old_w?-?old_h)?/?2,?old_w,?old_h,??
- ????????????????????????Color.white,?null);??
- ????????????}?else?{??
- ????????????????if?(old_w?<?old_h)?{??
- ????????????????????g.fillRect(0,?0,?old_h,?old_h);??
- ????????????????????g.drawImage(src,?(old_h?-?old_w)?/?2,?0,?old_w,?old_h,??
- ????????????????????????????Color.white,?null);??
- ????????????????}?else?{??
- ??
- ??????????????????????
- ????????????????????g.drawImage(src.getScaledInstance(old_w,?old_h,??
- ????????????????????????????Image.SCALE_SMOOTH),?0,?0,?null);??
- ????????????????}??
- ????????????}??
- ????????????g.dispose();??
- ????????????src?=?oldpic;??
- ??????????????
- ????????????if?(old_w?>?w)??
- ????????????????new_w?=?(int)?Math.round(old_w?/?w2);??
- ????????????else??
- ????????????????new_w?=?old_w;??
- ????????????if?(old_h?>?h)??
- ????????????????new_h?=?(int)?Math.round(old_h?/?h2);??
- ????????????else??
- ????????????????new_h?=?old_h;??
- ????????????BufferedImage?tag?=?new?BufferedImage(new_w,?new_h,??
- ????????????????????BufferedImage.TYPE_INT_RGB);??
- ??????????????
- ??????????????
- ????????????tag.getGraphics().drawImage(??
- ????????????????????src.getScaledInstance(new_w,?new_h,?Image.SCALE_SMOOTH),?0,??
- ????????????????????0,?null);??
- ????????????FileOutputStream?newimage?=?new?FileOutputStream(img_midname);???
- ????????????JPEGImageEncoder?encoder?=?JPEGCodec.createJPEGEncoder(newimage);??
- ????????????JPEGEncodeParam?jep?=?JPEGCodec.getDefaultJPEGEncodeParam(tag);??
- ??????????????
- ????????????jep.setQuality(per,?true);??
- ????????????encoder.encode(tag,?jep);??
- ??????????????
- ??????????????
- ????????????newimage.close();??
- ????????}?catch?(IOException?ex)?{??
- ????????????Logger.getLogger(Img_Middle.class.getName()).log(Level.SEVERE,??
- ????????????????????null,?ex);??
- ????????}??
- ????}??
- ??????
- ????public?static?void?main(String?args[]){????????
- ??????????
- ????????String?f="F://200903300012//pics//201006//";?????
- ????????File?file=new?File(f);??????????
- ????????if(file.exists()){????????
- ????????????File[]?filelist=file.listFiles();???
- ????????????for(int?i=0;i<filelist.length;i++){??????????
- ????????????????String?n=filelist[i].getName();???
- ????????????????Tosmallerpic(f,filelist[i],"_middle",n,185,160,(float)0.7);????
- ????????????????Tosmallerpic(f,filelist[i],"_small",n,45,45,(float)0.7);????
- ????????????????Tosmallerpic(f,filelist[i],"_smaller",n,116,100,(float)0.7);????
- ????????????}????????
- ????????}????
- ????}??
- }??
?
?
第一次修改后的代码:
?
[java]?view plaincopyprint?
- import?java.awt.Color;??
- import?java.awt.Graphics2D;??
- import?java.awt.Image;??
- import?java.awt.image.BufferedImage;??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.FileNotFoundException;??
- import?java.io.FileOutputStream;??
- import?java.io.IOException;??
- import?java.util.Calendar;??
- import?java.util.Date;??
- import?com.sun.image.codec.jpeg.ImageFormatException;??
- import?com.sun.image.codec.jpeg.JPEGCodec;??
- import?com.sun.image.codec.jpeg.JPEGEncodeParam;??
- import?com.sun.image.codec.jpeg.JPEGImageEncoder;??
- ??
- ?
- ?
- ?
- ?
- ?
- ??
- public?class?ImageUtil?{??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????private?static?BufferedImage?InputImage(String?srcImgPath)?{??
- ??
- ????????BufferedImage?srcImage?=?null;??
- ????????try?{??
- ??????????????
- ????????????File?file?=?new?File(srcImgPath);??
- ????????????FileInputStream?in?=?new?FileInputStream(file);??
- ????????????byte[]?b?=?new?byte[5];??
- ????????????in.read(b);??
- ????????????srcImage?=?javax.imageio.ImageIO.read(file);??
- ????????}?catch?(IOException?e)?{??
- ????????????System.out.println("读取图片文件出错!"?+?e.getMessage());??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?srcImage;??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????int?new_w,?int?new_h)?{??
- ????????Tosmallerpic(srcImgPath,?outImgPath,?new_w,?new_h,?1F);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????float?ratio)?{??
- ????????Tosmallerpic(srcImgPath,?outImgPath,?ratio,?1F);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????int?maxLength)?{??
- ????????Tosmallerpic(srcImgPath,?outImgPath,?maxLength,?1F);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????int?new_w,?int?new_h,?float?per)?{??
- ??????????
- ????????BufferedImage?src?=?InputImage(srcImgPath);??
- ????????int?old_w?=?src.getWidth();??
- ??????????
- ????????int?old_h?=?src.getHeight();??
- ??????????
- ??????????
- ????????BufferedImage?tempImg?=?new?BufferedImage(old_w,?old_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ??????????
- ????????Graphics2D?g?=?tempImg.createGraphics();??
- ????????g.setColor(Color.white);??
- ????????g.fillRect(0,?0,?old_w,?old_h);??
- ????????g.drawImage(src,?0,?0,?old_w,?old_h,?Color.white,?null);??
- ????????g.dispose();??
- ????????BufferedImage?newImg?=?new?BufferedImage(new_w,?new_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ????????newImg.getGraphics().drawImage(??
- ????????????????tempImg.getScaledInstance(new_w,?new_h,?Image.SCALE_SMOOTH),?0,??
- ????????????????0,?null);??
- ??????????
- ????????OutImage(outImgPath,?newImg,?per);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????float?ratio,?float?per)?{??
- ??????????
- ????????BufferedImage?src?=?InputImage(srcImgPath);??
- ????????int?old_w?=?src.getWidth();??
- ??????????
- ????????int?old_h?=?src.getHeight();??
- ??????????
- ????????int?new_w?=?0;??
- ??????????
- ????????int?new_h?=?0;??
- ??????????
- ????????BufferedImage?tempImg?=?new?BufferedImage(old_w,?old_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ????????Graphics2D?g?=?tempImg.createGraphics();??
- ????????g.setColor(Color.white);??
- ??????????
- ????????g.drawImage(src,?0,?0,?old_w,?old_h,?Color.white,?null);??
- ????????g.dispose();??
- ??????????
- ????????new_h?=?(int)?Math.round(old_h?*?ratio);??
- ????????BufferedImage?newImg?=?new?BufferedImage(new_w,?new_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ????????newImg.getGraphics().drawImage(??
- ????????????????tempImg.getScaledInstance(new_w,?new_h,?Image.SCALE_SMOOTH),?0,??
- ????????????????0,?null);??
- ??????????
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?void?Tosmallerpic(String?srcImgPath,?String?outImgPath,??
- ????????????int?maxLength,?float?per)?{??
- ??????????
- ????????BufferedImage?src?=?InputImage(srcImgPath);??
- ????????int?old_w?=?src.getWidth();??
- ??????????
- ????????int?old_h?=?src.getHeight();??
- ??????????
- ????????int?new_w?=?0;??
- ??????????
- ????????int?new_h?=?0;??
- ??????????
- ????????BufferedImage?tempImg?=?new?BufferedImage(old_w,?old_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ????????Graphics2D?g?=?tempImg.createGraphics();??
- ????????g.setColor(Color.white);??
- ??????????
- ????????g.fillRect(0,?0,?old_w,?old_h);??
- ????????g.drawImage(src,?0,?0,?old_w,?old_h,?Color.white,?null);??
- ????????g.dispose();??
- ??????????
- ????????if?(old_w?>?old_h)?{??
- ??????????????
- ????????????new_w?=?maxLength;??
- ????????????new_h?=?(int)?Math.round(old_h?*?((float)?maxLength?/?old_w));??
- ????????}?else?{??
- ????????????new_w?=?(int)?Math.round(old_w?*?((float)?maxLength?/?old_h));??
- ????????????new_h?=?maxLength;??
- ????????}??
- ????????BufferedImage?newImg?=?new?BufferedImage(new_w,?new_h,??
- ????????????????BufferedImage.TYPE_INT_RGB);??
- ????????newImg.getGraphics().drawImage(??
- ????????????????tempImg.getScaledInstance(new_w,?new_h,?Image.SCALE_SMOOTH),?0,??
- ????????????????0,?null);??
- ??????????
- ????????OutImage(outImgPath,?newImg,?per);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????private?static?void?OutImage(String?outImgPath,?BufferedImage?newImg,??
- ????????????float?per)?{??
- ??????????
- ????????File?file?=?new?File(outImgPath);??
- ????????if?(!file.getParentFile().exists())?{??
- ????????????file.getParentFile().mkdirs();??
- ????????}??
- ????????try?{??
- ????????????FileOutputStream?newimage?=?new?FileOutputStream(outImgPath);??
- ????????????JPEGImageEncoder?encoder?=?JPEGCodec.createJPEGEncoder(newimage);??
- ????????????JPEGEncodeParam?jep?=?JPEGCodec.getDefaultJPEGEncodeParam(newImg);??
- ??????????????
- ????????????jep.setQuality(per,?true);??
- ????????????encoder.encode(newImg,?jep);??
- ????????????newimage.close();??
- ????????}?catch?(FileNotFoundException?e)?{??
- ??????????????
- ????????}?catch?(ImageFormatException?e)?{??
- ??????????????
- ????????}?catch?(IOException?e)?{??
- ??????????????
- ????????}??
- ????}??
- ??
- ????public?static?void?main(String?args[])?{??
- ????????String?f?=?"c:/img/";??
- ????????File?file?=?new?File(f);??
- ????????if?(file.exists())?{??
- ????????????File[]?filelist?=?file.listFiles();??
- ????????????for?(int?i?=?0;?i?<?filelist.length;?i++)?{??
- ????????????????File?fi?=?filelist[i];??
- ????????????????System.out.println(fi.length());??
- ????????????????String?n?=?filelist[i].getName();??
- ??????????????????
- ??????????????????
- ??????????????????
- ??????????????????
- ????????????}??
- ????????}??
- ????????String?srcImg?=?"c:/img/car_2.jpg";??
- ????????String?tarDir?=?"c:/img/newImg/";??
- ????????long?startTime?=?new?Date().getTime();??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_1.jpg",?400);??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_2.jpg",?0.5F);??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_3.jpg",?400,?500);??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_11.jpg",?400,?0.7F);??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_22.jpg",?0.5F,?0.8F);??
- ????????Tosmallerpic(srcImg,?tarDir?+?"car_1_maxLength_33.jpg",?400,?500,?0.8F);??
- ????????System.out.println(new?Date().getTime()?-?startTime);??
- ????}??
- }??
?
?
第二次修改,只是对长宽尺寸压缩,按原图片质量
[java]?view plaincopyprint?
- import?java.awt.Graphics2D;??
- import?java.awt.Image;??
- import?java.awt.image.BufferedImage;??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.FileNotFoundException;??
- import?java.io.IOException;??
- import?javax.imageio.ImageIO;??
- ??
- ?
- ?
- ?
- ??
- public?class?ImageUtil?{??
- ??????
- ????private?static?BufferedImage?InputImage(String?srcImgPath)?{??
- ????????BufferedImage?srcImage?=?null;??
- ????????try?{??
- ????????????FileInputStream?in?=?new?FileInputStream(srcImgPath);??
- ????????????srcImage?=?javax.imageio.ImageIO.read(in);??
- ????????}?catch?(IOException?e)?{??
- ????????????System.out.println("读取图片文件出错!"?+?e.getMessage());??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?srcImage;??
- ????}??
- ??
- ?????
- ?
- ?
- ??
- ????public?static?void?compressImage(String?srcImgPath,?String?outImgPath,??
- ????????????int?new_w,?int?new_h)?{??
- ????????BufferedImage?src?=?InputImage(srcImgPath);??
- ????????disposeImage(src,?outImgPath,?new_w,?new_h);??
- ????}??
- ??
- ?????
- ?
- ?
- ??
- ????public?static?void?compressImage(String?srcImgPath,?String?outImgPath,??
- ????????????int?maxLength)?{??
- ??????????
- ????????BufferedImage?src?=?InputImage(srcImgPath);??
- ????????if?(null?!=?src)?{??
- ????????????int?old_w?=?src.getWidth();??
- ??????????????
- ????????????int?old_h?=?src.getHeight();??
- ??????????????
- ????????????int?new_w?=?0;??
- ??????????????
- ????????????int?new_h?=?0;??
- ??????????????
- ??????????????
- ????????????if?(old_w?>?old_h)?{??
- ??????????????????
- ????????????????new_w?=?maxLength;??
- ????????????????new_h?=?(int)?Math.round(old_h?*?((float)?maxLength?/?old_w));??
- ????????????}?else?{??
- ????????????????new_w?=?(int)?Math.round(old_w?*?((float)?maxLength?/?old_h));??
- ????????????????new_h?=?maxLength;??
- ????????????}??
- ????????????disposeImage(src,?outImgPath,?new_w,?new_h);??
- ????????}??
- ????}??
- ??
- ??????
- ????private?synchronized?static?void?disposeImage(BufferedImage?src,??
- ????????????String?outImgPath,?int?new_w,?int?new_h)?{??
- ??????????
- ????????int?old_w?=?src.getWidth();??
- ??????????
- ????????int?old_h?=?src.getHeight();??
- ??????????
- ????????BufferedImage?newImg?=?null;??
- ??????????
- ????????switch?(src.getType())?{??
- ????????case?13:??
- ??????????????
- ??????????????
- ????????????break;??
- ????????default:??
- ????????????newImg?=?new?BufferedImage(new_w,?new_h,?BufferedImage.TYPE_INT_RGB);??
- ????????????break;??
- ????????}??
- ????????Graphics2D?g?=?newImg.createGraphics();??
- ??????????
- ????????g.drawImage(src,?0,?0,?old_w,?old_h,?null);??
- ????????g.dispose();??
- ??????????
- ????????newImg.getGraphics().drawImage(??
- ????????????????src.getScaledInstance(new_w,?new_h,?Image.SCALE_SMOOTH),?0,?0,??
- ????????????????null);??
- ??????????
- ????????OutImage(outImgPath,?newImg);??
- ????}??
- ??
- ?????
- ?
- ?
- ??
- ????private?static?void?OutImage(String?outImgPath,?BufferedImage?newImg)?{??
- ??????????
- ????????File?file?=?new?File(outImgPath);??
- ????????if?(!file.getParentFile().exists())?{??
- ????????????file.getParentFile().mkdirs();??
- ????????}??
- ????????try?{??
- ????????????ImageIO.write(newImg,?outImgPath.substring(outImgPath??
- ????????????????????.lastIndexOf(".")?+?1),?new?File(outImgPath));??
- ????????}?catch?(FileNotFoundException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?catch?(IOException?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????}??
- }??