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

不失真的图片缩放

阅读更多
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.mortennobel.imagescaling.ResampleOp;
import org.apache.log4j.Logger;

/**
 * 图片缩放工具类
 * @author sunnymoon
 */
public class MyImage {
	private final Logger log = Logger.getLogger(this.getClass());
	/**
	 * 接收输入流输生成图片
	 * @param input
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(InputStream input, String writePath,
			Integer width, Integer height, String format) {
		try {
			BufferedImage inputBufImage = ImageIO.read(input);
			log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());
			ResampleOp resampleOp = new ResampleOp(width, height);// 转换
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,
					null);
			ImageIO.write(rescaledTomato, format, new File(writePath));
			log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 接收File输出图片
	 * @param file
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(File file, String writePath, Integer width,
			Integer height, String format) {
		try {
			BufferedImage inputBufImage = ImageIO.read(file);
			inputBufImage.getType();
			log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());
			ResampleOp resampleOp = new ResampleOp(width, height);// 转换
			BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,
					null);
			ImageIO.write(rescaledTomato, format, new File(writePath));
			log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 接收字节数组生成图片
	 * @param RGBS
	 * @param writePath
	 * @param width
	 * @param height
	 * @param format
	 * @return
	 */
	public boolean resizeImage(byte[] RGBS, String writePath, Integer width,
			Integer height, String format) {
		InputStream input = new ByteArrayInputStream(RGBS);
		return this.resizeImage(input, writePath, width, height, format);
	}

	public byte[] readBytesFromIS(InputStream is) throws IOException {
		int total = is.available();
		byte[] bs = new byte[total];
		is.read(bs);
		return bs;
	}
	
	//测试:只测试了字节流的方式,其它的相对简单,没有一一测试
	public static void main(String[] args) throws IOException {
		
		
		int width = 150;
		int height = 150;
		File inputFile = new File("F:\\from.jpg");
		File outFile = new File("F:\\to.jpg");
		String outPath = outFile.getAbsolutePath();
		MyImage myImage = new MyImage();
		InputStream input = new FileInputStream(inputFile);
		byte[] byteArrayImage=myImage.readBytesFromIS(input);
		input.read(byteArrayImage);
		myImage.resizeImage(byteArrayImage, outPath, width, height, "jpg");
	}
}

包下载地址:http://code.google.com/p/java-image-scaling/

4
4
分享到:
评论
9 楼 xiaoxin5230 2010-11-11  
请问这个开源包邮没有增加水印的功能呢
8 楼 sunnymoon 2010-11-04  
sunnymoon 写道
gaobusi 写道
确实是不失真!但是有没有能打到等比例缩放的!
不过等比例缩放应该不难实现!



根据大小去自行判断,还有,保证缩放有时需要截取,比如一个3:4的图片,你想缩放成1:4的,就需要舍掉一部分了



可以这样

ResampleOp resampleOp = new ResampleOp(Math.min(width, inputBufImage.getWidth()), Math.min(height, inputBufImage.getHeight()));// 转换   
7 楼 sunnymoon 2010-11-04  
gaobusi 写道
确实是不失真!但是有没有能打到等比例缩放的!
不过等比例缩放应该不难实现!



根据大小去自行判断,还有,保证缩放有时需要截取,比如一个3:4的图片,你想缩放成1:4的,就需要舍掉一部分了
6 楼 gaobusi 2010-10-29  
确实是不失真!但是有没有能打到等比例缩放的!
不过等比例缩放应该不难实现!
5 楼 gavinsun2008 2010-09-14  
com.mortennobel.imagescaling.ResampleOp 包参看
http://code.google.com/p/java-image-scaling/
作者是一个应用
4 楼 HappyRule 2010-05-19  
com.mortennobel.imagescaling.ResampleOp
请问楼主这个类是你自己的吗?怎么写,
3 楼 sunnymoon 2009-12-17  
是宿小不失真。
2 楼 qinzy 2009-12-17  
com.mortennobel.imagescaling.ResampleOp
是你自己的包?
1 楼 aidiyuxin 2009-12-17  
怎么保证放大不失真的?

相关推荐

Global site tag (gtag.js) - Google Analytics