基于java流實(shí)現(xiàn)壓縮圖片過(guò)程解析
整理文檔,搜刮出一個(gè)Java做圖片壓縮的代碼,稍微整理精簡(jiǎn)一下做下分享。
首先,要壓縮的圖片格式不能說(shuō)動(dòng)態(tài)圖片,你可以使用bmp、png、gif等,至于壓縮質(zhì)量,可以通過(guò)BufferedImage來(lái)指定。
在C盤的temp下放置一張圖片pic123.jpg,盡量找一個(gè)像素高一點(diǎn)的圖片,這里我找了一張5616*3744的。
package test;import java.io.*;import java.util.Date;import java.awt.*;import java.awt.p_w_picpath.*;import javax.p_w_picpathio.ImageIO;import com.sun.p_w_picpath.codec.jpeg.*;/** * 圖片壓縮處理 * @author 崔素強(qiáng) */public class ImgCompress {private Image img;private int width;private int height;@SuppressWarnings('deprecation') public static void main(String[] args) throws Exception {System.out.println('開始:' + new Date().toLocaleString());ImgCompress imgCom = new ImgCompress('C:temppic123.jpg');imgCom.resizeFix(400, 400);System.out.println('結(jié)束:' + new Date().toLocaleString());}/** * 構(gòu)造函數(shù) */public ImgCompress(String fileName) throws IOException {File file = new File(fileName);// 讀入文件 img = ImageIO.read(file);// 構(gòu)造Image對(duì)象 width = img.getWidth(null);// 得到源圖寬 height = img.getHeight(null);// 得到源圖長(zhǎng)}/** * 按照寬度還是高度進(jìn)行壓縮 * @param w int 最大寬度 * @param h int 最大高度 */public void resizeFix(int w, int h) throws IOException {if (width / height > w / h) {resizeByWidth(w);} else {resizeByHeight(h);}}/** * 以寬度為基準(zhǔn),等比例放縮圖片 * @param w int 新寬度 */public void resizeByWidth(int w) throws IOException {int h = (int) (height * w / width);resize(w, h);}/** * 以高度為基準(zhǔn),等比例縮放圖片 * @param h int 新高度 */public void resizeByHeight(int h) throws IOException {int w = (int) (width * h / height);resize(w, h);}/** * 強(qiáng)制壓縮/放大圖片到固定的大小 * @param w int 新寬度 * @param h int 新高度 */public void resize(int w, int h) throws IOException {// SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優(yōu)先級(jí)比速度高 生成的圖片質(zhì)量比較好 但速度慢 BufferedImage p_w_picpath = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );p_w_picpath.getGraphics().drawImage(img, 0, 0, w, h, null);// 繪制縮小后的圖 File destFile = new File('C:temp456.jpg');FileOutputStream out = new FileOutputStream(destFile);// 輸出到文件流 // 可以正常實(shí)現(xiàn)bmp、png、gif轉(zhuǎn)jpg JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(p_w_picpath);// JPEG編碼 out.close();}}
運(yùn)行后在C盤temp下生成一個(gè)465.jpg,像素大小為600*400,像素大小是我指定的。用時(shí)也就是一兩秒的事情,注意,我這張圖片是10M的,壓縮后是40.5KB。
一些細(xì)節(jié)事項(xiàng)可以參考代碼中的注釋。
要注意的是,你可能想試一試較大圖片的處理能力,如果你的JDK沒(méi)有指定默認(rèn)內(nèi)存,那可能會(huì)有如下異常,因?yàn)閮?nèi)存不夠大:
Exception in thread 'main' java.lang.OutOfMemoryError: Java heap space at java.awt.p_w_picpath.DataBufferByte.<init>(DataBufferByte.java:58) at java.awt.p_w_picpath.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397) at java.awt.p_w_picpath.Raster.createWritableRaster(Raster.java:938) at javax.p_w_picpathio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1169) at javax.p_w_picpathio.ImageReader.getDestination(ImageReader.java:2879) at com.sun.p_w_picpathio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:943) at com.sun.p_w_picpathio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:915) at javax.p_w_picpathio.ImageIO.read(ImageIO.java:1422) at javax.p_w_picpathio.ImageIO.read(ImageIO.java:1282) at test.ImgCompress.<init>(ImgCompress.java:31) at test.ImgCompress.main(ImgCompress.java:21)
解決方法:
在Eclipse里選:Window->Preference->Installed JREs->Edit(選中jre),
在Default VM Arguments里輸入-Xms256m -Xmx1024m,表示最小內(nèi)存256M,最大1G,然后運(yùn)行就可以了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)3. XML在語(yǔ)音合成中的應(yīng)用4. Jsp servlet驗(yàn)證碼工具類分享5. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)6. jscript與vbscript 操作XML元素屬性的代碼7. 基于PHP做個(gè)圖片防盜鏈8. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)9. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)10. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解
