大量的图片如何统一处理成固定宽度、或者固定长宽的图片,java中的java.awt包下面的一些类就可以实现图片处理的功能,下面就是自己写的一个简单的实例

 

下面的类是单张图片的处理,里面的参数含义可以参照参数命名:

     /**    
     *@Description: 压缩或放大图像处理  
     */    
    package cn.lulei.image.deal;     
       
    import java.awt.Color;   
    import java.awt.Font;   
    import java.awt.Graphics2D;   
    import java.awt.Image;   
    import java.awt.image.BufferedImage;   
    import java.io.File;   
    import java.io.FileOutputStream;   
       
    import javax.imageio.ImageIO;   
       
    import com.sun.image.codec.jpeg.JPEGCodec;   
    import com.sun.image.codec.jpeg.JPEGImageEncoder;   
         
    public class DealImage {   
       
        private File file = null;   
        private String inputDir;   
        private String outputDir;   
        private String inputFileName;   
        private String outputFileName;   
        private int outputWidth;   
        private int outputHeight;   
        private boolean proportion;//是否等比压缩   
           
        public DealImage() {   
            this.inputDir = "";   
            this.outputDir = "";   
            this.inputFileName = "";   
            this.outputFileName = "";   
            this.outputHeight = 200;   
            this.outputWidth = 200;   
            this.proportion = true;   
        }   
           
        public void setInputDir(String inputDir){   
            this.inputDir = inputDir;   
        }   
        public void setOutputDir(String outputDir){   
            this.outputDir = outputDir;   
        }   
        public void setInputFileName(String inputFileName){   
            this.inputFileName = inputFileName;   
        }   
        public void setOutputFileName(String outputFileName){   
            this.outputFileName = outputFileName;   
        }   
        public void setWidthAngHeight(int outputWidth, int outputHeight){   
            this.outputWidth = outputWidth;   
            this.outputHeight = outputHeight;   
        }   
           
        /**  
         * @param isVip  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:处理图片  
         */   
        private void dealImage(boolean isVip) {   
            try {   
                this.file = new File(this.inputDir + this.inputFileName);   
                if (!this.file.exists()){   
                    return;   
                }   
                Image img = ImageIO.read(this.file);   
                if (img.getWidth(null) == -1) {   
                    return;   
                }   
                int[] widthAndHeight = this.getNewWidthAndHeight(img);   
                BufferedImage bufferedImage = new BufferedImage(widthAndHeight[0], widthAndHeight[1], BufferedImage.TYPE_INT_RGB);   
                Graphics2D g = bufferedImage.createGraphics();   
                g.drawImage(img.getScaledInstance(widthAndHeight[0], widthAndHeight[1], Image.SCALE_SMOOTH), 0, 0, null);   
                if (!isVip) {   
                    g.setFont(new Font("楷体", Font.ITALIC, 14));   
                    g.setColor(Color.getHSBColor(1.0f, 0.3f, 1.0f));   
                    g.drawString("想要添加的水印", widthAndHeight[0]/3, widthAndHeight[1]/2);   
                }   
                File fileOut = new File(this.outputDir);   
                if (!fileOut.exists()) {   
                    fileOut.mkdirs();   
                }   
                FileOutputStream fileOutputStream = new FileOutputStream(this.outputDir + this.outputFileName);   
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);   
                encoder.encode(bufferedImage);   
                g.dispose();   
                fileOutputStream.close();   
            } catch (Exception e) {   
    //          e.printStackTrace();   
                System.out.println("There're some errors when handling picture which path is :" + this.inputDir + this.inputFileName);   
            }   
        }   
           
        /**  
         * @param inputDir  
         * @param inputFileName  
         * @param outputDir  
         * @param outputFileName  
         * @param isVip  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:处理图片  
         */   
        public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, boolean isVip) {   
            this.dealImage(inputDir, inputFileName, outputDir, outputFileName, this.outputWidth, this.outputHeight, this.proportion, isVip);   
        }   
           
        /**  
         * @param inputDir  
         * @param inputFileName  
         * @param outputDir  
         * @param outputFileName  
         * @param outputWidth  
         * @param outputHeight  
         * @param isVip  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:处理图片  
         */   
        public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, int outputWidth, int outputHeight, boolean isVip) {   
            this.dealImage(inputDir, inputFileName, outputDir, outputFileName, outputWidth, outputHeight, this.proportion, isVip);   
        }   
           
        /**  
         * @param inputDir  
         * @param inputFileName  
         * @param outputDir  
         * @param outputFileName  
         * @param outputWidth  
         * @param outputHeight  
         * @param proportion  
         * @param isVip  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:处理图片  
         */   
        public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, int outputWidth, int outputHeight, boolean proportion, boolean isVip) {   
            this.inputDir = inputDir;   
            this.inputFileName = inputFileName;   
            this.outputDir = outputDir;   
            this.outputFileName = outputFileName;   
            this.outputWidth = outputWidth;   
            this.outputHeight = outputHeight;   
            this.proportion = proportion;   
            this.dealImage(isVip);   
        }   
           
        /**  
         * @param img  
         * @return  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:返回压缩后的图片宽和高  
         */   
        private int[] getNewWidthAndHeight(Image img){   
            int[] reInt = new int[2];   
            if (this.proportion) {   
                double rateW = ((double)img.getWidth(null)) / (double)this.outputWidth + 0.1;   
                double rateH = ((double)img.getHeight(null)) / (double)this.outputHeight + 0.1;   
                double rate = rateW > rateH ? rateW :rateH;   
                reInt[0] = (int)(((double)img.getWidth(null)) / rate);   
                reInt[1] = (int)(((double)img.getHeight(null)) / rate);   
            } else {   
                reInt[0] = this.outputWidth;   
                reInt[1] = this.outputHeight;   
            }   
            return reInt;   
        }   
    }   
 
  
 
  
 
