码迷,mamicode.com
首页 > 编程语言 > 详细

java截取图片部分尺寸

时间:2016-01-06 15:52:52      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrbl.html

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);
 }

}

java截取图片部分尺寸

标签:

原文地址:http://www.cnblogs.com/wvqusrtg/p/5105377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!