图片缩放与转换

关键字: java, image, imageio, 缩放, 编码转换

通过对图片重绘,达到图片缩放、压缩编码转换功能。
Java代码 复制代码
  1.        
  2. import java.awt.Image;   
  3. import java.awt.image.BufferedImage;   
  4. import java.awt.image.RenderedImage;   
  5. import java.io.File;   
  6. import java.io.InputStream;   
  7. import java.io.OutputStream;   
  8.   
  9. import javax.imageio.ImageIO;   
  10.   
  11. public abstract class ImageUtils {   
  12.     /**  
  13.      * 缩放图片  
  14.      *   
  15.      * @param width  
  16.      *            输出宽度  
  17.      * @param height  
  18.      *            输出高度  
  19.      * @param input  
  20.      *            输入流  
  21.      * @param output  
  22.      *            输出流  
  23.      * @param format  
  24.      *            输出格式  
  25.      * @return  
  26.      * @throws Exception  
  27.      */  
  28.     public static boolean convert(int width, int height, InputStream input,   
  29.             OutputStream output, String format) throws Exception {   
  30.         // 输入   
  31.         BufferedImage inputImage = ImageIO.read(input);   
  32.         // 转换   
  33.         RenderedImage im = (RenderedImage) convert(height, height, inputImage);   
  34.         // 输出   
  35.         return ImageIO.write(im, format, output);   
  36.     }   
  37.   
  38.     /**  
  39.      * 转换压缩算法  
  40.      *   
  41.      * @param input  
  42.      *            输入文件  
  43.      * @param output  
  44.      *            输出文件  
  45.      * @return  
  46.      * @throws Exception  
  47.      */  
  48.     public static boolean convert(File input, File output) throws Exception {   
  49.         // 输入   
  50.         BufferedImage inputImage = ImageIO.read(input);   
  51.   
  52.         // 转换   
  53.         int width = inputImage.getWidth();   
  54.         int height = inputImage.getHeight();   
  55.   
  56.         RenderedImage im = (RenderedImage) convert(width, height, inputImage);   
  57.         String outputFilename = output.getName();   
  58.         String format = outputFilename.substring(outputFilename   
  59.                 .lastIndexOf('.') + 1);   
  60.         // 输出   
  61.         return ImageIO.write(im, format, output);   
  62.     }   
  63.   
  64.     /**  
  65.      * 缩放图片  
  66.      *   
  67.      * @param width  
  68.      *            输出宽度  
  69.      * @param height  
  70.      *            输出高度  
  71.      * @param input  
  72.      *            输入文件  
  73.      * @param output  
  74.      *            输出文件  
  75.      * @return  
  76.      * @throws Exception  
  77.      */  
  78.     public static boolean convert(int width, int height, File input, File output)   
  79.             throws Exception {   
  80.         // 输入   
  81.         BufferedImage inputImage = ImageIO.read(input);   
  82.         // 转换   
  83.         RenderedImage im = (RenderedImage) convert(width, height, inputImage);   
  84.         String outputFilename = output.getName();   
  85.         String format = outputFilename.substring(outputFilename   
  86.                 .lastIndexOf('.') + 1);   
  87.         // 输出   
  88.         return ImageIO.write(im, format, output);   
  89.     }   
  90.   
  91.     /**  
  92.      * 缩放图片  
  93.      *   
  94.      * @param width  
  95.      *            输出宽度  
  96.      * @param height  
  97.      *            输出高度  
  98.      * @param input  
  99.      *            输入路径  
  100.      * @param output  
  101.      *            输出路径  
  102.      * @return  
  103.      * @throws Exception  
  104.      */  
  105.     public static boolean convert(int width, int height, String inputPath,   
  106.             String outputPath) throws Exception {   
  107.         return convert(width, height, new File(inputPath), new File(outputPath));   
  108.     }   
  109.   
  110.     /**  
  111.      * 转换  
  112.      *   
  113.      * @param width  
  114.      *            输出宽度  
  115.      * @param height  
  116.      *            输出高度  
  117.      * @param input  
  118.      *            BufferedImage  
  119.      * @return BufferedImage  
  120.      * @throws Exception  
  121.      */  
  122.     private static BufferedImage convert(int width, int height,   
  123.             BufferedImage input) throws Exception {   
  124.         // 初始化输出图片   
  125.         BufferedImage output = new BufferedImage(width, height,   
  126.                 BufferedImage.TYPE_INT_RGB);   
  127.   
  128.         // 重新绘图   
  129.         Image image = input.getScaledInstance(output.getWidth(), output   
  130.                 .getHeight(), output.getType());   
  131.   
  132.         output.createGraphics().drawImage(image, nullnull);   
  133.   
  134.         return output;   
  135.     }   
  136.   
  137.     /**  
  138.      * 等比缩放图片  
  139.      *   
  140.      * @param width  
  141.      *            输出宽度  
  142.      * @param height  
  143.      *            输出高度  
  144.      * @param input  
  145.      *            输入流  
  146.      * @param output  
  147.      *            输出流  
  148.      * @return  
  149.      * @throws Exception  
  150.      */  
  151.     public static boolean equimultipleConvert(int width, int height,   
  152.             String input, String output) throws Exception {   
  153.         return equimultipleConvert(width, height, new File(input), new File(   
  154.                 output));   
  155.     }   
  156.   
  157.     /**  
  158.      * 等比缩放图片  
  159.      *   
  160.      * @param width  
  161.      *            输出宽度  
  162.      * @param height  
  163.      *            输出高度  
  164.      * @param input  
  165.      *            输入流  
  166.      * @param output  
  167.      *            输出流  
  168.      * @return  
  169.      *   
  170.      * @throws Exception  
  171.      */  
  172.     public static boolean equimultipleConvert(int width, int height,   
  173.             File input, File output) throws Exception {   
  174.         // 输入   
  175.         BufferedImage image = ImageIO.read(input);   
  176.   
  177.         // 重新核算尺寸   
  178.         if (image.getWidth() > 0 && image.getHeight() > 0) {   
  179.             if ((image.getWidth() / image.getHeight()) >= (width / height)) {   
  180.                 if (image.getWidth() > width) {   
  181.                     height = (image.getHeight() * width) / image.getWidth();   
  182.                 } else {   
  183.                     width = image.getWidth();   
  184.                     height = image.getHeight();   
  185.                 }   
  186.             } else {   
  187.                 if (image.getHeight() > height) {   
  188.                     width = (image.getWidth() * height) / image.getHeight();   
  189.                 } else {   
  190.                     width = image.getWidth();   
  191.                     height = image.getHeight();   
  192.                 }   
  193.             }   
  194.         }   
  195.   
  196.         // 转换 输出   
  197.         return convert(width, height, input, output);   
  198.     }   
  199. }  
	
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * 
 * @author 梁栋
 * @version 1.0
 * @since 1.0
 */
