Java——批量更改圖片畫素(大小)
阿新 • • 發佈:2019-01-22
在小程式開發中,資源載入會影響介面繪製;假如網路狀態不夠好,
很可能會引起初始化介面為空,直到圖片載入完成才顯示整個介面。我們知道,小程式程式碼及資源本身的限制為2MB,快取限制為10MB,
因此可以考慮將列表項所需的大量圖片通過更改畫素的方式大大減小其大小。
例如一張2MB的圖片可以縮小至2KB。那麼如何批量更改大量圖片的畫素又不改變其比例呢?
作為一個程式設計師當然要用程式碼來實現,在此介紹一下如何通過Java更改圖片畫素
題外話
在Windows系統獲取的檔案路徑由於其中的\
往往不能直接在程式碼中使用,
因此介紹一下如何替換字串中的\
對String物件使用
.replaceAll("\\\\","/")
即可將所有\
替換為/
,進而方便檔案的使用。
· 獲取圖片畫素
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResizer {
...
/**
* 功能:獲取圖片畫素
* * @param filePath 圖片路徑
*/
public static void getPixel(String filePath){
File file = new File(filePath);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth(); // 畫素
int height = bi.getHeight(); // 畫素
System.out.println("width=" + width + ",height=" + height + ".");
}
...
public static void main(String []args){
getPixel("G:/蝴蝶識別/壓縮/首圖/Acraea terpsicore0.jpg");
}
}
結果:
width=450,height=470.
· 更改圖片畫素
按圖片的原比例進行修改:
/**
* @param srcPath 源圖片路徑
* @param desPath 修改大小後圖片路徑
* @param scaleSize 圖片的修改比例,目標寬度
*/
public static void resizeImage(String srcPath, String desPath,int scaleSize) throws IOException {
File srcFile = new File(srcPath);
Image srcImg = ImageIO.read(srcFile);
BufferedImage bi = null;
try {
bi = ImageIO.read(srcFile);
} catch (Exception e) {
e.printStackTrace();
}
float width = bi.getWidth(); // 畫素
float height = bi.getHeight(); // 畫素
float scale=width/scaleSize;
BufferedImage buffImg = null;
buffImg = new BufferedImage(scaleSize, (int)(height/scale), BufferedImage.TYPE_INT_RGB);
//使用TYPE_INT_RGB修改的圖片會變色
buffImg.getGraphics().drawImage(
srcImg.getScaledInstance(scaleSize, (int)(height/scale), Image.SCALE_SMOOTH), 0,
0, null);
ImageIO.write(buffImg, "JPEG", new File(desPath));
}
public static void main(String []args) throws IOException{
getFiles("G:/蝴蝶識別/壓縮/shoutu","modified_100",100);//將圖片壓縮至100寬
}
按自定義寬高修改:
/**
*
* @param srcPath 原圖片路徑
* @param desPath 轉換大小後圖片路徑
* @param width 轉換後圖片寬度
* @param height 轉換後圖片高度
*/
public static void resizeImage(String srcPath, String desPath,
int width, int height) throws IOException {
File srcFile = new File(srcPath);
Image srcImg = ImageIO.read(srcFile);
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//使用TYPE_INT_RGB修改的圖片會變色
buffImg.getGraphics().drawImage(
srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,
0, null);
ImageIO.write(buffImg, "JPEG", new File(desPath));
}
批量修改:
/**
* @param srcPath 源圖片資料夾路徑
* @param desPath 修改大小後圖片資料夾路徑
* @param scaleSize 圖片的修改比例,目標寬度
*/
public static void getFiles(String path,String modPath,int scaleSize) throws IOException {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
//迴圈讀取目錄下圖片
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("檔案:" + tempList[i].getName()+"-"+tempList[i].getAbsolutePath().replaceAll("\\\\","/"));
String[] imagePath=tempList[i].getAbsolutePath().replaceAll("\\\\","/").split("/");
String imageNumber=null;
ImageResizer.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\","/"),"目標檔案路徑",scaleSize);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
// System.out.println("資料夾:" + tempList[i]);
}
}
System.out.println(path+"下檔案數量:"+files.size());
}
修改效果:
修改前:
修改後: