缩放图像(按比例),生成指定大小,图片不变型

package com.mobtec.gdmobileback.common.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class picScaleAction extends Action{

 public picScaleAction() {
  super();
  // TODO Auto-generated constructor stub
 }
 /**
   * 缩放图像
   *
   * @param srcImageFile
   *            源图像文件地址
   *           
   * @param destWidth
   *            需要生成的图片宽
   *           
   * @param destHeight
   *            需要生成的图片高     
   */
 public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
 {
  return mapping.getInputForward();
 }
  public static void scale(String srcImageFile, int destWidth,int destHeight)
  {
   try
   {
     BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
     int width = src.getWidth(); // 得到源图宽
     int height = src.getHeight(); // 得到源图长
     String descDir = "C:/pic/";
           //第一步,压缩
     if(width>=destWidth&&height>=destHeight)
     {
      if(width>height)
      {
       height = height*destWidth/width;
       width =destWidth;
      }
      else
      {
       width =width*destHeight/height;
       height = destHeight;      
      }
     }
     else if(width>=destWidth&&height<destHeight)
     {
      height = height*destWidth/width;
      width = destWidth;
     }
     else if(width<destWidth&&height>=destHeight)
     {
      width =width*destHeight/height;
      height = destHeight;
     } 
     String picname = "";
     picname = srcImageFile.substring(srcImageFile.lastIndexOf("/")+1,srcImageFile.length());
     Image image = src.getScaledInstance(width, height,Image.SCALE_DEFAULT);
     BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
     Graphics g = tag.getGraphics();
     g.drawImage(image, 0, 0, null); // 绘制缩小后的图
     g.dispose();
     ImageIO.write(tag, "JPEG", new File(descDir+picname));// 输出到文件流
 
      
        //第二步,把缩小的图片写到透明背景图片上,生成新的图片
        createPicTwo(0,0,descDir+picname,destWidth,destHeight);
 
   }
   catch (IOException e)
   {
    e.printStackTrace();
   }
 }
  public static void createPicTwo(int x,int y,String scalepic,int destWidth,int destHeight)
   {
       try
       {
         //读取压缩后的图片
         File fileOne = new File(scalepic);
         BufferedImage ImageOne = ImageIO.read(fileOne);
         int width = ImageOne.getWidth();//图片宽度
         int height = ImageOne.getHeight();//图片高度
         //从图片中读取RGB
         int[] ImageArrayOne = new int[width*height];
         ImageArrayOne = ImageOne.getRGB(0,0,width,height,ImageArrayOne,0,width);

        //画一张新图片,透明背景
     BufferedImage bufFrmImage = new BufferedImage(destWidth,destHeight,BufferedImage.TYPE_INT_RGB);
     Graphics g2D = bufFrmImage.getGraphics();
           g2D.setColor(new Color(255,255,255));
           g2D.fillRect(0,0,destWidth,destHeight);
       
           int x1 = 0;
           int y1 = 0;
           x1 = (destWidth-width)/2;
           y1 = (destHeight-height)/2;
          
           bufFrmImage.setRGB(x1,y1,width,height,ImageArrayOne,0,width);//设置RGB
        File outFile = new File(scalepic);
        ImageIO.write(bufFrmImage, "jpeg", outFile);//写图片

       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
   }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  scale("c:/20081223.jpg",200,500);
  
  //createPicTwo(0,0);
 }

}

原图:366*439

生成后的图片:200*500

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值