最近網(wǎng)上看到兩種不同的java圖片縮略圖的繪制方案
第一種,使用Graphics().drawImage按照一定的比例重新繪制圖像。
- package com.image.suoluetu;
-
- import java.io.*;
- import java.awt.*;
- import java.awt.image.*;
- import com.sun.image.codec.jpeg.*;
-
- public class DrawImage {
- private String destFile;
- private int width;
- private int height;
- private Image img;
-
- public DrawImage(String fileName) throws IOException {
- File _file = new File(fileName); // 讀入文件
- _file.getName();
- this.destFile = "D:/dage2.jpg";// this.srcFile.substring(0,
- // this.srcFile.lastIndexOf("."))
- // +"_s.jpg";
- img = javax.imageio.ImageIO.read(_file); // 構(gòu)造Image對象
- width = img.getWidth(null); // 得到源圖寬
- height = img.getHeight(null); // 得到源圖長
- }
-
- /**
- * /**
- *
- * @param args
- */
- public void resize(int w, int h) throws IOException {
- try {
- BufferedImage _image = new BufferedImage(w, h,
- BufferedImage.TYPE_INT_RGB);
- _image.getGraphics().drawImage(img, 0, 0, w, h, null); // 繪制縮小后的圖
- FileOutputStream newimageout = new FileOutputStream(destFile); // 輸出到文件流
- /*
- * JPEGImageEncoder 將圖像緩沖數(shù)據(jù)編碼為 JPEG 數(shù)據(jù)流。該接口的用戶應(yīng)在 Raster 或
- * BufferedImage 中提供圖像數(shù)據(jù),在 JPEGEncodeParams 對象中設(shè)置必要的參數(shù), 并成功地打開
- * OutputStream(編碼 JPEG 流的目的流)。JPEGImageEncoder 接口可 將圖像數(shù)據(jù)編碼為互換的縮略
- * JPEG 數(shù)據(jù)流,該數(shù)據(jù)流將寫入提供給編碼器的 OutputStream 中。
- * 注意:com.sun.image.codec.jpeg 包中的類并不屬于核心 Java API。它們屬于 Sun 發(fā)布的 JDK
- * 和 JRE 產(chǎn)品的組成部分。雖然其它獲得許可方可能選擇發(fā)布這些類,但開發(fā)人員不能寄 希望于從非 Sun
- * 實現(xiàn)的軟件中得到它們。我們期望相同的功能最終可以在核心 API 或標準擴 展中得到。
- */
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimageout);
- encoder.encode(_image); // 近JPEG編碼
- newimageout.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- /**
- * 按照固定的比例縮放圖片
- *
- * @param t
- * double 比例
- * @throws IOException
- */
- public void resize(double t) throws IOException {
- int w = (int) (width * t);
- int h = (int) (height * t);
- resize(w, h);
- }
-
- /**
- * 以寬度為基準,等比例放縮圖片
- *
- * @param w
- * int 新寬度
- * @throws IOException
- */
- public void resizeByWidth(int w) throws IOException {
- int h = (int) (height * w / width);
- resize(w, h);
- }
-
- /**
- * 以高度為基準,等比例縮放圖片
- *
- * @param h
- * int 新高度
- * @throws IOException
- */
- public void resizeByHeight(int h) throws IOException {
- int w = (int) (width * h / height);
- resize(w, h);
- }
-
- /**
- * 按照最大高度限制,生成最大的等比例縮略圖
- *
- * @param w
- * int 最大寬度
- * @param h
- * int 最大高度
- * @throws IOException
- */
- public void resizeFix(int w, int h) throws IOException {
- if (width / height > w / h) {
- resizeByWidth(w);
- } else {
- resizeByHeight(h);
- }
- }
-
- /**
- * 設(shè)置目標文件名 setDestFile
- *
- * @param fileName
- * String 文件名字符串
- */
- public void setDestFile(String fileName) throws Exception {
- if (!fileName.endsWith(".jpg")) {
- throw new Exception("Dest File Must end with \".jpg\".");
- }
- destFile = fileName;
- }
-
- /**
- * 獲取目標文件名 getDestFile
- */
- public String getDestFile() {
- return destFile;
- }
-
- /**
- * 獲取圖片原始寬度 getSrcWidth
- */
- public int getSrcWidth() {
- return width;
- }
-
- /**
- * 獲取圖片原始高度 getSrcHeight
- */
- public int getSrcHeight() {
- return height;
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- DrawImage ccc = new DrawImage("D:/dage.jpg");
- ccc.resizeFix(600, 400);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
第二種:使用仿射轉(zhuǎn)換的技術(shù)進行圖片繪制。
- package com.image.suoluetu;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.awt.image.AffineTransformOp;
- import java.awt.geom.AffineTransform;
-
- public class AffineTransImage {
-
- public static void main (String argv[]) {
- try {
- File fi = new File("D:/dage.jpg"); //大圖文件
- File fo = new File("D:/dagex.jpg"); //將要轉(zhuǎn)換出的小圖文件
- int nw = 500;
- /*
- AffineTransform 類表示 2D 仿射變換,它執(zhí)行從 2D 坐標到其他 2D
- 坐標的線性映射,保留了線的“直線性”和“平行性”??梢允褂靡幌?nbsp;
- 列平移、縮放、翻轉(zhuǎn)、旋轉(zhuǎn)和剪切來構(gòu)造仿射變換。
- */
- AffineTransform transform = new AffineTransform();
- BufferedImage bis = ImageIO.read(fi); //讀取圖片
- int w = bis.getWidth();
- int h = bis.getHeight();
- //double scale = (double)w/h;
- int nh = (nw*h)/w ;
- double sx = (double)nw/w;
- double sy = (double)nh/h;
- transform.setToScale(sx,sy); //setToScale(double sx, double sy) 將此變換設(shè)置為縮放變換。
- System.out.println(w + " " +h);
- /*
- * AffineTransformOp類使用仿射轉(zhuǎn)換來執(zhí)行從源圖像或 Raster 中 2D 坐標到目標圖像或
- * Raster 中 2D 坐標的線性映射。所使用的插值類型由構(gòu)造方法通過
- * 一個 RenderingHints 對象或通過此類中定義的整數(shù)插值類型之一來指定。
- 如果在構(gòu)造方法中指定了 RenderingHints 對象,則使用插值提示和呈現(xiàn)
- 的質(zhì)量提示為此操作設(shè)置插值類型。要求進行顏色轉(zhuǎn)換時,可以使用顏色
- 呈現(xiàn)提示和抖動提示。 注意,務(wù)必要滿足以下約束:源圖像與目標圖像
- 必須不同。 對于 Raster 對象,源圖像中的 band 數(shù)必須等于目標圖像中
- 的 band 數(shù)。
- */
- AffineTransformOp ato = new AffineTransformOp(transform,null);
- BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
- /*
- * TYPE_3BYTE_BGR 表示一個具有 8 位 RGB 顏色分量的圖像,
- * 對應(yīng)于 Windows 風(fēng)格的 BGR 顏色模型,具有用 3 字節(jié)存
- * 儲的 Blue、Green 和 Red 三種顏色。
- */
- ato.filter(bis,bid);
- ImageIO.write(bid,"jpeg",fo);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- }
|