Android中3種圖片壓縮處理方法
Android中圖片的存在形式:
1:文件形式:二進(jìn)制形式存在與硬盤中。
2:流的形式:二進(jìn)制形式存在與內(nèi)存中。
3:Bitmap的形式
三種形式的區(qū)別:
文件形式和流的形式:對(duì)圖片體積大小并沒有影響。也就是說,如果你手機(jī)SD卡上的圖片通過流的形式讀到內(nèi)存中,在內(nèi)存中的大小也是原圖的大小。
注意:不是Bitmap的形式。
Bitmap的形式:圖片占用的內(nèi)存會(huì)瞬間變大。
以下是代碼的形式:
/** * 圖片壓縮的方法總結(jié) */ /* * 圖片壓縮的方法01:質(zhì)量壓縮方法 */ private Bitmap compressImage(Bitmap beforBitmap) { // 可以捕獲內(nèi)存緩沖區(qū)的數(shù)據(jù),轉(zhuǎn)換成字節(jié)數(shù)組。 ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (beforBitmap != null) { // 第一個(gè)參數(shù):圖片壓縮的格式;第二個(gè)參數(shù):壓縮的比率;第三個(gè)參數(shù):壓縮的數(shù)據(jù)存放到bos中 beforBitmap.compress(CompressFormat.JPEG, 100, bos); int options = 100; // 循環(huán)判斷壓縮后的圖片是否是大于100kb,如果大于,就繼續(xù)壓縮,否則就不壓縮 while (bos.toByteArray().length / 1024 > 100) { bos.reset();// 置為空 // 壓縮options% beforBitmap.compress(CompressFormat.JPEG, options, bos); // 每次都減少10 options -= 10; } // 從bos中將數(shù)據(jù)讀出來(lái) 存放到ByteArrayInputStream中 ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray()); // 將數(shù)據(jù)轉(zhuǎn)換成圖片 Bitmap afterBitmap = BitmapFactory.decodeStream(bis); return afterBitmap; } return null; } /* * 圖片壓縮方法02:獲得縮略圖 */ public Bitmap getThumbnail(int id) { // 獲得原圖 Bitmap beforeBitmap = BitmapFactory.decodeResource( mContext.getResources(), id); // 寬 int w = mContext.getResources() .getDimensionPixelOffset(R.dimen.image_w); // 高 int h = mContext.getResources().getDimensionPixelSize(R.dimen.image_h); // 獲得縮略圖 Bitmap afterBitmap = ThumbnailUtils .extractThumbnail(beforeBitmap, w, h); return afterBitmap; } /** * 圖片壓縮03 * * @param id * 要操作的圖片的大小 * @param newWidth * 圖片指定的寬度 * @param newHeight * 圖片指定的高度 * @return */ public Bitmap compressBitmap(int id, double newWidth, double newHeight) { // 獲得原圖 Bitmap beforeBitmap = BitmapFactory.decodeResource( mContext.getResources(), id); // 圖片原有的寬度和高度 float beforeWidth = beforeBitmap.getWidth(); float beforeHeight = beforeBitmap.getHeight(); // 計(jì)算寬高縮放率 float scaleWidth = 0; float scaleHeight = 0; if (beforeWidth > beforeHeight) { scaleWidth = ((float) newWidth) / beforeWidth; scaleHeight = ((float) newHeight) / beforeHeight; } else { scaleWidth = ((float) newWidth) / beforeHeight; scaleHeight = ((float) newHeight) / beforeWidth; } // 矩陣對(duì)象 Matrix matrix = new Matrix(); // 縮放圖片動(dòng)作 縮放比例 matrix.postScale(scaleWidth, scaleHeight); // 創(chuàng)建一個(gè)新的Bitmap 從原始圖像剪切圖像 Bitmap afterBitmap = Bitmap.createBitmap(beforeBitmap, 0, 0, (int) beforeWidth, (int) beforeHeight, matrix, true); return afterBitmap; }
相關(guān)文章
Android Flutter制作交錯(cuò)動(dòng)畫的示例代碼
這篇文章我們將用Flutter實(shí)現(xiàn)一個(gè)交錯(cuò)動(dòng)畫的應(yīng)用實(shí)例,我們讓輪子在草地滾動(dòng)著前進(jìn),而且還能粘上“綠色的草”,感興趣的可以動(dòng)手嘗試一下2022-06-06Android:“萬(wàn)能”Activity重構(gòu)篇
本文主要介紹了mvp以及每一層,以及使用mvp來(lái)重構(gòu)“萬(wàn)能”Activity,其實(shí)每一層需要注意的東西還有很多,比如model層是最難寫的一層。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法
這篇文章主要為大家詳細(xì)介紹了RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android Handler runWithScissors 梳理流程解析
這篇文章主要為大家介紹了Android Handler runWithScissors 梳理流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android在view.requestFocus(0)返回false的解決辦法
這篇文章主要介紹了Android在view.requestFocus(0)返回false的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-08-08A10_DatePicker的對(duì)話框設(shè)置(使用OnDateSetListener監(jiān)聽器)
本文主要彌補(bǔ)A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程
MVVM架構(gòu)模式,即Model-View-ViewModel三個(gè)層級(jí),MVVM模式出來(lái)的時(shí)間已經(jīng)很長(zhǎng)了,網(wǎng)上關(guān)于MVVM模式的解析也有很多,我這里只說一下我自己的理解,基本上是和MVP模式相比較的一個(gè)差異2021-10-10android 控件同時(shí)監(jiān)聽單擊和雙擊實(shí)例
這篇文章主要介紹了android 控件同時(shí)監(jiān)聽單擊和雙擊實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08