JAVA改变上传图片大小

package com.test;


import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;



public class ImageUtil {

public static void main(String arg[])
{
     String filePath = "C:\\Users\\Lee\\Desktop\\111.jpg"; // 图片的位置

     int height=300;
     int width=300;
     Icon icon = null;
     try
     {
    	 icon = getFixedIcon(filePath,width,height);
     }
     catch(Exception e)
     {
      System.out.println("exception : " +e);
     }
     System.out.println(" ### " +icon); //生成新图片的位置;
}

/**
 * 按宽的比例更改图片的大小
 * @param filePath 图片路径
 * @param width 需要改变图片的宽度
 * @return
 * @throws Exception
 */
public static Icon getRatioWidth(String filePath, int width) throws Exception{
	
	File f = new File(filePath); 
	
	BufferedImage bi = ImageIO.read(f);
	
	double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例
	
	int height = (int)(wRatio * bi.getHeight());  //图片转换后的高度
	
	Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小
	
	AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,wRatio),null);   //设置图像的缩放比例
	
	image = op.filter(bi,null);
	
	int lastLength = filePath.lastIndexOf(".");   
	String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
    String fileType = filePath.substring(lastLength);  //图片类型
    File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);
    
    Icon ret = null;
	try
    {
     ImageIO.write((BufferedImage)image, "jpg", zoomFile); 
     ret = new ImageIcon(zoomFile.getPath()); 
    }
    catch (Exception e) 
    {
     e.printStackTrace();
    } 
    
   return ret;
}
/**
 * 按高的比例更改图片大小
 * @param filePath 图片路径
 * @param height 需要改变图片的高度
 * @return
 * @throws Exception
 */
public static Icon getRatioHeight(String filePath, int height) throws Exception{
	File f = new File(filePath); 
	
	BufferedImage bi = ImageIO.read(f);
	
	double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例
	
	int width = (int)(hRatio * bi.getWidth());  //图片转换后的高度
	
	Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小
	
	AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(hRatio,hRatio),null);   //设置图像的缩放比例
	
	image = op.filter(bi,null);
	
	int lastLength = filePath.lastIndexOf(".");   
	String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
    String fileType = filePath.substring(lastLength);  //图片类型
    File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);
    
    Icon ret = null;
	try
    {
     ImageIO.write((BufferedImage)image, "jpg", zoomFile); 
     ret = new ImageIcon(zoomFile.getPath()); 
    }
    catch (Exception e) 
    {
     e.printStackTrace();
    } 
    
   return ret;
}

/**
 * 按输入的任意宽高改变图片的大小
 * @param filePath
 * @param width
 * @param height
 * @return
 * @throws Exception
 */
public static Icon getFixedIcon(String filePath, int width, int height) throws Exception{
	File f = new File(filePath); 
	
	BufferedImage bi = ImageIO.read(f);
	
	double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例
	
	double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例
	
	Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小
	
	AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio),null);   //设置图像的缩放比例
	
	image = op.filter(bi,null);
	
	int lastLength = filePath.lastIndexOf(".");   
	String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径
    String fileType = filePath.substring(lastLength);  //图片类型
    File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);
    
    Icon ret = null;
	try
    {
     ImageIO.write((BufferedImage)image, "jpg", zoomFile); 
     ret = new ImageIcon(zoomFile.getPath()); 
    }
    catch (Exception e) 
    {
     e.printStackTrace();
    } 
    
   return ret;
}

}


 

上面的注释与使用方法写的很详细了,可以自己去试试,如需要传入IO流和输出IO流,可以对方法进对适当的修改即可

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值