最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼

 更新時(shí)間:2017年07月27日 09:02:31   作者:MrZang  
本篇文章主要介紹了Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

整理文檔,搜刮出一個(gè)Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。

詳解:

1.獲取本地圖片F(xiàn)ile文件 獲取BitmapFactory.Options對(duì)象 計(jì)算原始圖片 目標(biāo)圖片寬高比 計(jì)算輸出的圖片寬高

2.根據(jù)寬高比計(jì)算options.inSampleSize值(縮放比例 If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.)得到bitmap位圖 根據(jù)位圖對(duì)象獲取新的輸出位圖對(duì)象 Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)Creates a new bitmap, scaled from an existing bitmap, whenpossible.

3.獲取圖片方向調(diào)整、失量壓縮圖片保持在1024kb以下

 //進(jìn)行大小縮放來(lái)達(dá)到壓縮的目的
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(srcImagePath, options);
 //根據(jù)原始圖片的寬高比和期望的輸出圖片的寬高比計(jì)算最終輸出的圖片的寬和高

 float srcWidth = options.outWidth;
 float srcHeight = options.outHeight;
 float maxWidth = outWidth;
 float maxHeight = outHeight;
 float srcRatio = srcWidth / srcHeight; //原始圖片寬高比
 float outRatio = maxWidth / maxHeight; //目標(biāo)圖片寬高比
 float actualOutWidth = srcWidth;
 float actualOutHeight = srcHeight;
  if (srcWidth > maxWidth || srcHeight > maxHeight) {
   if(srcRatio>outRatio){ //原始寬高比大于目標(biāo)寬高比
     actualOutWidth = maxWidth;
     actualOutHeight = actualOutWidth / srcRatio;
   }else if(srcRatio<outRatio){ //原始寬高比小于目標(biāo)寬高比
     actualOutHeight = maxHeight;
     actualOutWidth = actualOutHeight * srcRatio;
   }
 }else{
   actualOutWidth = maxWidth;
   actualOutHeight = maxHeight;
 }

 options.inSampleSize = computSampleSize(options, actualOutWidth, actualOutHeight);
 options.inJustDecodeBounds = false;
 Bitmap scaledBitmap = null;
 try {
   scaledBitmap = BitmapFactory.decodeFile(srcImagePath, options);
 } catch (OutOfMemoryError e) {
   e.printStackTrace();
 }
 if (scaledBitmap == null) {
   return null;
 }

  //生成最終輸出的bitmap
 Bitmap actualOutBitmap = Bitmap.createScaledBitmap(scaledBitmap, (int) actualOutWidth, (int) actualOutHeight, true);

 //釋放原始位圖資源
 if(scaledBitmap!=actualOutBitmap){ //判斷目標(biāo)位圖是否和原始位圖指向棧目標(biāo)相同
   scaledBitmap.recycle();
   scaledBitmap = null;
 }

  //處理圖片旋轉(zhuǎn)問(wèn)題
 ExifInterface exif = null;
 try {
   exif = new ExifInterface(srcImagePath);
   int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 0);
   Matrix matrix = new Matrix();
   if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
     matrix.postRotate(90);
   } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
     matrix.postRotate(180);
   } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
     matrix.postRotate(270);
   }
   actualOutBitmap = Bitmap.createBitmap(actualOutBitmap, 0, 0,
       actualOutBitmap.getWidth(), actualOutBitmap.getHeight(), matrix, true);
 } catch (IOException e) {
   e.printStackTrace();
   return null;
 }

  //進(jìn)行有損壓縮
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 int options_ = 100;
 actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//質(zhì)量壓縮方法,把壓縮后的數(shù)據(jù)存放到baos中 (100表示不壓縮,0表示壓縮到最小)

 int baosLength = baos.toByteArray().length;

 while (baosLength / 1024 > maxFileSize) {//循環(huán)判斷如果壓縮后圖片是否大于maxMemmorrySize,大于繼續(xù)壓縮
   baos.reset();//重置baos即讓下一次的寫(xiě)入覆蓋之前的內(nèi)容
   options_ = Math.max(0, options_ - 10);//圖片質(zhì)量每次減少10
   actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//將壓縮后的圖片保存到baos中
   baosLength = baos.toByteArray().length;
   if (options_ == 0)//如果圖片的質(zhì)量已降到最低則,不再進(jìn)行壓縮
     break;
 }
 actualOutBitmap.recycle();

 //將bitmap保存到指定路徑
 FileOutputStream fos = null;
 String filePath = getOutputFileName(srcImagePath);
 try {
   fos = new FileOutputStream(filePath);
   //包裝緩沖流,提高寫(xiě)入速度
   BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
   bufferedOutputStream.write(baos.toByteArray());
   bufferedOutputStream.flush();
 } catch (FileNotFoundException e) {
   return null;
 } catch (IOException e) {
   return null;
 } finally {
   if (baos != null) {
     try {
       baos.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   if (fos != null) {
     try {
       fos.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }

 //獲取位圖縮放比例
 private int computSampleSize(BitmapFactory.Options options, float reqWidth, float reqHeight) {
   float srcWidth = options.outWidth;//20
   float srcHeight = options.outHeight;//10
   int sampleSize = 1;
   if (srcWidth > reqWidth || srcHeight > reqHeight) {
     int withRatio = Math.round(srcWidth / reqWidth);
     int heightRatio = Math.round(srcHeight / reqHeight);
     sampleSize = Math.min(withRatio, heightRatio);
   }
   return sampleSize;
 }

壓縮比例換算:

float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
float widthScale = outWidth / srcWidth;//目標(biāo)/原始 寬比例
float heightScale = outHeight / srcHeight; //目標(biāo)原始 高比
//對(duì)比寬高比選擇較大的一種比例
float scale = widthScale > heightScale ? widthScale : heightScale;
float actualOutWidth = srcWidth;
float actualOutHeight = srcHeight;
if (scale < 1) {
  actualOutWidth = srcWidth * scale;
  actualOutHeight = srcHeight * scale;
}

設(shè)置縮放比例--生成新的位圖

  Matrix matrix1 = new Matrix();
  matrix1.postScale(scale, scale);// 放大縮小比例
  //生成最終輸出的bitmap
  Bitmap actualOutBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix1, true);

  if (actualOutBitmap != scaledBitmap) {
    scaledBitmap.recycle();
    scaledBitmap = null;
    System.gc();
  }

參考:https://github.com/guizhigang/LGImageCompressor

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Fragment概述及用法

    Android Fragment概述及用法

    本文主要介紹Android Fragment的知識(shí),這里整理了詳細(xì)的資料及重要知識(shí)點(diǎn),幫助大家學(xué)習(xí)理解Fragment,有需要的小伙伴可以參考下
    2016-09-09
  • Android仿新浪微博、QQ空間等帖子顯示(1)

    Android仿新浪微博、QQ空間等帖子顯示(1)

    這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博、QQ空間等帖子顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android View的事件分發(fā)機(jī)制深入分析講解

    Android View的事件分發(fā)機(jī)制深入分析講解

    事件分發(fā)從手指觸摸屏幕開(kāi)始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會(huì)傳遞給Android的輸入系統(tǒng)服務(wù)IMS,通過(guò)Binder把消息發(fā)送到activity,activity會(huì)通過(guò)phoneWindow、DecorView最終發(fā)送給ViewGroup。這里就直接分析ViewGroup的事件分發(fā)
    2023-01-01
  • Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Android?OkHttp庫(kù)簡(jiǎn)單使用和封裝教程助你快速掌握網(wǎng)絡(luò)請(qǐng)求技能

    Android?OkHttp庫(kù)簡(jiǎn)單使用和封裝教程助你快速掌握網(wǎng)絡(luò)請(qǐng)求技能

    OkHttp是一個(gè)高效的HTTP客戶(hù)端庫(kù),適用于Android和Java應(yīng)用程序。它支持HTTP/2和SPDY協(xié)議,提供了同步和異步請(qǐng)求API、請(qǐng)求和響應(yīng)攔截器、連接池和多路復(fù)用器、緩存支持、GZIP和DEFLATE壓縮等功能,可以大大提高網(wǎng)絡(luò)請(qǐng)求的性能和可擴(kuò)展性
    2023-04-04
  • Android UI中TextView的使用方法

    Android UI中TextView的使用方法

    這篇文章主要介紹了Android UI中TextView的使用方法的相關(guān)資料,希望通過(guò)本文大家能掌握TextView的使用方法,需要的朋友可以參考下
    2017-09-09
  • Android NTP 時(shí)間同步機(jī)制詳解

    Android NTP 時(shí)間同步機(jī)制詳解

    這篇文章主要為大家介紹了Android NTP時(shí)間同步機(jī)制實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà)

    一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà)

    這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android實(shí)現(xiàn)隨手指移動(dòng)小球

    Android實(shí)現(xiàn)隨手指移動(dòng)小球

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隨手指移動(dòng)小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android HttpURLConnection斷點(diǎn)下載(單線(xiàn)程)

    Android HttpURLConnection斷點(diǎn)下載(單線(xiàn)程)

    這篇文章主要為大家詳細(xì)介紹了Android HttpURLConnection斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評(píng)論

思南县| 安庆市| 潜山县| 甘德县| 海原县| 惠东县| 扎赉特旗| 桐梓县| 吉安市| 防城港市| 苏尼特右旗| 安新县| 邯郸县| 信丰县| 万山特区| 安新县| 鹤山市| 澄江县| 莱西市| 金阳县| 怀来县| 万荣县| 隆子县| 德安县| 旺苍县| 汾西县| 铜山县| 河池市| 凤冈县| 宁津县| 黄梅县| 兰州市| 曲阳县| 永善县| 庆云县| 河西区| 寻甸| 尚志市| 布尔津县| 琼中| 肃宁县|