Java图片缩放代码

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
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 ImgUtils {

	/** 
	* 图像缩放 jpg格式 
	* @param imgsrc :原图片文件路径 
	* @param imgdist:生成的缩略图片文件路径 
	* @param widthdist:生成图片的宽度 
	* @param heightdist:生成图片的高度 
	*/  
	public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist){
		try{
			File srcFile = new File(imgsrc);
			if(!srcFile.exists()){
				return;
			}
			
			Image src = ImageIO.read(srcFile);
//			//按比例
//			int imageWidth = src.getWidth(null);  
//			int imageHeight = src.getHeight(null);
//			System.out.println("原始宽度:"+imageWidth+"--------------原始高度:"+imageHeight);
//			if ( imageWidth >= imageHeight){  
//				widthdist = widthdist;  
//				heightdist = (int)Math.round((imageHeight * widthdist * 1.0 / imageWidth));  
//			}else{  
//				heightdist = heightdist;  
//				widthdist = (int)Math.round((imageWidth * heightdist * 1.0 / imageHeight));  
//			}  
			BufferedImage ti = new BufferedImage((int)widthdist, (int)heightdist, BufferedImage.TYPE_INT_RGB);
			ti.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
			
			FileOutputStream out = new FileOutputStream(imgdist); 
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			
			encoder.encode(ti);  
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void reduceImgGif(String imgsrc, String imgdist, int widthdist, int heightdist) {  
		try {  
			File srcfile = new File(imgsrc);  
			if (!srcfile.exists()) {  
			    return;  
			}  
			Image src = ImageIO.read(srcfile);  
			   
			BufferedImage tag = new BufferedImage((int) widthdist, (int) heightdist, BufferedImage.TYPE_INT_RGB);  

			tag.getGraphics().drawImage(  
			src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);  

			ImageIO.write(tag, "gif", new File(imgdist));  
			   
		} catch (Exception ex) {  
			ex.printStackTrace();  
		}  
	} 
	
	/** 
	* 根据图片路径 读取图片文件 
	* @param fileName 
	* @return 
	*/  
	public static BufferedImage readImage(String fileName) {  
		BufferedImage bi = null;  
	    try {  
	    	bi = ImageIO.read(new File(fileName));  
	    } catch (Exception ioe) {  
	        ioe.printStackTrace();  
	    }  
	    return bi;  
	} 
	
	 /** 
	 * 生成新的图片文件 
	 * @param im 
	 * @param formatName 
	 * @param fileName 
	 * @return 
	 */  
	 public static boolean writeImage(RenderedImage im, String formatName, String fileName) {  
		 boolean result = false;  
	     try {  
	    	 result = ImageIO.write(im, formatName, new File(fileName));  
	     } catch (Exception ioe) {  
	         ioe.printStackTrace();  
	     }  
	     return result;  
	 }
	 
	  /** 
	  * 转换图片格式 到 jpg 
	  * @param im 
	  * @param fileName 
	  * @return 
	  */  
	  public static boolean writeJPEGImage(RenderedImage im, String fileName) {  
		  return writeImage(im, "JPEG", fileName);  
	  }  
	    
	  /** 
	  * 转换图片格式 到 gif  不知到好用不 
	  * @param im 
	  * @param fileName 
	  * @return 
	  */  
	  public static boolean writeGIFImage(RenderedImage im, String fileName) {  
	      return writeImage(im, "GIF", fileName);  
	  }  
	    
	  public static boolean writePNGImage(RenderedImage im, String fileName) {  
	      return writeImage(im, "PNG", fileName);  
	  }  
	    
	  public static boolean writeBMPImage(RenderedImage im, String fileName) {  
	      return writeImage(im, "BMP", fileName);  
	  } 
	  
	  public static void main(String[] args) {
		  ImgUtils.reduceImg( "G:/Wow/a.jpg", "G:/Wow/b.jpg", 100, 200);
	  }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值