當前位置:首頁 » 文件管理 » png壓縮java

png壓縮java

發布時間: 2022-06-09 17:12:51

① 照片格式png 是什麼意思

png:攜帶型網路圖形是一種無損壓縮的點陣圖片形格式,其設計目的是試圖替代GIF和TIFF文件格式,同時增加一些GIF文件格式所不具備的特性。PNG使用從LZ77派生的無損數據壓縮演算法,一般應用於java程序、網頁或S60程序中,原因是它壓縮比高,生成文件體積小。

在PNG傳播過程中,很多網路瀏覽器經過很長時間才開始完全支持PNG格式,如Microsoft Windows默認的Internet Explorer瀏覽器一直到7.0版才支持PNG格式中的半透明效果,較早期的版本(如6.0 SP1)需要下載Hotfix 或由網站提供額外的Script去支持,這造成PNG格式並沒有得到廣泛的認知。



(1)png壓縮java擴展閱讀:

1995年早期,Unisys公司根據它在GIF格式中使用的LZW數據壓縮演算法的軟體專利開始商業收費,為避免專利影響,用於表現單張圖像的PNG、用於表現動畫的MNG圖形文件格式被同時創建出來。1999年8月,Unisys公司進一步中止了對自由軟體和非商用軟體開發者的GIF專利免費許可,從而使PNG格式獲得了更多的關注。

1996年6月提出PNF(Portable Network Frame)草案,當年8月改名為MNG(Multiple-image Network Graphics)。

② java 可以壓縮gif圖片嗎

不可以,目前只有用java轉化jpg、png、jpeg格式的文件,gif內部是用幀實現的,不能被壓縮。

③ 您好!請問用java怎麼將截取png的圖片中間一部分,以及如何壓縮一個png圖片

  • getSubimage方法是進行圖片裁剪。

舉例:
public static void main(String[] args) {
try {
//從特定文件載入
BufferedImage bi = ImageIO.read(new File("c:\test.png"));
bi.getSubimage(0, 0, 10, 10);//前兩個值是坐標位置X、Y,後兩個是長和寬
} catch (IOException e) {
e.printStackTrace();
}
}

  • 以下是進行的圖片壓縮,涉及到多個工具類。

/**
* 圖片工具類
* 壓縮圖片大小
* @author Cyw
* @version 1.0
*/
public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默認輸出圖片寬

private int outPutFileHeight = 100; // 默認輸出圖片高

private static boolean isScaleZoom = true; // 是否按比例縮放

public ZIPImage() {
outPutFilePath = "";
inPutFilePath = "";
inPutFileName = "";
autoBuildFileName = true;
outPutFileName = "";
}

/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = true;
outPutFileName = opfn;
}

/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
* @param aBFN
* 是否自動生成目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,
boolean aBFN) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = aBFN;
outPutFileName = opfn;
}

public boolean isAutoBuildFileName() {
return autoBuildFileName;
}

public void setAutoBuildFileName(boolean autoBuildFileName) {
this.autoBuildFileName = autoBuildFileName;
}

public String getInPutFilePath() {
return inPutFilePath;
}

public void setInPutFilePath(String inPutFilePath) {
this.inPutFilePath = inPutFilePath;
}

public String getOutPutFileName() {
return outPutFileName;
}

public void setOutPutFileName(String outPutFileName) {
this.outPutFileName = outPutFileName;
}

public String getOutPutFilePath() {
return outPutFilePath;
}

public void setOutPutFilePath(String outPutFilePath) {
this.outPutFilePath = outPutFilePath;
}

public int getOutPutFileHeight() {
return outPutFileHeight;
}

public void setOutPutFileHeight(int outPutFileHeight) {
this.outPutFileHeight = outPutFileHeight;
}

public int getOutPutFileWidth() {
return outPutFileWidth;
}

public void setOutPutFileWidth(int outPutFileWidth) {
this.outPutFileWidth = outPutFileWidth;
}

