JAvaプログラムで画像を分割する方法と画像の他の操作


package com.langhua.cutFile;   
import java.io.*;      
import java.awt.*;      
import java.awt.image.*;      
import java.awt.Graphics;      
import java.awt.color.ColorSpace;      
import javax.imageio.ImageIO;      
  
import com.langhua.ImageUtils.ImageUtils;   
     
public class ChangeImageSize      
{      
    /** *//**    
     *         
     * @param srcImageFile            
     * @param result                   
     * @param scale                
     * @param flag             :true   ; false   ;    
     */     
    public static void scale(String srcImageFile, String result, int scale, boolean flag)      
    {      
        try     
        {      
            BufferedImage src = ImageIO.read(new File(srcImageFile)); //           
            int width = src.getWidth(); //            
            int height = src.getHeight(); //            
            if (flag)      
            {      
                //         
                width = width * scale;      
                height = height * scale;      
            }      
            else     
            {      
                //         
                width = width / scale;      
                height = height / scale;      
            }      
            Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);      
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);      
            Graphics g = tag.getGraphics();      
            g.drawImage(image, 0, 0, null); //              
            g.dispose();      
            ImageIO.write(tag, "JPEG", new File(result));//             
        }      
        catch (IOException e)      
        {      
            e.printStackTrace();      
        }      
    }      
     
    /**  
     *         
     * @param srcImageFile          
     * @param descDir                 
     * @param destWidth              
     * @param destHeight             
     */     
    public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)      
    {      
        try     
        {      
            Image img;      
            ImageFilter cropFilter;    
            String dir = null;   
            //            
            BufferedImage bi = ImageIO.read(new File(srcImageFile));      
            int srcWidth = bi.getHeight(); //           
            int srcHeight = bi.getWidth(); //        
            System.out.println("srcWidth:"+srcWidth);   
            System.out.println("srcHeight:"+srcHeight);   
            if (srcWidth > destWidth && srcHeight > destHeight)      
            {      
                Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);      
                destWidth = 300; //           
                destHeight = 300; //           
                int cols = 0; //             
                int rows = 0; //             
                //                   
                if (srcWidth % destWidth == 0)      
                {      
                    cols = srcWidth / destWidth;      
                }      
                else     
                {      
                    cols = (int) Math.floor(srcWidth / destWidth) + 1;      
                }      
                if (srcHeight % destHeight == 0)      
                {      
                    rows = srcHeight / destHeight;      
                }      
                else     
                {      
                    rows = (int) Math.floor(srcHeight / destHeight) + 1;      
                }      
                //                         
                for (int i = 0; i < rows; i++)      
                {      
                    for (int j = 0; j < cols; j++)      
                    {      
                        //                       
                        //  : CropImageFilter(int x,int y,int width,int height)      
                        cropFilter = new CropImageFilter(j * 300, i * 300, destWidth, destHeight);      
                        img = Toolkit.getDefaultToolkit().createImage(      
                                        new FilteredImageSource(image.getSource(), cropFilter));      
                        BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);      
                        Graphics g = tag.getGraphics();      
                        g.drawImage(img, 0, 0, null); //              
                        g.dispose();      
                        //           
                        dir = descDir + "cut_image_" + i + "_" + j + ".jpg";   
                        File f = new File(dir);   
                        ImageIO.write(tag, "JPEG",f);   
                        System.out.println(dir);   
                        ImageUtils.pressText("  ",dir,"  ",1,1,25,10,10);   
                    }      
                }      
            }      
        }      
        catch (Exception e)      
        {      
            e.printStackTrace();      
        }      
    }      
     
    /**   
     *        GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)    
     */     
    public static void convert(String source, String result)      
    {      
        try     
        {      
            File f = new File(source);      
            f.canRead();      
            f.canWrite();      
            BufferedImage src = ImageIO.read(f);      
            ImageIO.write(src, "JPG", new File(result));      
        }      
        catch (Exception e)      
        {      
            // TODO Auto-generated catch block      
            e.printStackTrace();      
        }      
    }      
     
    /**  
     *           
     * @param source    
     * @param result    
     */     
    public static void gray(String source, String result)      
    {      
        try     
        {      
            BufferedImage src = ImageIO.read(new File(source));      
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);      
            ColorConvertOp op = new ColorConvertOp(cs, null);      
            src = op.filter(src, null);      
            ImageIO.write(src, "JPEG", new File(result));      
        }      
        catch (IOException e)      
        {      
            e.printStackTrace();      
        }      
    }      
     
    /**  
     * @param args    
     */     
    public static void main(String[] args)      
    {      
        //scale("c:\\test\\456.jpg","C:\\test\\image1.jpg",2,false);      
        //cut("c:\\1.jpg","C:\\2.jpg",64,64);      
        //gray("c:\\test\\456.jpg","C:\\test\\image4.jpg");      
    }      
     
}    

 最適化された画像分割
package com.langhua.cutFile;   
  
import java.awt.Rectangle;   
import java.awt.image.BufferedImage;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.IOException;   
import java.util.Iterator;   
  
