程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java 圖片緊縮完成思緒及代碼

Java 圖片緊縮完成思緒及代碼

編輯:關於JAVA

Java 圖片緊縮完成思緒及代碼。本站提示廣大學習愛好者:(Java 圖片緊縮完成思緒及代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java 圖片緊縮完成思緒及代碼正文


Java圖片緊縮代碼

package com.img;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
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 可樂加糖
*/
public class CompressPicDemo {
private File file = null; // 文件對象
private String inputDir; // 輸出圖途徑
private String outputDir; // 輸入圖途徑
private String inputFileName; // 輸出圖文件名
private String outputFileName; // 輸入圖文件名
private int outputWidth = 100; // 默許輸入圖片寬
private int outputHeight = 100; // 默許輸入圖片高
private boolean proportion = true; // 能否等比縮放標志(默許為等比縮放)
public CompressPicDemo() { // 初始化變量
inputDir = "";
outputDir = "";
inputFileName = "";
outputFileName = "";
outputWidth = 100;
outputHeight = 100;
}
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 setWidthAndHeight(int width, int height) {
this.outputWidth = width;
this.outputHeight = height;
}
/*
* 取得圖片年夜小
* 傳入參數 String path :圖片途徑
*/
public long getPicSize(String path) {
file = new File(path);
return file.length();
}
// 圖片處置
public String compressPic() {
try {
//取得源文件
file = new File(inputDir + inputFileName);
if (!file.exists()) {
return "";
}
Image img = ImageIO.read(file);
// 斷定圖片格局能否准確
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
} else {
int newWidth; int newHeight;
// 斷定能否是等比縮放
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;
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 的縮略算法 生成縮略圖片的膩滑度的
* 優先級比速度高 生成的圖片質量比擬好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 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";
}
public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
// 輸出圖途徑
this.inputDir = inputDir;
// 輸入圖途徑
this.outputDir = outputDir;
// 輸出圖文件名
this.inputFileName = inputFileName;
// 輸入圖文件名
this.outputFileName = outputFileName;
return compressPic();
}
public String compressPic(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;
// 設置圖片長寬
setWidthAndHeight(width, height);
// 能否是等比縮放 標志
this.proportion = gp;
return compressPic();
}
// main測試
// compressPic(年夜圖片途徑,生成小圖片途徑,年夜圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度,能否等比縮放(默許為true))
public static void main(String[] arg) {
CompressPicDemo mypic = new CompressPicDemo();
System.out.println("輸出的圖片年夜小:" + mypic.getPicSize("e:\\1.jpg")/1024 + "KB");
mypic.compressPic("e:\\", "e:\\test\\", "1.jpg", "r1.jpg", 120, 120, false);
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved