麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

Java 圖片壓縮實現思路及代碼

2019-11-26 16:02:58
字體:
來源:轉載
供稿:網友
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);
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美性生视频 | 欧美a一 | 久久国产秒| www69xxxxx| 性欧美日本| 日本黄色a视频 | 九九精品在线播放 | 黄色片在线观看网站 | 国产精品免费大片 | 狠狠久久伊人中文字幕 | 久久久久9999 | 色999久久久精品人人澡69 | av电影在线播放 | 亚洲一区免费观看 | 2021狠狠操| 高清在线观看av | 中文字幕在线观看国产 | av成人免费在线观看 | 一级α片免费看刺激高潮视频 | 欧美日韩观看 | 久久免费视频5 | 成人福利软件 | 黄色av片在线观看 | 亚洲国产高清一区 | 亚洲午夜精选 | 全视频tv| 一本在线高清码电影 | 成品片a免人视频 | 欧美wwwwww | 97伦理| 中文字幕网站在线 | 欧美日韩国产综合网 | 亚洲白嫩在线观看 | 精品一区二区久久久久久按摩 | 毛片在线看免费 | 九九午夜 | 免费久久久久 | 国产一级毛片国语版 | 日韩免费黄色 | 国产精品毛片va一区二区三区 | 全黄性性激高免费视频 |