读书人

一个图片缩放处置

发布时间: 2012-10-24 14:15:58 作者: rapoo

一个图片缩放处理
package com.tjsoft.system;

import java.io.*;

import java.io.File;

import java.io.FileOutputStream;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* <p>Title:CImageZoom.java </p>

* <p>Description:图像缩放,支持JPG(JPEG)、GIF、PNG三种格式</p>

* 长,宽,比例,最少要设置一个

* <p>Copyright: Copyright 2002 Shenzhen Taiji SoftwareCorparation</p>

* <p>Company:Shenzhen Taiji SoftwareCorparation </p>

* @author 邹纪根

* @version 1.0 2003-10-05

*/

public class CImageZoom {

// 对象是否己经初始化

private boolean isInitFlag = false;

// 定义生成小图片的宽度和高度,给其一个就可以了

private int smallpicwidth = 0;

private int smallpicheight = 0;

// 定义小图片的相比原图片的比例

private double picscale = 0;

/**

* 构造函数

* @param 没有参数

*/

public CImageZoom() {

this.picscale = 0;

this.smallpicwidth = 0;

this.smallpicheight = 0;

this.isInitFlag = false;

}

/**

* @param scale 设置缩影图像相对于源图像的大小比例

*/

public void SetScale(double scale) {

if (scale <= 0) {

System.out.println(" 缩放比例不能为 0 和负数! ");

}

this.picscale = scale;

this.isInitFlag = true;

}

/**

* @param smallpicwidth 设置缩影图像的宽度

*/

public void SetSmallWidth(int smallpicwidth) {

if (smallpicwidth <= 0) {

System.out.println(" 缩影图片的宽度不能为 0 和负数! ");

}

this.smallpicwidth = smallpicwidth;

this.isInitFlag = true;

}

/**

* @param smallpicheight 设置缩影图像的高度

*/

public void SetSmallHeight(int smallpicheight) {

if (smallpicheight <= 0) {

System.out.println(" 缩影图片的高度不能为 0 和负数! ");

}

this.smallpicheight = smallpicheight;

this.isInitFlag = true;

}

/**

* @description 生成源图像的缩影图像

* @param pic_big_pathfilename 源图像的完全路径

* @param pic_small_pathfilename 缩影图像的存放路径

*/

public boolean doZoom(String pic_big_pathfilename, String pic_small_pathfilename) {

if (!this.isInitFlag) {

System.out.println(" 对象参数没有初始化! ");

return false;

}

if (pic_big_pathfilename == null || pic_small_pathfilename == null) {

System.out.println(" 包含文件名的路径为空! ");

return false;

}

int smallw = 0;

int smallh = 0;

File fi = new File(pic_big_pathfilename);

Image src = null;

try {

src = javax.imageio.ImageIO.read(fi); //通过缓冲读入源图片文件,构造Image对象

}

catch (IOException ex) {

System.out.println(" 读取源图像文件出错! ");

return false;

}

int srcw=src.getWidth(null); //得到源图宽

int srch=src.getHeight(null); //得到源图长

double scale = (double) srcw / srch; // 图像的长宽比例

//如果长宽都设置了

if(this.smallpicheight!=0&&this.smallpicwidth!=0){

double scalesmall=(double)this.smallpicwidth/this.smallpicheight;

if(scale>scalesmall){

smallw = this.smallpicwidth;

smallh=(int)((double)this.smallpicwidth/scale);

}else{

smallh = this.smallpicheight;

smallw=(int)((double)this.smallpicheight*scale);

}

}else{

// 只设置了宽

if (this.smallpicwidth != 0) {

smallw = this.smallpicwidth;

smallh = (smallw * srch) / srcw;

}

//只设置了长

else if (this.smallpicheight != 0) {

smallh = this.smallpicheight;

smallw = (smallh * srcw) / srch;

}

// 只设置的缩小比例

else if (this.picscale != 0) {

smallw = (int) ( (float) srcw * this.picscale);

smallh = (int) ( (float) srch * this.picscale);

}

else {

System.out.println(" 对象参数初始化不正确! ");

return false;

}

}

try {

BufferedImage tag = new BufferedImage(smallw,smallh,BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src,0,0,smallw,smallh,null);

FileOutputStream out=new FileOutputStream(pic_small_pathfilename);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

return true;

}

catch (IOException ex1) {

System.out.println(" 写入缩略图像文件出错! ");

return false;

}

}

public static void main(String arg[]) throws Exception {

try{

CImageZoom CImageZoom = new CImageZoom();

CImageZoom.SetSmallWidth(600);

CImageZoom.SetSmallHeight(400);

CImageZoom.doZoom("D:\\uploadfile\\20060514154821562626-1.jpg","e:\\sm.jpg");

}catch(Exception e){

e.printStackTrace();

}

}

}



csdn

读书人网 >Web前端

热点推荐