zoukankan      html  css  js  c++  java
  • java截取图片部分尺寸

    package util;
    import java.awt.Rectangle;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.Iterator;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReadParam;
    import javax.imageio.ImageReader;
    import javax.imageio.stream.ImageInputStream;
    
    public class ImgCut {
     
     public void readUsingImageReader(String src, String dest, int w, int h)
       throws Exception {
      // 取得图片读入器
      Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
      ImageReader reader = (ImageReader) readers.next();
      // 取得图片读入流
      InputStream source = new FileInputStream(src);
      ImageInputStream iis = ImageIO.createImageInputStream(source);
      reader.setInput(iis, true);
      // 图片参数
      ImageReadParam param = reader.getDefaultReadParam();
      // 100,200是左上起始位置,300就是取宽度为300的,就是从100开始取300宽,就是横向100~400,同理纵向200~350的区域就取高度150
      // Rectangle rect = new Rectangle(100, 200, 300, 150);//
      int hh = 0;
      if (h > 100)
      hh = (h - 100) / 3;
      Rectangle rect = new Rectangle(0, hh, 227, 100);
      param.setSourceRegion(rect);
      BufferedImage bi = reader.read(0, param);
      ImageIO.write(bi, "jpg", new File(dest));
     }
     public static void main(String[] args) throws Exception { // main方法测试用
      ImgCut t = new ImgCut();
      t.readUsingImageReader("d://2.jpg", "d://3.jpg", 227, 163);
     }
    }

    文章转自:http://blog.sina.com.cn/s/blog_407a68fc0100nrbl.html

  • 相关阅读:
    web第一章(html)
    面向对象第七章,内存的管理,面向对象三大特征封装、继承、多态
    面向对象第六章(接口、强转、多态)
    面向对象第五章(封装、抽象类、匿名内部类)
    面向对象第四章(封装、static)
    面向对象第二章(引用类型数组、继承)
    python 生成随机数
    python 生成测试报告
    python 邮件发送
    python 日志打印
  • 原文地址:https://www.cnblogs.com/dreammyle/p/3993004.html
Copyright © 2011-2022 走看看