java學習筆記(中級篇)—java實現高質量圖片壓縮


使用java幾十行代碼實現一個高質量圖片壓縮程序,再也不用去自己找網絡的壓縮程序啦!而且很多網上的工具還有水印或者其他的限制,自己動手寫一個簡單的應用,是再合適不過了。

一、實現原理

1、聲明兩個字符串變量,分別是要壓縮圖片的路徑和壓縮后圖片的存放路徑

private String brfore_image_path = "D:\\01.jpg";
private String after_image_path = "D:\\temp";

2、利用字符串的方法lastIndexOf,找到\和.最后出現的位置,目的是匹配到圖片文件名。

int begin = brfore_image_path.lastIndexOf("\\");
int end = brfore_image_path.lastIndexOf(".");
String image_name=brfore_image_path.substring(begin+1,end);

3、創建BufferedImage對象來讀取需要壓縮的圖片

4、獲取原始圖片的一系列參數

int in_width  = bi.getWidth();//圖寬
int in_height = bi.getHeight();//圖高
int in_minx   = bi.getMinX();//BufferedImage的最小x
int in_miny   = bi.getMinY();//BufferedImage的最小y
int type = bi.getType();//返回圖像類型
int out_width = in_width;//要輸出圖像的寬
int out_height = in_height;//要輸出圖像的高
int multiple = 1;//系數

5、壓縮核心代碼,可自己調試找最適合的臨界值,我選取的是大於1000000像素點時就壓縮一半

while(out_width * out_height > 1000000){
	out_width = out_width/2;
	out_height = out_height/2;
	multiple = multiple * 2;
}	

6、創建新的BufferedImage對象,把新的參數傳進去,並根據系數把一個個像素點寫進圖片。

for(int i=0;i<out_width;i++) {
	for(int j =0;j<out_height;j++) {      intpixel=bi.getRGB(i*multiple+in_minx,j*multiple+in_miny);
 ut_image_martrix.setRGB(i, j, pixel);
	}
}

7、把新的BufferedImage對象寫到你要保存壓縮圖片的地址就好了。

二、完整代碼

public class CompressImage {
	private String brfore_image_path = "D:\\01.jpg";
    private String after_image_path = "D:\\temp";
	
	public CompressImage(){
	}

	public void get_image(){
		int begin = brfore_image_path.lastIndexOf("\\");
		int end = brfore_image_path.lastIndexOf(".");
		String image_name = brfore_image_path.substring(begin+1,end);

		File in_file = new File(brfore_image_path);
		BufferedImage bi = null;
		try {
			bi = ImageIO.read(in_file);
		}catch(Exception e) {
			e.printStackTrace();
		}
		int in_width  = bi.getWidth();
		int in_height = bi.getHeight();
		int in_minx   = bi.getMinX();
		int in_miny   = bi.getMinY();
		int type = bi.getType();
		int out_width = in_width;
		int out_height = in_height;
		int multiple = 1;
		
		//具體的值可調
		while(out_width * out_height > 1000000){
			out_width = out_width/2;
			out_height = out_height/2;
			multiple = multiple * 2;
		}	
		BufferedImage out_image_martrix = new 		     BufferedImage(out_width, out_height, type);
		for(int i=0;i<out_width;i++) {
			for(int j =0;j<out_height;j++) {
				int pixel =bi.getRGB(i*multiple+in_minx, j*multiple+in_miny);
				out_image_martrix.setRGB(i, j, pixel);
			}
		}
		try{
			after_image_path = after_image_path + image_name + ".jpg";
			ImageIO.write(out_image_martrix,"jpg", new File(new_path));
			bi = null;
			out_image_martrix = null;
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//測試代碼
	public static void main(String[] args) {
		new CompressImage().get_image();
	}
}

三、總結

代碼挺簡單的,但是自己動手實現完成一個小功能也不一樣哦,而且我覺得壓縮的質量還挺高的,所以把自己的實現思路和代碼分享出來。有興趣的童鞋可以自己復制上面的完整代碼,只要改成自己的路徑就可以運行了。當然啦,幾行代碼無法媲美專業的壓縮工具啦~
最后,喜歡我文章的小伙伴就關注一下我的公眾號吧~
長按識別二維碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM