`
234390216
  • 浏览: 10194108 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:460821
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1771877
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1395481
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:393917
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:678253
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:529320
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1178774
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:462010
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:150169
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:66890
社区版块
存档分类
最新评论

Java改变图片的大小

    博客分类:
  • java
阅读更多

前面在做项目的时候,有一个需求是需要上传图片的,然而该图片只是简单的展示一些信息,不需要很大,所以在上传图片的时候改变图片的大小就显得很有必要了!然后就写了下面这个方法来改变图片的大小!

 

 

	/**
	 * 改变图片的大小到宽为size,然后高随着宽等比例变化
	 * @param is 上传的图片的输入流
	 * @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
	 * @param size 新图片的宽
	 * @param format 新图片的格式
	 * @throws IOException
	 */
	public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
		BufferedImage prevImage = ImageIO.read(is);
		double width = prevImage.getWidth();
		double height = prevImage.getHeight();
		double percent = size/width;
		int newWidth = (int)(width * percent);
		int newHeight = (int)(height * percent);
		BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
		Graphics graphics = image.createGraphics();
		graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
		ImageIO.write(image, format, os);
		os.flush();
		is.close();
		os.close();
	}
7
2
分享到:
评论
4 楼 coolhappiness 2013-12-26  
非常感谢!
3 楼 234390216 2011-10-10  
wubaodong 写道
问下,这个能处理 CMYK模式的图片吗?我之前也搞过类似的东西,不过还没有解决CMYK模式的图片。

没试过这个
2 楼 wubaodong 2011-10-10  
借地发下我自己的,简单对比了下,核心用到的东西一样。

package graphicsTest;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

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

public class GraphicsTest1 {

	// 图片宽和高的最大尺寸
	public static final int IMAGEMAXBIG = 2000;
	// 图片宽和高的最小尺寸
	public static final int IMAGEMINBIG = 10;
	// 按原图大小生成新图
	public static final int CREATENEWIMAGETYPE_0 = 0;
	// 按指定的大小生成新图
	public static final int CREATENEWIMAGETYPE_1 = 1;
	// 按原图宽高比例生成新图-按指定的宽度
	public static final int CREATENEWIMAGETYPE_2 = 2;
	// 按原图宽高比例生成新图-按指定的高度
	public static final int CREATENEWIMAGETYPE_3 = 3;
	// 按原图宽高比例生成新图-按指定的宽和高中较大的尺寸
	public static final int CREATENEWIMAGETYPE_4 = 4;
	// 按原图宽高比例生成新图-按指定的宽和高中较小的尺寸
	public static final int CREATENEWIMAGETYPE_5 = 5;

	/**
	 * 
	 * @param _file
	 *            原图片
	 * @param createType
	 *            处理类型
	 * @param newW
	 *            新宽度
	 * @param newH
	 *            新高度
	 * @return
	 * @throws Exception
	 */
	public static String createNewImage(File _file, int createType, int newW,
			int newH) throws Exception {
		if (_file == null)
			return null;
		String fileName = _file.getPath();
		if (fileName == null || "".equals(fileName)
				|| fileName.lastIndexOf(".") == -1)
			return null;
		String newFileName = "_NEW";
		/*
		 * else newFileName = "_" + newFileName;
		 */

		String outFileName = fileName.substring(0, fileName.lastIndexOf("."))
				+ newFileName
				+ fileName.substring(fileName.lastIndexOf("."), fileName
						.length());
		String fileExtName = fileName.substring(
				(fileName.lastIndexOf(".") + 1), fileName.length());
		if (newW < IMAGEMINBIG)
			newW = IMAGEMINBIG;
		else if (newW > IMAGEMAXBIG)
			newW = IMAGEMAXBIG;

		if (newH < IMAGEMINBIG)
			newH = IMAGEMINBIG;
		else if (newH > IMAGEMAXBIG)
			newH = IMAGEMAXBIG;

		// 得到原图信息
		if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()
				|| !checkImageFile(fileExtName))
			return null;
		if ((new File(outFileName)).exists()) {
			System.out.println("file [" + outFileName + "] already exists");
			throw new Exception();
		}
		Image src = ImageIO.read(_file);
		int w = src.getWidth(null);
		int h = src.getHeight(null);

		// 确定目标图片的大小
		int nw = w;
		int nh = h;
		if (createType == CREATENEWIMAGETYPE_0)
			;
		else if (createType == CREATENEWIMAGETYPE_1) {
			nw = newW;
			nh = newH;
		} else if (createType == CREATENEWIMAGETYPE_2) {
			nw = newW;
			nh = (int) ((double) h / (double) w * nw);
		} else if (createType == CREATENEWIMAGETYPE_3) {
			nh = newH;
			nw = (int) ((double) w / (double) h * nh);
		} else if (createType == CREATENEWIMAGETYPE_4) {
			if ((double) w / (double) h >= (double) newW / (double) newH) {
				nh = newH;
				nw = (int) ((double) w / (double) h * nh);
			} else {
				nw = newW;
				nh = (int) ((double) h / (double) w * nw);
			}
		} else if (createType == CREATENEWIMAGETYPE_5) {
			if ((double) w / (double) h <= (double) newW / (double) newH) {
				nh = newH;
				nw = (int) ((double) w / (double) h * nh);
			} else {
				nw = newW;
				nh = (int) ((double) h / (double) w * nw);
			}
		}

		// 构造目标图片
		BufferedImage tag = new BufferedImage(nw, nh,
				BufferedImage.TYPE_INT_RGB);

		// 得到目标图片输出流
		FileOutputStream out = new FileOutputStream(outFileName);

		// 根据需求画出目标图片 方式1
		tag.getGraphics().drawImage(src, 0, 0, nw, nh, null);

		// 将画好的目标图输出到输出流 方式1
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(tag);
		out.close();
		return outFileName;
	}

	public static boolean checkImageFile(String extName) {

		if ("jpg".equalsIgnoreCase(extName))
			return true;
		if ("gif".equalsIgnoreCase(extName))
			return true;
		if ("bmp".equalsIgnoreCase(extName))
			return true;
		if ("jpeg".equalsIgnoreCase(extName))
			return true;
		if ("png".equalsIgnoreCase(extName))
			return true;
		return false;
	}

}



1 楼 wubaodong 2011-10-10  
问下,这个能处理 CMYK模式的图片吗?我之前也搞过类似的东西,不过还没有解决CMYK模式的图片。

相关推荐

Global site tag (gtag.js) - Google Analytics