import javax.imageio.ImageIO;   
import javax.imageio.ImageReadParam;   
import javax.imageio.ImageReader;   
import javax.imageio.stream.ImageInputStream;   
  
import com.langhua.ImageUtils.ImageUtils;   
  
public class CutImage {   
  
    //         :c:\1.jpg   
    private String srcpath;   
  
    //           . :c:\2.jpg   
    private String subpath;   
  
    //    x     
    private int x;   
  
    private int y;   
  
    //         
    private int width;   
  
    private int height;   
  
    public CutImage() {   
  
    }   
  
    public CutImage(int x, int y, int width, int height) {   
        this.x = x;   
        this.y = y;   
        this.width = width;   
        this.height = height;   
    }   
  
    /**  
     *      ,            。  
     */  
    public void cut() throws IOException {   
  
        FileInputStream is = null;   
        ImageInputStream iis = null;   
  
        try {   
            //          
            is = new FileInputStream(srcpath);   
  
            /**  
             *             ImageReader   Iterator,   ImageReader           。  
             *   :formatName -           . (   "jpeg"   "tiff")  。  
             */  
            Iterator<ImageReader> it = ImageIO   
                    .getImageReadersByFormatName("jpg");   
            ImageReader reader = it.next();   
            //         
            iis = ImageIO.createImageInputStream(is);   
  
            /**  
             * iis:   .true:     .      ‘     ’。                        ,      
             * reader                             。  
             */  
            reader.setInput(iis, true);   
  
            /**  
             * <p>  
             *               
             * <p>  
             * .            Java Image I/O                     。                 
             * ImageReader     getDefaultReadParam       ImageReadParam    。  
             */  
            ImageReadParam param = reader.getDefaultReadParam();   
  
            /**  
             *       。Rectangle              ,   Rectangle     
             *         (x,y)、             。  
             */  
            Rectangle rect = new Rectangle(x, y, width, height);   
  
            //      BufferedImage,             。   
            param.setSourceRegion(rect);   
  
            /**  
             *        ImageReadParam        imageIndex      ,             
             * BufferedImage   。  
             */  
            BufferedImage bi = reader.read(0, param);   
  
            //         
            ImageIO.write(bi, "jpg", new File(subpath));   
        } finally {   
            if (is != null)   
                is.close();   
            if (iis != null)   
                iis.close();   
        }   
    }   
  
    /**  
     *       
     *   
     * @param srcImageFile  
     *                   
     * @param descDir  
     *                     
     * @param destWidth  
     *                    
     * @param destHeight  
     *                       List,            
     */  
    public static java.util.List<String> cutImg(String srcImageFile,   
            String descDir, int destWidth, int destHeight) {   
        java.util.List<String> list = new java.util.ArrayList<String>(9);   
        try {   
            String dir = null;   
            //         
            BufferedImage bi = ImageIO.read(new File(srcImageFile));   
            int srcWidth = bi.getHeight(); //        
            int srcHeight = bi.getWidth(); //        
            if (srcWidth > destWidth && srcHeight > destHeight) {   
                destWidth = 300; //        
                destHeight = 300; //        
                int cols = 0; //          
                int rows = 0; //          
  
                //                
                if (srcWidth % destWidth == 0) {   
                    cols = srcWidth / destWidth;   
                } else {   
                    cols = (int) Math.floor(srcWidth / destWidth) + 1;   
                }   
                if (srcHeight % destHeight == 0) {   
                    rows = srcHeight / destHeight;   
                } else {   
                    rows = (int) Math.floor(srcHeight / destHeight) + 1;   
                }   
                //          
                for (int i = 0; i < rows; i++) {   
                    for (int j = 0; j < cols; j++) {   
                        CutImage cutImage = new CutImage(j * 300, i * 300, 300,   
                                300);   
                        cutImage.setSrcpath(srcImageFile);   
                        dir = descDir + "cut_image_" + i + "_" + j + ".jpg";   
                        cutImage.setSubpath(dir);   
                        cutImage.cut();   
                        list.add("cut_image_" + i + "_" + j + ".jpg");   
                        ImageUtils.pressText("  ", dir, "  ", 1, 1, 25, 10, 10);   
                    }   
                }   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        return list;   
    }   
  
    public int getHeight() {   
        return height;   
    }   
  
    public void setHeight(int height) {   
        this.height = height;   
    }   
  
    public String getSrcpath() {   
        return srcpath;   
    }   
  
    public void setSrcpath(String srcpath) {   
        this.srcpath = srcpath;   
    }   
  
    public String getSubpath() {   
        return subpath;   
    }   
  
    public void setSubpath(String subpath) {   
        this.subpath = subpath;   
    }   
  
    public int getWidth() {   
        return width;   
    }   
  
    public void setWidth(int width) {   
        this.width = width;   
    }   
  
    public int getX() {   
        return x;   
    }   
  
    public void setX(int x) {   
        this.x = x;   
    }   
  
    public int getY() {   
        return y;   
    }   
  
    public void setY(int y) {   
        this.y = y;   
    }   
}