public abstract class ImageUtils {
	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @param format
	 *            输出格式
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, InputStream input,
			OutputStream output, String format) throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);
		// 转换
		RenderedImage im = (RenderedImage) convert(height, height, inputImage);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 转换压缩算法
	 * 
	 * @param input
	 *            输入文件
	 * @param output
	 *            输出文件
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(File input, File output) throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);

		// 转换
		int width = inputImage.getWidth();
		int height = inputImage.getHeight();

		RenderedImage im = (RenderedImage) convert(width, height, inputImage);
		String outputFilename = output.getName();
		String format = outputFilename.substring(outputFilename
				.lastIndexOf('.') + 1);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入文件
	 * @param output
	 *            输出文件
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, File input, File output)
			throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);
		// 转换
		RenderedImage im = (RenderedImage) convert(width, height, inputImage);
		String outputFilename = output.getName();
		String format = outputFilename.substring(outputFilename
				.lastIndexOf('.') + 1);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入路径
	 * @param output
	 *            输出路径
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, String inputPath,
			String outputPath) throws Exception {
		return convert(width, height, new File(inputPath), new File(outputPath));
	}

	/**
	 * 转换
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            BufferedImage
	 * @return BufferedImage
	 * @throws Exception
	 */
	private static BufferedImage convert(int width, int height,
			BufferedImage input) throws Exception {
		// 初始化输出图片
		BufferedImage output = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		// 重新绘图
		Image image = input.getScaledInstance(output.getWidth(), output
				.getHeight(), output.getType());

		output.createGraphics().drawImage(image, null, null);

		return output;
	}

	/**
	 * 等比缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @return
	 * @throws Exception
	 */
	public static boolean equimultipleConvert(int width, int height,
			String input, String output) throws Exception {
		return equimultipleConvert(width, height, new File(input), new File(
				output));
	}

	/**
	 * 等比缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @return
	 * 
	 * @throws Exception
	 */
	public static boolean equimultipleConvert(int width, int height,
			File input, File output) throws Exception {
		// 输入
		BufferedImage image = ImageIO.read(input);

		// 重新核算尺寸
		if (image.getWidth() > 0 && image.getHeight() > 0) {
			if ((image.getWidth() / image.getHeight()) >= (width / height)) {
				if (image.getWidth() > width) {
					height = (image.getHeight() * width) / image.getWidth();
				} else {
					width = image.getWidth();
					height = image.getHeight();
				}
			} else {
				if (image.getHeight() > height) {
					width = (image.getWidth() * height) / image.getHeight();
				} else {
					width = image.getWidth();
					height = image.getHeight();
				}
			}
		}

		// 转换 输出
		return convert(width, height, input, output);
	}
}


给出一个简单的测试类:
Java代码 复制代码
  1.   
  2. import org.junit.Test;   
  3.   
  4. public class ImageUtilsTest {   
  5.   
  6.     /**  
  7.      * Test method for  
  8.      * {@link org.zlex.common.image.ImageUtils#main(java.lang.String[])}.  
  9.      */  
  10.     @Test  
  11.     public void test() throws Exception {   
  12.         System.out.println(ImageUtils.convert(16501024"c://1.png",   
  13.                 "c://1.png.jpg"));   
  14.         System.out.println(ImageUtils.convert(400300"c://1.jpg",   
  15.                 "c://1.jpg.jpg"));   
  16.         System.out.println(ImageUtils.convert(400300"c://1.jpg",   
  17.                 "c://1.jpg.png"));   
  18.         System.out.println(ImageUtils.convert(5050"c://1.jpg",   
  19.                 "c://1.jpg.gif"));   
  20.         System.out.println(ImageUtils.convert(4030"c://1.bmp",   
  21.                 "c://1.bmp.gif"));   
  22.         System.out.println(ImageUtils   
  23.                 .convert(4030"c://1.bmp""c://1.jpeg"));   
  24.         System.out.println(ImageUtils.equimultipleConvert(16001400new File(   
  25.                 "c://1.bmp"), new File("c://1Equimultiple.jpeg")));   
  26.   
  27.     }   
  28.   
  29. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值