Java中实现图片裁剪

Java中实现图片裁剪

Java 如何截取图片  

Author      :      zhyiwww

E-Mail       :      [email protected]

Date           :      2007-1-30

转载请注明出处 www.BlogJava.net/zhyiwww

                                (copyright by @ zhangyi)  

下面是我的一段代码,实现如何截取图片的:

         // 图片源

       private static final String SRC_FILE="org//zy//demo//jdk//base//image//car1.jpg";

      // 目标图片

       private static final String DEST_FILE="c://a.jpg";

 

/**

        * 读取图像文件

        * ImageReader

        * @param imgPath

        * @throws IOException

        */

       public void readUsingImageReader(String imgPath) throws IOException{

             

              // 取得图片读入器

              Iterator readers = ImageIO.getImageReadersByFormatName("jpg");

              System.out.println(readers);

              ImageReader reader = (ImageReader)readers.next();

              System.out.println(reader);

             

              // 取得图片读入流

              InputStream source=this.parseImagePath(ImageDemo.SRC_FILE);

              ImageInputStream iis = ImageIO.createImageInputStream(source);

             

              reader.setInput(iis, true);

             

              // 图片参数

             

              ImageReadParam param = reader.getDefaultReadParam();

              int imageIndex = 0;

              int half_width = reader.getWidth(imageIndex)/2;

              int half_height = reader.getHeight(imageIndex)/2;

//            Rectangle rect = new Rectangle(60, 60, half_width, half_height);

              Rectangle rect = new Rectangle(60, 60, 100, 100);

             

              param.setSourceRegion(rect);

      

              BufferedImage bi = reader.read(0,param);             

             

              ImageIO.write(bi, "jpg", this.initDestFile());          

             

       }

 

我的源图片是:

img0.jpg

 

上面的程序运行后截得的图片如下:

img1.jpg

 



|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|

你可能感兴趣的:(Java中实现图片裁剪)