Android圖片壓縮功能匯總
原圖:width:2976; height:2976原圖實(shí)際:--->byte:2299820 Mb:2.19328質(zhì)量壓縮 size--->:byte:1599831 kb:1562.33496按比例壓縮 size--->:byte:191707 kb:187.21387魯班壓縮 size--->:byte:143792 kb:140.42188
public void getBitmap(String imgPath, String outPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();newOpts.inJustDecodeBounds = false;newOpts.inPurgeable = true;newOpts.inInputShareable = true;// Do not compressnewOpts.inSampleSize = 1;newOpts.inPreferredConfig = Config.RGB_565;storeImage(bitmap, outPath); //保存圖片}
質(zhì)量壓縮不會(huì)減少圖片的像素,它是在保持像素的前提下改變圖片的位深及透明度等,來達(dá)到壓縮圖片的目的,這也是為什么該方法叫質(zhì)量壓縮方法。所以這種方法,很可能不會(huì)減少圖片的大小
如果是bit.compress(CompressFormat.PNG, quality, baos);這樣的png格式,quality就沒有作用了,bytes.length不會(huì)變化,因?yàn)閜ng圖片是無損的,不能進(jìn)行壓縮
/*** 把bitmap轉(zhuǎn)化成圖片存儲(chǔ)在本地** @param bitmap* @param outPath 本地的存儲(chǔ)路徑* @throws FileNotFoundException*/public static boolean storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {FileOutputStream os = new FileOutputStream(outPath);boolean compressResult = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);return compressResult;}
/*** 按比例壓縮** @param path 原圖片路徑* @param targetW 壓縮后寬度* @param targetH 壓縮后高度* @return 壓縮后的圖片的保存路徑*/public static String compressScale(String path,, String outPath, int targetW, int targetH) throws FileNotFoundException {// 獲取optionBitmapFactory.Options options = new BitmapFactory.Options();// inJustDecodeBounds設(shè)置為true,這樣使用該option decode出來的Bitmap是null,// 只是把長(zhǎng)寬存放到option中options.inJustDecodeBounds = true;// 此時(shí)bitmap為nullBitmap bitmap = BitmapFactory.decodeFile(path, options);int inSampleSize = 1; // 1是不縮放// 計(jì)算寬高縮放比例int inSampleSizeW = options.outWidth / targetW;int inSampleSizeH = options.outHeight / targetH;// 最終取大的那個(gè)為縮放比例,這樣才能適配,例如寬縮放3倍才能適配屏幕,而// 高不縮放就可以,那樣的話如果按高縮放,寬在屏幕內(nèi)就顯示不下了if (inSampleSizeW > inSampleSizeH) {inSampleSize = inSampleSizeW;} else {inSampleSize = inSampleSizeH;}// 一定要記得將inJustDecodeBounds設(shè)為false,否則Bitmap為nulloptions.inJustDecodeBounds = false;// 設(shè)置縮放比例(采樣率)options.inSampleSize = inSampleSize;bitmap = BitmapFactory.decodeFile(path, options);boolean isSuccess = storeImage(bitmap, outPath);if (isSuccess) {return outPath;}return "";}
這種方法是設(shè)置圖片的采樣率,降低圖片像素,通過縮放圖片像素來減少圖片大小。
那么我又是如何來獲取圖片壓縮前后的大小的呢?
注意:這里的圖片的大小是指圖片的實(shí)際大小,不是bitmap在內(nèi)存中所占的大小,看壓縮效果,就得看圖片在文件中所占的大小。
/*** 獲取本地文件大小** @param imgPath 圖片的路徑* @return 圖片實(shí)際的大小,單位byte*/public static int getFileSize(String imgPath) {int size = 0;try {FileInputStream fis = new FileInputStream(new File(imgPath));size = fis.available();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return size;}
魯班壓縮是根據(jù)一種算法得出來的,壓縮效果基本上和微信一致,差不多在200k以內(nèi),圖片不失真。
https://github.com/Curzibn/Luban
compile 'top.zibin:Luban:1.1.3'private void lunBanPress(String path) {String pressPath = Environment.getExternalStorageDirectory().getPath();Luban.with(this).load(path) // 傳人要壓縮的圖片列表.ignoreBy(100) // 忽略不壓縮圖片的大小.setTargetDir(pressPath) // 設(shè)置壓縮后文件存儲(chǔ)位置.setCompressListener(new OnCompressListener() { //設(shè)置回調(diào)@Overridepublic void onStart() {// TODO 壓縮開始前調(diào)用,可以在方法內(nèi)啟動(dòng) loading UILog.i(TAG, "onStart:開始魯班壓縮 ");}@Overridepublic void onSuccess(File file) {// TODO 壓縮成功后調(diào)用,返回壓縮后的圖片文件Glide.with(activity).load(file).into(iv2);Log.i(TAG, "onSuccess: 魯班壓縮成功 :");try {int size = new FileInputStream(file).available();Log.i("tag", "魯班壓縮 size--->:" + "byte:" + size + " kb:"+ (float) size / 1024);} catch (IOException e) {e.printStackTrace();}}@Overridepublic void onError(Throwable e) {// TODO 當(dāng)壓縮過程出現(xiàn)問題時(shí)調(diào)用Log.i(TAG, "onError: 魯班壓縮出錯(cuò)");}}).launch(); //啟動(dòng)壓縮}
https://github.com/zhouxu88/ImgCompress
評(píng)論
圖片
表情
