日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android圖片壓縮技巧

Android圖片壓縮技巧

來源:程序員人生   發布時間:2014-12-09 08:39:31 閱讀次數:2850次

請尊重他人的勞動成果,轉載請注明出處:Android圖片緊縮技能

http://blog.csdn.net/fengyuzhengfan/article/details/41759835

當需要將Android客戶真個圖片上傳到服務器時,常常需要將圖片進行緊縮,關于圖片的緊縮方法,小編分享幾種經常使用的方式:

第1種方式:裁切以到達緊縮的目的

我曾在《Android開發之裁剪照片》1文中詳細介紹過如何裁切照片,感興趣的朋友可以去看1下。


第2種方式:將圖片進行降質處理(即下降圖片的質量)以到達緊縮的目的

這類方式也是比較經常使用的方式,下面就為大家介紹如何對圖片進行降質:

將圖片降質我們可使用Bitmap的這個方法:boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

其中,參數format表示緊縮后的格式,quality緊縮后的圖片質量(0表示最低,100表示不緊縮),stream表示要將緊縮后的圖片保存到的輸出流。

下面是詳細代碼:

/** * 多線程緊縮圖片的質量 * @author JPH * @param bitmap 內存中的圖片 * @param imgPath 圖片的保存路徑 * @date 2014⑴2⑸下午11:30:43 */ public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){ new Thread(new Runnable() {//開啟多線程進行緊縮處理 @Override public void run() { // TODO Auto-generated method stub ByteArrayOutputStream baos = new ByteArrayOutputStream(); options = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//質量緊縮方法,把緊縮后的數據寄存到baos中 (100表示不緊縮,0表示緊縮到最小) while (baos.toByteArray().length / 1024 > 100) {//循環判斷如果緊縮后圖片是不是大于100kb,大于繼續緊縮 baos.reset();//重置baos即讓下1次的寫入覆蓋之前的內容 options -= 10;//圖片質量每次減少10 if(options<0)options=0;//如果圖片質量小于10,則將圖片的質量緊縮到最小值 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//將緊縮后的圖片保存到baos中 if(options==0)break;//如果圖片的質量已降到最低則,不再進行緊縮 } try { FileOutputStream fos = new FileOutputStream(new File(imgPath));//將緊縮后的圖片保存的本地上指定路徑中 fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }

方法解析:

由于此方法中包括I/O操作和遞歸調用比較耗時所以我采取了多線程去處理。


第3種方式:按比例縮小圖片的像素以到達緊縮的目的

此種方法主要是使用android.graphics.BitmapFactory.Options.Options()方法將圖片以指定的采取率加載到內存然后輸出到本地以到達緊縮像素的目的。

詳細代碼:

/** * 按比例縮小圖片的像素以到達緊縮的目的 * @author JPH * @param imgPath * @date 2014⑴2⑸下午11:30:59 */ public static void compressImageByPixel(String imgPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只讀邊,不讀內容 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts); newOpts.inJustDecodeBounds = false; int width = newOpts.outWidth; int height = newOpts.outHeight; float maxSize = 1000f;//默許1000px int be = 1; if (width > height && width > maxSize) {//縮放比,用高或寬其中較大的1個數據進行計算 be = (int) (newOpts.outWidth / maxSize); } else if (width < height && height > maxSize) { be = (int) (newOpts.outHeight / maxSize); } be++; newOpts.inSampleSize = be;//設置采樣率 newOpts.inPreferredConfig = Config.ARGB_8888;//該模式是默許的,可不設 newOpts.inPurgeable = true;// 同時設置才會有效 newOpts.inInputShareable = true;//。當系統內存不夠時候圖片自動被回收 bitmap = BitmapFactory.decodeFile(imgPath, newOpts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); try { FileOutputStream fos = new FileOutputStream(new File(imgPath)); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }

第4種方式:將圖片先按比例緊縮然后再降質

此種方式主要結合第2種和第3種方法以下是詳細代碼:

/** * 多線程緊縮圖片的質量 * @author JPH * @param bitmap 內存中的圖片 * @param imgPath 圖片的保存路徑 * @date 2014⑴2⑸下午11:30:43 */ public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){ new Thread(new Runnable() {//開啟多線程進行緊縮處理 @Override public void run() { // TODO Auto-generated method stub ByteArrayOutputStream baos = new ByteArrayOutputStream(); options = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//質量緊縮方法,把緊縮后的數據寄存到baos中 (100表示不緊縮,0表示緊縮到最小) while (baos.toByteArray().length / 1024 > 100) {//循環判斷如果緊縮后圖片是不是大于100kb,大于繼續緊縮 baos.reset();//重置baos即讓下1次的寫入覆蓋之前的內容 options -= 10;//圖片質量每次減少10 if(options<0)options=0;//如果圖片質量小于10,則將圖片的質量緊縮到最小值 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//將緊縮后的圖片保存到baos中 if(options==0)break;//如果圖片的質量已降到最低則,不再進行緊縮 } try { FileOutputStream fos = new FileOutputStream(new File(imgPath));//將緊縮后的圖片保存的本地上指定路徑中 fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } /** * 按比例縮小圖片的像素以到達緊縮的目的 * @author JPH * @param imgPath * @date 2014⑴2⑸下午11:30:59 */ public static void compressImageByPixel(String imgPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只讀邊,不讀內容 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts); newOpts.inJustDecodeBounds = false; int width = newOpts.outWidth; int height = newOpts.outHeight; float maxSize = 1000f;//默許1000px int be = 1; if (width > height && width > maxSize) {//縮放比,用高或寬其中較大的1個數據進行計算 be = (int) (newOpts.outWidth / maxSize); } else if (width < height && height > maxSize) { be = (int) (newOpts.outHeight / maxSize); } be++; newOpts.inSampleSize = be;//設置采樣率 newOpts.inPreferredConfig = Config.ARGB_8888;//該模式是默許的,可不設 newOpts.inPurgeable = true;// 同時設置才會有效 newOpts.inInputShareable = true;//。當系統內存不夠時候圖片自動被回收 bitmap = BitmapFactory.decodeFile(imgPath, newOpts); compressImageByQuality(bitmap,imgPath);//緊縮好比例大小后再進行質量緊縮 }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 久久精品国产精品青草 | 国产一区在线播放 | 精品国产欧美 | 日韩精品久久一区二区三区 | 嫩草影院免费进入网站 | 曰韩一级片 | 激情av在线 | 日韩亚州 | 懂色av午夜一区二区三区蜜桃 | 中文字幕欧美一区 | 中文字幕 自拍偷拍 | 国产黄一级 | 国产精品视频一区二区免费不卡 | 成人av免费在线 | 精品久久精品久久 | 国产精品久久久久久久久久久久久 | 欧美成人免费在线视频 | 久久久精品免费观看 | 欧美日免费 | 毛片在线看片 | 欧美成人午夜免费视在线看片 | 亚洲精品乱码久久久久久蜜桃91 | 日韩一级片 | 亚洲在线一区 | 一级视频在线观看免费 | 日韩免费电影 | 欧洲天堂网 | 免费成人黄色网 | 性做久久久久久免费观看欧美 | 精品国产青草久久久久96 | 香蕉视频成年人 | 亚洲综合色一区 | 久久久久国产一区二区三区四区 | 欧美成人a级片 | 中文字幕一区三区 | 91日韩在线 | 99精品免费久久久久久久久日本 | 国产日韩一区二区三区 | 免费日韩av| 欧美一区 | 国产精品久久久久久久久久东京 |