方法一:
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class bbb {
public static void main (String argv[]) {
try {
File fi = new File("c:/image2.jpg"); //大圖文件
File fo = new File("c:/imgTest.jpg"); //將要轉(zhuǎn)換出的小圖文件
int nw = 100;
/*
AffineTransform 類表示 2D 仿射變換,它執(zhí)行從 2D 坐標(biāo)到其他 2D
坐標(biāo)的線性映射,保留了線的“直線性”和“平行性”。可以使用一系
列平移、縮放、翻轉(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 坐標(biāo)到目標(biāo)圖像或
* Raster 中 2D 坐標(biāo)的線性映射。所使用的插值類型由構(gòu)造方法通過
* 一個(gè) RenderingHints 對(duì)象或通過此類中定義的整數(shù)插值類型之一來指定。
如果在構(gòu)造方法中指定了 RenderingHints 對(duì)象,則使用插值提示和呈現(xiàn)
的質(zhì)量提示為此操作設(shè)置插值類型。要求進(jìn)行顏色轉(zhuǎn)換時(shí),可以使用顏色
呈現(xiàn)提示和抖動(dòng)提示。 注意,務(wù)必要滿足以下約束:源圖像與目標(biāo)圖像
必須不同。 對(duì)于 Raster 對(duì)象,源圖像中的 band 數(shù)必須等于目標(biāo)圖像中
的 band 數(shù)。
*/
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
/*
* TYPE_3BYTE_BGR 表示一個(gè)具有 8 位 RGB 顏色分量的圖像,
* 對(duì)應(yīng)于 Windows 風(fēng)格的 BGR 顏色模型,具有用 3 字節(jié)存
* 儲(chǔ)的 Blue、Green 和 Red 三種顏色。
*/
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
方法二:
import java.io.*;import java.awt.*;
import java.awt.image.*;import com.sun.image.codec.jpeg.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)2007-6-13</p>
* <p>Company: fuen</p>
* @author 楊振朋
* @version 1.0
*/
public class ccc {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;
/**
* 構(gòu)造函數(shù)
* @param fileName String
* @throws IOException
*/
public ccc(String fileName) throws IOException {
File _file = new File(fileName); //讀入文件
this.srcFile = _file.getName();
this.destFile = "c:/aa.jpg";//this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
img = javax.imageio.ImageIO.read(_file); //構(gòu)造Image對(duì)象
width = img.getWidth(null); //得到源圖寬
height = img.getHeight(null); //得到源圖長(zhǎng)
} /**
* 強(qiáng)制壓縮/放大圖片到固定的大小
* @param w int 新寬度
* @param h int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
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 對(duì)象中設(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 實(shí)現(xiàn)的軟件中得到它們。我們期望相同的功能最終可以在核心 API 或標(biāo)準(zhǔn)擴(kuò)
展中得到。
*/
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimageout);
encoder.encode(_image); //近JPEG編碼
newimageout.close();
} /**
* 按照固定的比例縮放圖片
* @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);
} /**
* 以寬度為基準(zhǔn),等比例放縮圖片
* @param w int 新寬度
* @throws IOException
*/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
} /**
* 以高度為基準(zhǔn),等比例縮放圖片
* @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è)置目標(biāo)文件名
* 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;
} /**
* 獲取目標(biāo)文件名
* getDestFile
*/
public String getDestFile() {
return destFile;
} /**
* 獲取圖片原始寬度
* getSrcWidth
*/
public int getSrcWidth() {
return width;
}
/**
* 獲取圖片原始高度
* getSrcHeight
*/
public int getSrcHeight() {
return height;
}
/*
* 調(diào)用測(cè)試
*/
public static void main(String[] args) throws Exception {
ccc ccc = new ccc("c:/image2.jpg");
ccc.resizeFix(500, 300);
}
}
posted on 2008-05-08 13:26
Hank1026 閱讀(7534)
評(píng)論(1) 編輯 收藏