`
supben
  • 浏览: 326645 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

图片截取和缩略

阅读更多
package com.supben.util;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {
	private static final Logger log = Logger.getLogger(ImageUtil.class);
	
	/**
	 * 切割图片
	 * @param x 截点横坐标 (从左开始计数)
	 * @param y 截点纵坐标 (从上开始计数)
	 * @param width 截取的宽度
	 * @param height 截取的长度
	 * @param oldpath 图片位置
	 * @param newpath 新生成的图片位置
	 */
	public static void cutImage(int x, int y, int width, int height, String oldpath, String newpath) {

		FileInputStream is = null;
		ImageInputStream iis = null;
		
		//这个是获取图片扩展名的方法,比如:jpg。我这里有现成的,如果没有,自己实现
		String imgType = StringUtil.getExt(oldpath);

		try {
			is = new FileInputStream(oldpath);
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(imgType);
			ImageReader reader = it.next();
			iis = ImageIO.createImageInputStream(is);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();
			Point p = new Point();
			p.setLocation(x, y);

			Dimension d = new Dimension();
			d.setSize(width, height);
			Rectangle rect = new Rectangle(p, d);
			param.setSourceRegion(rect);

			BufferedImage bi = reader.read(0, param);
			ImageIO.write(bi, imgType, new File(newpath));

			is.close();
			iis.close();
		} catch (Exception e) {
			log.error(e);
		}
	}
	
	/**
	 * 缩略图片
	 * @param oldpath 原图片
	 * @param newpath 新生成的图片存放地址
	 * @param wdith   缩略后的宽
	 * @param height  缩略后的高
	 */
	public static void scaleImage(String oldpath, String newpath, int wdith, int height) {
		// 获取老的图片
		File oldimg = new File(oldpath);

		try {
			BufferedImage bi = ImageIO.read(oldimg);
			Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
			BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
			thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);

			// 缩略后的图片路径
			File newimg = new File(newpath);
			FileOutputStream out = new FileOutputStream(newimg);

			// 绘图
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
			param.setQuality(1.0f, false);
			encoder.encode(thumbnail);
			out.close();
			bi.flush();
			bi = null;
		} catch (IOException e) {
			log.error(e);
		}

	}
	
	
	

	public static void main(String[] args) {
		scaleImage("D:/2.jpg", "D:/3.jpg", 50, 50);
	}
}

分享到:
评论
5 楼 小小流浪猪 2010-11-21  
处理图片,最好还是用ImageMagic
4 楼 greatghoul 2010-11-21  
很不错的代码练习。
3 楼 whaosoft 2010-11-20  
这个网上很多的 说实话
2 楼 joygarden 2010-11-19  
曾经用java做图片切割 速度太慢
1 楼 181054867 2010-11-19  
Java做这个总有不足,不太放心

相关推荐

Global site tag (gtag.js) - Google Analytics