隨筆-200  評論-148  文章-15  trackbacks-0
          方法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) {
          ?// 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.*;

          //縮略圖類,
          //本java類能將jpg圖片文件,進行等比或非等比的大小轉換。
          //具體使用方法
          //s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度,是否等比縮放(默認為true))
          ?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) {
          //s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度)
          ?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)
          ?);

          ?}
          ?}

          3.jsp方式
          java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,

          ??try
          ????{
          ?java.io.File file = new java.io.File("E:\\Document\\My Pictures\\3.gif");
          ?String newurl="E:\\Document\\My Pictures\\32.gif"; //新的縮略圖保存地址
          ?Image src = javax.imageio.ImageIO.read(file); //構造Image對象
          ?float tagsize=200;
          ?int old_w=src.getWidth(null); //得到源圖寬
          ?int old_h=src.getHeight(null);
          ?int new_w=0;
          ?int new_h=0; //得到源圖長
          ?int tempsize;
          ?float tempdouble;
          ?if(old_w>old_h){
          ?tempdouble=old_w/tagsize;
          ?}else{
          ?tempdouble=old_h/tagsize;
          ?}
          ?new_w=Math.round(old_w/tempdouble);
          ?new_h=Math.round(old_h/tempdouble);//計算新圖長寬
          ?BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
          ?tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪制縮小后的圖
          ?FileOutputStream newimage=new FileOutputStream(newurl); //輸出到文件流
          ?JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
          ?encoder.encode(tag); //近JPEG編碼
          ?newimage.close();

          }catch (Exception e){

          e.toString();

          }
          posted on 2008-06-16 13:04 無聲 閱讀(1844) 評論(0)  編輯  收藏 所屬分類: 職場生活
          道可道非常道,名可名非常名
          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類(174)

          隨筆檔案(200)

          文章分類(20)

          文章檔案(15)

          收藏夾

          開源網站

          朋友博客

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大港区| 额济纳旗| 库车县| 法库县| 晋城| 石门县| 安阳市| 博乐市| 孙吴县| 佛山市| 天峻县| 青河县| 右玉县| 正定县| 安溪县| 白城市| 于都县| 温州市| 伊春市| 双鸭山市| 霍林郭勒市| 沭阳县| 抚顺县| 泸西县| 新郑市| 松桃| 祁阳县| 嫩江县| 恭城| 阜阳市| 麻栗坡县| 抚远县| 达州市| 无锡市| 炉霍县| 大厂| 辛集市| 建阳市| 濮阳市| 城口县| 崇阳县|