缩放图像(按比例),生成指定大小,图片不变型
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
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
相关文章推荐
- 图片按指定比例缩放并压缩至指定大小,解决保存图片文件体积过大bug。
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- 一个可以使得上传的图片大小按照指定的宽度,高度自动按比例进行缩放的函数(C#)
- 用ASP.NET将原始图片按照指定大小按原始比例缩放显示图片
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php按指定大小等比缩放生成上传图片缩略图
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- JS实现控制图片显示大小的方法【图片等比例缩放功能】
- 转:图片等比例缩放,图片指定宽或高按比例缩放,图片不变形缩放
- JS控制图片显示的大小(图片等比例缩放)
- 载入图片后按指定宽高缩放比例
- JS控制图片显示的大小(图片等比例缩放) --zt
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- 学习 ------ ASP.NET C# 按原图片大小缩放生成缩略图
- ASP控制图片显示的大小(等比例缩放)
- PHP等比例缩放图片大小压缩图片空间
- 上传图片,并生成缩略图(指定大小)
- JS控制图片显示的大小(图片等比例缩放)