Android開發(fā)之圖片壓縮工具類完整實(shí)例
本文實(shí)例講述了Android圖片壓縮工具類。分享給大家供大家參考,具體如下:
這里共享一個(gè)圖片壓縮工具類:
package com.sanweidu.TddPay.util2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class ImaZipUtil { /** * 壓縮圖片到指定寬高,并進(jìn)行質(zhì)量壓縮,最終大小保持在100K以下 * * @param sourceBm * @param targetWidth * @param targetHeight * @return */ public static Bitmap zipPic(Bitmap sourceBm, float targetWidth, float targetHeight) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了 newOpts.inJustDecodeBounds = true; // 可刪除 newOpts.inPurgeable = true; // 可共享 newOpts.inInputShareable = true; // 轉(zhuǎn)成數(shù)組 ByteArrayOutputStream baos = new ByteArrayOutputStream(); sourceBm.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] temp = baos.toByteArray(); // 此時(shí)返回bm為空 Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為 float hh = targetHeight; float ww = targetWidth; // 縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可 int be = 1;// be=1表示不縮放 // 如果寬度大的話根據(jù)寬度固定大小縮放 if (w > h && w > ww) { be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) { // 如果高度高的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) { be = 1; } // 設(shè)置縮放比例 newOpts.inSampleSize = be; // 重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了 bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts); // 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮 return compressImage(bitmap); } /** * @Description 質(zhì)量壓縮方法 * @author XiongJie * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 while (baos.toByteArray().length / 1024 > 100) { // 重置baos即清空baos baos.reset(); // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); // 每次都減少10 options -= 10; } // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream數(shù)據(jù)生成圖片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } /** * 只進(jìn)行分辨率壓縮,不進(jìn)行圖片的質(zhì)量壓縮 * * @param sourceBm * @param targetWidth * @param targetHeight * @return */ public static Bitmap zipPicWithoutCompress(Bitmap sourceBm, float targetWidth, float targetHeight) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了 newOpts.inJustDecodeBounds = true; // 可刪除 newOpts.inPurgeable = true; // 可共享 newOpts.inInputShareable = true; // 轉(zhuǎn)成數(shù)組 ByteArrayOutputStream baos = new ByteArrayOutputStream(); sourceBm.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] temp = baos.toByteArray(); // 此時(shí)返回bm為空 Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為 float hh = targetHeight; float ww = targetWidth; // 縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可 // be=1表示不縮放 int be = 1; if (w > h && w > ww) { // 如果寬度大的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) { // 如果高度高的話根據(jù)寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) { be = 1; } // 設(shè)置縮放比例 newOpts.inSampleSize = be; // 重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了 bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts); // 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮 return bitmap; } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android仿淘寶商品拖動(dòng)查看詳情及標(biāo)題欄漸變功能
這篇文章主要介紹了Android仿淘寶商品拖動(dòng)查看詳情及標(biāo)題欄漸變功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Andriod Studio實(shí)現(xiàn)保存QQ密碼功能(案例代碼詳解)
這篇文章主要介紹了Andriod Studio實(shí)現(xiàn)保存QQ密碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對(duì)按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-11-11關(guān)于Android的 DiskLruCache磁盤緩存機(jī)制原理
DiskLruCache是一種管理數(shù)據(jù)存儲(chǔ)的技術(shù),單從Cache的字面意思也可以理解到,"Cache","高速緩存";今天我們來從源碼上分析下DiskLruCache;關(guān)于Android LruCache的緩存機(jī)制原理,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
Android應(yīng)用程序的四大組件中Activity、BroadcastReceiver、ContentProvider、Service都可以進(jìn)行跨進(jìn)程,Android系統(tǒng)采用了遠(yuǎn)程過程調(diào)用(RPC)方式來實(shí)現(xiàn)跨進(jìn)程調(diào)用服務(wù)(Service),對(duì)于Service的跨進(jìn)程調(diào)用需要通過AIDL來實(shí)現(xiàn),關(guān)于如何實(shí)現(xiàn)aidl service請(qǐng)看本文介紹2015-10-10Android實(shí)現(xiàn)便于批量操作可多選的圖片ListView實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)便于批量操作可多選的圖片ListView功能實(shí)現(xiàn)方法,涉及ListView針對(duì)多圖操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08Android.mk文件中添加第三方j(luò)ar文件的方法
這篇文章主要介紹了Android.mk文件中添加第三方j(luò)ar文件及引用第三方j(luò)ar包的方法,需要的朋友可以參考下2018-01-01Android實(shí)現(xiàn)view拖動(dòng)到任意位置
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)view拖動(dòng)到任意位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04