public boolean isScaleZoom() {
return isScaleZoom;
}

public void setScaleZoom(boolean isScaleZoom) {
this.isScaleZoom = isScaleZoom;
}

public String getInPutFileName() {
return inPutFileName;
}

public void setInPutFileName(String inPutFileName) {
this.inPutFileName = inPutFileName;
}

/**
* 壓縮圖片大小
*
* @return boolean
*/
public boolean compressImage() {
boolean flag = false;

try {
if (inPutFilePath.trim().equals("")) {
throw new NullPointerException("源文件夾路徑不存在。");
}
if (inPutFileName.trim().equals("")) {
throw new NullPointerException("圖片文件路徑不存在。");
}
if (outPutFilePath.trim().equals("")) {
throw new NullPointerException("目標文件夾路徑地址為空。");
} else {
if (!ZIPImage.mddir(outPutFilePath)) {
throw new FileNotFoundException(outPutFilePath
+ " 文件夾創建失敗!");
}
}

if (this.autoBuildFileName) { // 自動生成文件名
String tempFile[] = getFileNameAndExtName(inPutFileName);
outPutFileName = tempFile[0] + "_cyw." + tempFile[1];
compressPic();
} else {
if (outPutFileName.trim().equals("")) {
throw new NullPointerException("目標文件名為空。");
}
compressPic();
}

} catch (Exception e) {
flag = false;
e.printStackTrace();
return flag;
}

return flag;
}

// 圖片處理
private void compressPic() throws Exception {
try {
// 獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
throw new Exception("文件不可讀!");
} else {
int newWidth;
int newHeight;
// 判斷是否是等比縮放
if (ZIPImage.isScaleZoom == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) outPutFileWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) outPutFileHeight + 0.1;

// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {
newWidth = outPutFileWidth; // 輸出的圖片寬度
newHeight = outPutFileHeight; // 輸出的圖片高度
}

