您的位置:首页 > 其它

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

2010-07-22 11:47 387 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: