生成原图缩略图

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

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


public class ImageDrint
{

	
	/**
	 * 新生成的图片的宽度
	 * 新生成的图片的高度
	 * 被读取的图片的URL地址
	 * 要写的图片的地址URL地址
	 * 生成图片的格式 jpg gif jpeg 等等
	 * 
	 */
	//生成的图片宽度 单位像素
	private static  final int IMG_WIDTH = 100;
	//生成的图片高度
	private static  final int IMG_HEIGHT = 100;
	
	
	public static void main(String[] args) throws IOException
	{
		//读取图片
		File file = new File("C:\\temp\\03.jpg");
		
		BufferedImage   sourceImg   =   ImageIO.read(file);
		System.out.println("图片宽度"+sourceImg.getWidth());
		System.out.println("图片高度"+sourceImg.getHeight());
		System.out.println("图片类型"+sourceImg.getType());
		
		//首先判断长宽
		
		//如果是长 > 宽 flag 为true 否则为FALSE
		boolean flag = true;
		
		int img_w = sourceImg.getWidth();
		int img_h = sourceImg.getHeight();
		//如果长度大于等于高度则 flag = true;
		if (img_w >= img_h)
		{
			flag = true;
			System.out.println("图片长度Width大于或者等于高度Height");
		}
		else
		{
			flag = false;
			System.out.println("图片长度Width小于高度Height");
		}
		
		Float f = new Float(0);
		//长大于宽的情况下
		if(flag)
		{
			f = ((new Float(img_h) * IMG_WIDTH )/new Float(img_w));
			System.out.println("新生成的图片的高度height是:"+f);

			int newHeight = f.intValue()+1;
			
			BufferedImage image = new BufferedImage(IMG_WIDTH, newHeight,
					BufferedImage.TYPE_INT_RGB); // 表示一个图像,该图像具有打包成整数像素的 8 位
			
			Graphics g = image.createGraphics();
			
			g.drawImage(sourceImg, 0, 0, IMG_WIDTH, newHeight,  null);
			//释放此图形的上下文并释放它所使用的所有系统资源。
			g.dispose();
			
			
			File itemFile = new File("C:\\temp\\zzz.jpg");
			FileOutputStream out = new FileOutputStream(itemFile);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(image);
			out.flush();
			out.close();
			
		}else
		{
			f = ((new Float(img_w) * IMG_HEIGHT )/new Float(img_h));
			System.out.println("新生成的图片的宽度width是:"+f);
			
			int newWidth = f.intValue()+1;
			BufferedImage image = new BufferedImage(newWidth, IMG_HEIGHT,
					BufferedImage.TYPE_INT_RGB); // 表示一个图像,该图像具有打包成整数像素的 8 位
			
			Graphics g = image.createGraphics();
			
			g.drawImage(sourceImg, 0, 0, newWidth, IMG_HEIGHT,  null);
			//释放此图形的上下文并释放它所使用的所有系统资源。
			g.dispose();
			
			//写新文件的地址
			File itemFile = new File("C:\\temp\\zzz.jpg");
			FileOutputStream out = new FileOutputStream(itemFile);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(image);
			out.flush();
			out.close();
			
		}
		
		//System.out.println(f.intValue() +1);
		
		
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值