BufferedImage tag = new BufferedImage((int) newWidth,
(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*
* Image.SCALE_SMOOTH 的縮略演算法 生成縮略圖片的平滑度的 優先順序比速度高 生成的圖片質量比較好 但速度慢
*/
tag.getGraphics().drawImage(
img.getScaledInstance(newWidth, newHeight,
Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath
+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();

}
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* 創建文件夾目錄
*
* @param filePath
* @return
* @throws Exception
*/
@SuppressWarnings("unused")
private static boolean mddir(String filePath) throws Exception {
boolean flag = false;
File f = new File(filePath);
if (!f.exists()) {
flag = f.mkdirs();
} else {
flag = true;
}
return flag;
}

/**
* 獲得文件名和擴展名
*
* @param fullFileName
* @return
* @throws Exception
*/
private String[] getFileNameAndExtName(String fullFileName)
throws Exception {
String[] fileNames = new String[2];
if (fullFileName.indexOf(".") == -1) {
throw new Exception("源文件名不正確!");
} else {
fileNames[0] = fullFileName.substring(0, fullFileName
.lastIndexOf("."));
fileNames[1] = fullFileName
.substring(fullFileName.lastIndexOf(".") + 1);
}
return fileNames;
}

public Image getSourceImage() throws IOException{
//獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
return img;
}

/*
* 獲得圖片大小
* @path :圖片路徑
*/
public long getPicSize(String path) {
File file = new File(path);
return file.length();
}

}

//下面是測試程序

package com.sun.util.cyw;

import java.awt.Image;
import java.io.IOException;

public class ImageTest {
public static void main(String[] args) throws IOException {
ZIPImage zip=new ZIPImage("d:\","1.jpg","d:\test\","處理後的圖片.jpg",false);
zip.setOutPutFileWidth(1000);
zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();
long size=zip.getPicSize("d:\1.jpg");
System.out.println("處理前的圖片大小 width:"+image.getWidth(null));
System.out.println("處理前的圖片大小 height:"+image.getHeight(null));
System.out.println("處理前的圖片容量:"+ size +" bit");

zip.compressImage();
}
}

④ png是什麼格式

png是一種採用無損壓縮演算法的點陣圖格式,其設計目的是試圖替代GIF和TIFF文件格式,同時增加一些GIF文件格式所不具備的特性。PNG使用從LZ77派生的無損數據壓縮演算法,一般應用於JAVA程序、網頁或S60程序中,原因是它壓縮比高,生成文件體積小。

發展:

PNG現行版本是國際標准(ISO/IEC 15948:2003),並在2003年11月10日作為W3C建議發布。這個版本與1.2版僅有細微差別。

2004年末,PNG的動畫擴展——APNG,被提出來。這是一個相對於MNG更簡單的動畫實現方案,不識別APNG格式的PNG解碼器至少能夠正常回放第一幅普通PNG畫面。

⑤ png圖片什麼意思

png是一種採用無損壓縮演算法的點陣圖格式。

png其設計目的是試圖替代GIF和TIFF文件格式,同時增加一些GIF文件格式所不具備的特性。PNG使用從LZ77派生的無損數據壓縮演算法,一般應用於JAVA程序、網頁或S60程序中,原因是它壓縮比高,生成文件體積小。

PNG同時還支持真彩和灰度級圖像的Alpha通道透明度。最高支持24位真彩色圖像以及8位灰度圖像。支持Alpha通道的透明/半透明特性。支持圖像亮度的Gamma校準信息。支持存儲附加文本信息,以保留圖像名稱、作者、版權、創作時間、注釋等信息。

具備特點:

1、體積小:

網路通訊中因受帶寬制約,在保證圖片清晰、逼真的前提下,網頁中不可能大范圍的使用文件較大的bmp格式文件。

2、無損壓縮:

PNG文件採用LZ77演算法的派生演算法進行壓縮,其結果是獲得高的壓縮比,不損失數據。它利用特殊的編碼方法標記重復出現的數據,因而對圖像的顏色沒有影響,也不可能產生顏色的損失,這樣就可以重復保存而不降低圖像質量。

3、更優化的網路傳輸顯示:

PNG圖像在瀏覽器上採用流式瀏覽,即使經過交錯處理的圖像會在完全下載之前提供瀏覽者一個基本的圖像內容,然後再逐漸清晰起來。它允許連續讀出和寫入圖像數據,這個特性很適合於在通信過程中顯示和生成圖像。

⑥ PNG是什麼意思

攜帶型網路圖形(英語:PortableNetworkGraphics,PNG)是一種支持無損壓縮的點陣圖圖形格式,支持索引、灰度、RGB三種顏色方案以及Alpha通道等特性。

特性

體積小網路通訊中因受帶寬制約,在保證圖片清晰、逼真的前提下,網頁中不可能大范圍的使用文件較大的bmp格式文件。

無損壓縮PNG文件採用LZ77演算法的派生演算法進行壓縮,其結果是獲得高的壓縮比,不損失數據。它利用特殊的編碼方法標記重復出現的數據,因而對圖像的顏色沒有影響,也不可能產生顏色的損失,這樣就可以重復保存而不降低圖像質量。

索引彩色模式PNG-8格式與GIF圖像類似,同樣採用8位調色板將RGB彩色圖像轉換為索引彩色圖像。圖像中保存的不再是各個像素的彩色信息,而是從圖像中挑選出來的具有代表性的顏色編號,每一編號對應一種顏色,圖像的數據量也因此減少,這對彩色圖像的傳播非常有利。

更優化的網路傳輸顯示PNG圖像在瀏覽器上採用流式瀏覽,即使經過交錯處理的圖像會在完全下載之前提供瀏覽者一個基本的圖像內容,然後再逐漸清晰起來。它允許連續讀出和寫入圖像數據,這個特性很適合於在通信過程中顯示和生成圖像。

支持透明效果PNG可以為原圖像定義256個透明層次,使得彩色圖像的邊緣能與任何背景平滑地融合,從而徹底地消除鋸齒邊緣。這種功能是GIF和JPEG沒有的。

PNG同時還支持真彩和灰度級圖像的Alpha通道透明度。最高支持24位真彩色圖像以及8位灰度圖像。支持Alpha通道的透明/半透明特性。支持圖像亮度的Gamma校準信息。支持存儲附加文本信息,以保留圖像名稱、作者、版權、創作時間、注釋等信息。

與JPEG相比

JPEG可以對照片(或類似)圖像生成更小的文件,這是由於JPEG採用了一種針對照片圖像的特定有損編碼方法,這種編碼適用於低對比,圖像顏色過渡平滑,雜訊多,且結構不規則的情況下。如果在這種情況下用PNG代替JPEG,文件尺寸增大很多,而圖像質量的提高有限。

相應的,如果保存文本,線條或類似的邊緣清晰,有大塊相同顏色區域的圖像,PNG格式的壓縮效果就要比JPEG好很多,並且不會出現JPEG那樣的高對比度區域的圖像有損。如果圖像既有清晰邊緣,又有照片圖像的特點,就需要在這兩種格式之間權衡一下了。JPEG不支持透明度。

由於JPEG是有損壓縮,會產生迭代有損,在重復壓縮和解碼的過程中會不斷丟失信息使圖像質量下降。由於PNG是無損的,保存將要被編輯的圖像來說更加合適。

雖然PNG壓縮照片圖像也有效,但有專門針對照片圖像設計的無損壓縮格式,比如無損JPEG2000,Adobe DNG(頁面存檔備份,存於互聯網檔案館)等。

總的來說這些格式都不能做到適用所有圖像。對於將要發布的圖像可以保存成JPEG,用JPEG編碼一次不會造成明顯的圖像有損。

以上內容參考網路-PNG

⑦ java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變

也就是圖片壓縮,可以不修改任何大小的壓縮(速度快),也可等比例修改大小壓縮(較慢)
下面這是一段等比例縮小圖片的方法。

public String compressPic(String inputDir, String outputDir,
String inputFileName, String outputFileName, int width,
int height, boolean gp,String hzm) {
try {
if (!image.exists()) {
return "";
}
Image img = ImageIO.read(image);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
return "no";
} else {
int newWidth; int newHeight;
// 判斷是否是等比縮放
if (gp == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null)) / (double) width ;
double rate2 = ((double) img.getHeight(null)) / (double) height ;
// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = img.getWidth(null); // 輸出的圖片寬度
newHeight = img.getHeight(null); // 輸出的圖片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

/*
* Image.SCALE_SMOOTH 的縮略演算法 生成縮略圖片的平滑度的
* 優先順序比速度高 生成的圖片質量比較好 但速度慢
*/
Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(im, 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
//JPEGImageEncoder可適用於其他圖片類型的轉換
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}

⑧ 求助java壓縮圖片存儲大小的方法

可以使用Draw這個類,通過改變像素來改變存儲大小,實例如下:

(StringsrcFilePath,StringdescFilePath)throwsIOException{
Filefile=null;
BufferedImagesrc=null;
FileOutputStreamout=null;
ImageWriterimgWrier;
ImageWriteParamimgWriteParams;

//指定寫圖片的方式為jpg
imgWrier=ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams=newjavax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
//要使用壓縮,必須指定壓縮方式為MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
//這里指定壓縮的程度,參數qality是取值0~1范圍內,
imgWriteParams.setCompressionQuality((float)1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModelcolorModel=ImageIO.read(newFile(srcFilePath)).getColorModel();//ColorModel.getRGBdefault();
//指定壓縮時使用的色彩模式
//imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
//colorModel,colorModel.createCompatibleSampleModel(16,16)));
imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
colorModel,colorModel.createCompatibleSampleModel(16,16)));

try{
if(isBlank(srcFilePath)){
returnfalse;
}else{
file=newFile(srcFilePath);System.out.println(file.length());
src=ImageIO.read(file);
out=newFileOutputStream(descFilePath);

imgWrier.reset();
//必須先指定out值,才能調用write方法,ImageOutputStream可以通過任何
//OutputStream構造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
//調用write方法,就可以向輸入流寫圖片
imgWrier.write(null,newIIOImage(src,null,null),
imgWriteParams);
out.flush();
out.close();
}
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
publicstaticbooleanisBlank(Stringstring){
if(string==null||string.length()==0||string.trim().equals("")){
returntrue;
}
returnfalse;
}

⑨ PNG 格式用什麼軟體打開

PNG文件可以使用WINDOWS自帶的圖片查看器、網頁瀏覽器、畫圖工具、PHOTOSHOP打開。

PNG格式打開步驟:

1、找到需要打開操作的PNG格式文件。

⑩ java壓縮png圖片

您轉換的是圖片的後綴名吧?您這樣的方式已經把圖片的信息刪除了!
http://sjbbs.zol.com.cn/1/313_9068.html您去下載一個java圖片壓縮器吧
或者直接在Java下編輯代碼來實習轉換
package com.sun.util.cyw;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* 圖片工具類
* 壓縮圖片大小
* @author Cyw
* @version 1.0
*/
public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默認輸出圖片寬

private int outPutFileHeight = 100; // 默認輸出圖片高

private static boolean isScaleZoom = true; // 是否按比例縮放

public ZIPImage() {
outPutFilePath = "";
inPutFilePath = "";
inPutFileName = "";
autoBuildFileName = true;
outPutFileName = "";
}

/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = true;
outPutFileName = opfn;
}

/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
* @param aBFN
* 是否自動生成目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,
boolean aBFN) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = aBFN;
outPutFileName = opfn;
}

public boolean isAutoBuildFileName() {
return autoBuildFileName;
}

public void setAutoBuildFileName(boolean autoBuildFileName) {
this.autoBuildFileName = autoBuildFileName;
}

public String getInPutFilePath() {
return inPutFilePath;
}

public void setInPutFilePath(String inPutFilePath) {
this.inPutFilePath = inPutFilePath;
}

public String getOutPutFileName() {
return outPutFileName;
}

public void setOutPutFileName(String outPutFileName) {
this.outPutFileName = outPutFileName;
}

public String getOutPutFilePath() {
return outPutFilePath;
}

public void setOutPutFilePath(String outPutFilePath) {
this.outPutFilePath = outPutFilePath;
}

public int getOutPutFileHeight() {
return outPutFileHeight;
}

public void setOutPutFileHeight(int outPutFileHeight) {
this.outPutFileHeight = outPutFileHeight;
}

public int getOutPutFileWidth() {
return outPutFileWidth;
}

public void setOutPutFileWidth(int outPutFileWidth) {
this.outPutFileWidth = outPutFileWidth;
}

public boolean isScaleZoom() {
return isScaleZoom;
}

public void setScaleZoom(boolean isScaleZoom) {
this.isScaleZoom = isScaleZoom;
}

public String getInPutFileName() {
return inPutFileName;
}

public void setInPutFileName(String inPutFileName) {
this.inPutFileName = inPutFileName;
}

/**
* 壓縮圖片大小
*
* @return boolean
*/
public boolean compressImage() {
boolean flag = false;

try {
if (inPutFilePath.trim().equals("")) {
throw new NullPointerException("源文件夾路徑不存在。");
}
if (inPutFileName.trim().equals("")) {
throw new NullPointerException("圖片文件路徑不存在。");
}
if (outPutFilePath.trim().equals("")) {
throw new NullPointerException("目標文件夾路徑地址為空。");
} else {
if (!ZIPImage.mddir(outPutFilePath)) {
throw new FileNotFoundException(outPutFilePath
+ " 文件夾創建失敗!");
}
}

if (this.autoBuildFileName) { // 自動生成文件名
String tempFile[] = getFileNameAndExtName(inPutFileName);
outPutFileName = tempFile[0] + "_cyw." + tempFile[1];
compressPic();
} else {
if (outPutFileName.trim().equals("")) {
throw new NullPointerException("目標文件名為空。");
}
compressPic();
}

} catch (Exception e) {
flag = false;
e.printStackTrace();
return flag;
}

return flag;
}

// 圖片處理
private void compressPic() throws Exception {
try {
// 獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
throw new Exception("文件不可讀!");
} else {
int newWidth;
int newHeight;
// 判斷是否是等比縮放
if (ZIPImage.isScaleZoom == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) outPutFileWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) outPutFileHeight + 0.1;

// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {
newWidth = outPutFileWidth; // 輸出的圖片寬度
newHeight = outPutFileHeight; // 輸出的圖片高度
}

BufferedImage tag = new BufferedImage((int) newWidth,
(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*
* Image.SCALE_SMOOTH 的縮略演算法 生成縮略圖片的平滑度的 優先順序比速度高 生成的圖片質量比較好 但速度慢
*/
tag.getGraphics().drawImage(
img.getScaledInstance(newWidth, newHeight,
Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath
+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();

}
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* 創建文件夾目錄
*
* @param filePath
* @return
* @throws Exception
*/
@SuppressWarnings("unused")
private static boolean mddir(String filePath) throws Exception {
boolean flag = false;
File f = new File(filePath);
if (!f.exists()) {
flag = f.mkdirs();
} else {
flag = true;
}
return flag;
}

/**
* 獲得文件名和擴展名
*
* @param fullFileName
* @return
* @throws Exception
*/
private String[] getFileNameAndExtName(String fullFileName)
throws Exception {
String[] fileNames = new String[2];
if (fullFileName.indexOf(".") == -1) {
throw new Exception("源文件名不正確!");
} else {
fileNames[0] = fullFileName.substring(0, fullFileName
.lastIndexOf("."));
fileNames[1] = fullFileName
.substring(fullFileName.lastIndexOf(".") + 1);
}
return fileNames;
}

public Image getSourceImage() throws IOException{
//獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
return img;
}

/*
* 獲得圖片大小
* @path :圖片路徑
*/
public long getPicSize(String path) {
File file = new File(path);
return file.length();
}

}

//下面是測試程序

package com.sun.util.cyw;

import java.awt.Image;
import java.io.IOException;

public class ImageTest {
public static void main(String[] args) throws IOException {
ZIPImage zip=new ZIPImage("d:\\","1.jpg","d:\\test\\","處理後的圖片.jpg",false);
zip.setOutPutFileWidth(1000);
zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();
long size=zip.getPicSize("d:\\1.jpg");
System.out.println("處理前的圖片大小 width:"+image.getWidth(null));
System.out.println("處理前的圖片大小 height:"+image.getHeight(null));
System.out.println("處理前的圖片容量:"+ size +" bit");

zip.compressImage();
}
}

熱點內容
只編譯一個c文件 發布:2024-09-28 09:54:39 瀏覽:237
指紋密碼怎麼破 發布:2024-09-28 09:45:11 瀏覽:661
自編自選腳本 發布:2024-09-28 09:45:10 瀏覽:932
androidui教程pdf 發布:2024-09-28 09:44:13 瀏覽:899
iphone排列文件夾 發布:2024-09-28 09:30:46 瀏覽:355
安卓用什麼自拍 發布:2024-09-28 09:20:14 瀏覽:384
python聲明列表 發布:2024-09-28 08:54:52 瀏覽:15
全自動密碼指紋鎖哪裡批發 發布:2024-09-28 08:54:51 瀏覽:608
移動服務密碼怎麼查詢 發布:2024-09-28 08:54:47 瀏覽:479
怎麼配置一台主機 發布:2024-09-28 08:42:02 瀏覽:323