JAVA生成缩略小图片类

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JAVA生成缩略小图片类

java.awt.image.BufferedImage是缓冲图片类主要将生成的图片对象缓冲起来;javax.imageio.ImageIO是图片IO控制类,可以将缓冲图片对象输出为文件,也可以将文件读为缓冲图片对象,java.awt.Graphics2D类可以操作缓冲图片对象;JAI(Java Advanced Image)也行,包括组件JMagick都是可行的,自己找找资料吧

JAVA生成缩略小图片类2007-01-25 17:13生成缩略小图片类,把它放在tgcom_cdsia\src\tgcom\common下,使用方法:
s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)

源代码:

package mon;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import .*;
import java.applet.*;
import java.sql.*;

/**
*缩略图类
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @author 蒲刚 2007-1-2 21:00
* 本java类能将jpg图片文件,进行等比或非等比的大小转换
* 具体使用方法
* s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
*/

public class Small_pic{
String InputDir; //输入图路径
String OutputDir; //输出图路径
String InputFileName; //输入图文件名
String OutputFileName; //输出图文件名
int OutputWidth=80; //默认输出图片宽
int OutputHeight=80; //默认输出图片高
int rate=0;
boolean proportion=true; //是否等比缩放标记(默认为等比缩放)

public Small_pic(){
//初始化变量
InputDir="";
OutputDir="";
InputFileName="";
OutputFileName="";
OutputWidth=80;
OutputHeight=80;
rate=0;
}

public void setInputDir(String InputDir){
this.InputDir=InputDir;
}

public void setOutputDir(String OutputDir){
this.OutputDir=OutputDir;
}

public void setInputFileName(String InputFileName){
this.InputFileName=InputFileName;
}

public void setOutputFileName(String OutputFileName){
this.OutputFileName=OutputFileName;
}

public void setOutputWidth(int OutputWidth){
this.OutputWidth=OutputWidth;
}

public void setOutputHeight(int OutputHeight){
this.OutputHeight=OutputHeight;
}

public void setW_H(int width,int height){
this.OutputWidth=width;
this.OutputHeight=height;
}

public String s_pic(){
BufferedImage image;
String NewFileName;
//建立输出文件对象
File file = new File(OutputDir+OutputFileName);
FileOutputStream tempout =null;
try{
tempout= new FileOutputStream(file);
}catch(Exception ex){
System.out.println(ex.toString());
}
Image img=null;
Toolkit tk=Toolkit.getDefaultToolkit();
Applet app=new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img=tk.getImage(InputDir+InputFileName);
mt.addImage(img, 0);
mt.waitForID(0);
}catch(Exception e) {
e.printStackTra

ce();
}

if(img.getWidth(null)==-1){
System.out.println(" can't read,retry!"+"
");
return "no";
}else{
int new_w;
int new_h;
if (this.proportion==true) //判断是否是等比缩放.
{
//为等比缩放计算输出的图片宽度及高度
double rate1=((double)img.getWidth(null))/(double)OutputWidth+0.1;
double rate2=((double)img.getHeight(null))/(double)OutputHeight+0.1;
double rate=rate1>rate2?rate1:rate2;
new_w=(int)(((double)img.getWidth(null))/rate);
new_h=(int)(((double)img.getHeight(null))/rate);
}
else{
new_w=OutputWidth; //输出的图片宽度
new_h=OutputHeight; //输出的图片高度
}
BufferedImage buffImg = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);

Graphics g = buffImg.createGraphics();

g.setColor(Color.white);
g.fillRect(0,0,new_w,new_h);

g.drawImage(img,0,0,new_w,new_h,null);
g.dispose();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try{
encoder.encode(buffImg);
tempout.close();
}catch(IOException ex){
System.out.println(ex.toString());
}
}
return "转换成功!";
}

public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName){
//输入图路径
this.InputDir=InputDir;
//输出图路径
this.OutputDir=OutputDir;
//输入图文件名
this.InputFileName=InputFileName;
//输出图文件名
this.OutputFileName=OutputFileName;
return s_pic();
}

public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName,int width,int height,boolean gp){
//输入图路径
this.InputDir=InputDir;
//输出图路径
this.OutputDir=OutputDir;
//输入图文件名
this.InputFileName=InputFileName;
//输出图文件名
this.OutputFileName=OutputFileName;
//设置图片长宽
setW_H(width,height);
//是否是等比缩放 标记
this.proportion=gp;
return s_pic();
}
public static void main(String [] a)
{
//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
Small_pic mypic =new Small_pic();
System.out.println(
mypic.s_pic("E:\\JAVA\\","E:\\JAVA\\","1.jpg","new1.jpg",80,80,true)
);
}
}


////////////////////////////////////////////////////////////////////////
一个生成文字图片的JAVA函数
该文章转载自网络大本营:/Info/13803.Html

try {
BufferedImage bufImg = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufImg.getGraphics();
g2d.drawString("Test123",10,10);

ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
JPEGEncodeParam params = JPEGCodec.getDefaultJPEGEncodeParam(bufImg);
params.setQuality(100, true);
enc.encode(bufImg, params);

File f = new File("test.jpg");
FileOutputStream fimage = new FileOutputStream(new File("test.jpg"));
boutstream.writeTo(fimage);
fimage.c

lose();
} catch (Exception e) {
System.out.println(e);
}



--------------------------------------------------------------------------
java 生成缩略图 zz

java 生成缩略图 zz
关键词: java 缩略图

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import junit.framework.TestCase;

public class changeImg extends TestCase {

public static void testChangePhoto() {

File file = new File("c:/b.jpg");
String sourceImg = "c:/a.jpg";
int width = 100;
int height = 0;

if (!file.exists()) {
Image img = null;
int OutputWidth = 120;
int OutputHeight = 93;
FileOutputStream tempout = null;

try {
tempout = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
System.out.println(ex.toString());
}

Toolkit tk = Toolkit.getDefaultToolkit();
Applet app = new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img = tk.getImage(sourceImg);
mt.addImage(img, 0);
mt.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}

if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!");
} else {
int new_w;
int new_h;
if (true) // 判断是否是等比缩放.
{
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) OutputWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) OutputHeight + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) (((double) img.getWidth(null)) / rate);
new_h = (int) (((double) img.getHeight(null)) / rate);
} else {
if (width > 0 && height > 0) {
new_w = width; // 输出的图片宽度
new_h = height; // 输出的图片高度
} else {
new_w = OutputWidth; // 输出的图片宽度
new_h = OutputHeight; // 输出的图片高度
}
}

BufferedImage buffImg = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);

Graphics g = buffImg.createGraphics();

g.setColor(Color.white);
g.fillRect(0, 0, new_w, new_h);

g.drawImage(img, 0, 0, new_w, new_h, null);
g.dispose();

try {
ImageIO.write(buffImg, "jpeg", tempout);
} catch (Exception ex) {
ex.printStackTrace();
}
// JPEGImageEncoder encoder =
// JPEGCodec.createJPEGEncoder(tempout);
// try {
// encoder.encode(buffImg);
// tempout.close();
// } catch (IOException ex) {
// System.out.println(ex.toString());
// }
}
}
}

}



相关文档
最新文档