generate thumbnails

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;

 

/*

 * Thumbnail class

 */

public class SmallPic {

private String inputDir; // input map path

private String outputDir; // output graph path

private String inputFileName; // input image file name

private String outFileName; // output image file name

 

private int outputHeight = 180; // Default output image width

private int outputWidth = 180; // Default output image width

private int rate = 0;

private boolean proportion = false; // whether to scale the marker (default is proportional scale)

 

public SmallPic() {

// Initialize variables

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;

// Create output file object

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) // Determine whether it is proportional scaling.

{

// Calculate the output image width and height for proportional scaling

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; // output image width

new_h = outputWidth; // output image height

}

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) {

// input map path

this.inputDir = InputDir;

// output map path

this.outputDir = OutputDir;

// input image file name

this.inputFileName = InputFileName;

// output image file name

this.outFileName = OutputFileName;

return s_pic();

}

 

public String s_pic(String InputDir, String OutputDir,

String InputFileName, String OutputFileName, int width, int height,

boolean gp) {

// input map path

this.inputDir = InputDir;

// output map path

this.outputDir = OutputDir;

// input image file name

this.inputFileName = InputFileName;

// output image file name

this.outFileName = OutputFileName;

// set image length and width

setW_H(width, height);

// Whether it is a proportional zoom marker

this.proportion = gp;

return s_pic();

}

 

public static void main(String[] a) {

// s_pic (large image path, generate small image path, large image file name, generate small image text name, generate small image width, generate small image height)

SmallPic mypic = new SmallPic();

System.out.println(mypic.s_pic(

"D:\\img\\",

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

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

 

}

 

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326813162&siteId=291194637