生成缩略图

http://blog.163.com/hjy301@126/blog/static/277123932010517103059983/

http://cache.baiducontent.com/c?m=9d78d513d99b12eb0bfa940f1a67a1716925971238c0a36468d5e35fe2144c35407193be30531710948522685be90f1efdf1456f2a5d7bf0de9fd349d6b1913f2fff7c722757d15612a448f2945b759f7dc547eaab19e2b1f5&p=8b2a971e86cc41ac53f3d5684a4380&newp=882a9546dd921fb918adc62d0214a5231610db2151d4d01e2e8a8508d336&user=baidu&fm=sc&query=java+%CD%BC%C6%AC%CB%F5%B7%C5&qid=&p1=4

////////////

package com.thumbail;

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.FileOutputStream;

import java.io.IOException;

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

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

/*

 * 缩略图类

 */

public class SmallPic {

private String inputDir; // 输入图路径

private String outputDir; // 输出图路径

private String inputFileName; // 输入图文件名

private String outFileName; // 输出图文件名

private int outputHeight = 180; // 默认输出图片宽

private int outputWidth = 180; // 默认输出图片宽

private int rate = 0;

private boolean proportion = false; // 是否等比例缩放标记(默认为等比缩放)

public SmallPic() {

// 初始化变量

inputDir = "";

outputDir = "";

inputFileName = "";

outFileName = "";

outputHeight = 80;

outputWidth = 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.outFileName = 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 + outFileName);

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.printStackTrace();

}

if (img.getWidth(null) == -1) {

System.out.println(" can't read,retry!" + "<BR>");

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 = outputWidth; // 输出的图片高度

}

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 "ok";

}

public String s_pic(String InputDir, String OutputDir,

String InputFileName, String OutputFileName) {

// 输入图路径

this.inputDir = InputDir;

// 输出图路径

this.outputDir = OutputDir;

// 输入图文件名

this.inputFileName = InputFileName;

// 输出图文件名

this.outFileName = 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.outFileName = OutputFileName;

// 设置图片长宽

setW_H(width, height);

// 是否是等比缩放 标记

this.proportion = gp;

return s_pic();

}

public static void main(String[] a) {

// s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)

SmallPic mypic = new SmallPic();

System.out.println(mypic.s_pic(

"D:\\img\\",

"D:\\img\\", "noavatar_middle.gif",

"top2_small.jpg", 150,119, false));

}

}

猜你喜欢

转载自fanshengli-1119.iteye.com/blog/1922795