来源: http://wing929.javaeye.com/blog/185508
方法1:[第一种方法比后一种生成的缩略图要清晰]
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.WritableRaster;
- import java.awt.*;
- import java.awt.geom.AffineTransform;
- import java.io.InputStream;
- import java.io.File;
- import java.io.FileOutputStream;
-
- public class Test {
- public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
-
- int type = source.getType();
- BufferedImage target = null;
- double sx = (double) targetW / source.getWidth();
- double sy = (double) targetH / source.getHeight();
-
-
- if(sx>sy)
- {
- sx = sy;
- targetW = (int)(sx * source.getWidth());
- }else{
- sy = sx;
- targetH = (int)(sy * source.getHeight());
- }
- if (type == BufferedImage.TYPE_CUSTOM) {
- ColorModel cm = source.getColorModel();
- WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
- boolean alphaPremultiplied = cm.isAlphaPremultiplied();
- target = new BufferedImage(cm, raster, alphaPremultiplied, null);
- } else
- target = new BufferedImage(targetW, targetH, type);
- Graphics2D g = target.createGraphics();
-
- g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
- g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
- g.dispose();
- return target;
- }
- public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
- throws Exception {
- BufferedImage srcImage;
-
- String imgType = "JPEG";
- if (fromFileStr.toLowerCase().endsWith(".png")) {
- imgType = "PNG";
- }
-
- File saveFile=new File(saveToFileStr);
- File fromFile=new File(fromFileStr);
- srcImage = ImageIO.read(fromFile);
- if(width > 0 || hight > 0)
- {
- srcImage = resize(srcImage, width, hight);
- }
- ImageIO.write(srcImage, imgType, saveFile);
-
- }
-
- public static void main (String argv[]) {
- try{
-
- Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);
- } catch(Exception e)
- {
- e.printStackTrace();
- }
-
- }
- }
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Test {
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
//则将下面的if else语句注释即可
if(sx>sy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width > 0 || hight > 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);
}
public static void main (String argv[]) {
try{
//参数1(from),参数2(to),参数3(宽),参数4(高)
Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);
} catch(Exception e)
{
e.printStackTrace();
}
}
}
方法2:
- import java.io.*;
- import java.util.*;
- import com.sun.image.codec.jpeg.*;
- import java.awt.image.*;
- import java.awt.*;
- import java.net.*;
- import java.applet.*;
- import java.sql.*;
-
-
-
-
-
- public class Tes {
- String InputDir;
- String OutputDir;
- String InputFileName;
- String OutputFileName;
- int OutputWidth = 80;
- int OutputHeight = 80;
- int rate = 0;
- boolean proportion = true;
-
- public Tes() {
-
- 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.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 = 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 "ok";
- }
-
- 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) {
-
- Tes mypic = new Tes();
- System.out.println(
- mypic.s_pic("E:/Document/My Pictures/",
- "E:/Document/My Pictures/",
- "topbg-3.gif", "3.gif", 400, 400, true)
- );
-
- }
- }