下面的类是对图片文件或者图片文件夹做统一的处理: 
Java代码  收藏代码 
 
     /**    
     *@Description:   批处理文件夹下的所有图片    
     */    
    package cn.lulei.image.deal;     
       
    import java.io.File;   
       
    import cn.lulei.image.deal.DealImage;   
         
         
    public class DealImageFile {   
           
        //图片后缀名   
        private static String[] IMG_TYPE = {".jpg",".bmp",".jpeg",".png",".pcx",".tiff",".gif",".tga",".exif"   
                ,".fpx",".svg",".psd","cdr",".pcd",".dxf",".ufo",".eps",".hdri",".ai",".raw"};    
        private static int outputHeight = 200;   
        private static int outputWidth = 200;   
        private static int count = 0;   
        private static boolean proportion = true;   
           
        public String dealImageFile(String inputDir, String outputDir) {   
            return this.dealImageFile(inputDir, outputDir, outputWidth, outputHeight, proportion);   
        }   
           
        public String dealImageFile(String inputDir, String outputDir, int outputWidth, int outputHeight) {   
            return this.dealImageFile(inputDir, outputDir, outputWidth, outputHeight, proportion);   
        }   
        /**  
         * @param inputDir  
         * @param outputDir  
         * @param outputWidth  
         * @param outputHeight  
         * @param proportion  
         * @return  
         * @Date: 2013-8-7    
         * @Author: lulei    
         * @Description:批处理文件夹下的文件  
         */   
        public String dealImageFile(String inputDir, String outputDir, int outputWidth, int outputHeight, boolean proportion) {   
            File fileDir = new File(inputDir);   
            File[] files = fileDir.listFiles();   
            DealImage dealImage = new DealImage();   
            for (File file : files) {   
                String imgPath = file.toString().toLowerCase();   
                String fileName = imgPath.substring(inputDir.length());   
                if (this.isImage(imgPath)) {   
                    dealImage.dealImage(inputDir, fileName, outputDir, fileName, outputWidth, outputHeight, proportion, true);   
                    count++;    
                } else if (file.isDirectory()) {   
                    if (!imgPath.equals(outputDir.toLowerCase())) {   
                        //这里是递归   
                        this.dealImageFile(imgPath, outputDir + "/" + fileName, outputWidth, outputHeight, proportion);   
                    }   
                }   
            }   
            return "Handle " + count + " pictures! ";   
        }   
           
        private boolean isImage(String imgPath) {   
            for (String str : IMG_TYPE) {   
                if (imgPath.endsWith(str)) {   
                    return true;   
                }   
            }   
            return false;   
        }   
           
        public static void main(String[] args) {   
            // TODO Auto-generated method stub     
               
        }   
       
    }  

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java列出 Windows 下当前运行的所有进程详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。