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

Android開發(fā)之圖片壓縮工具類完整實例

 更新時間:2017年11月24日 10:41:58   作者:勤修戒定慧  
這篇文章主要介紹了Android開發(fā)之圖片壓縮工具類,結合完整實例形式分析了Android針對圖片壓縮的相關屬性設置與轉換操作實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Android圖片壓縮工具類。分享給大家供大家參考,具體如下:

這里共享一個圖片壓縮工具類:

package com.sanweidu.TddPay.util2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImaZipUtil {
  /**
   * 壓縮圖片到指定寬高,并進行質(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();
    // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
    newOpts.inJustDecodeBounds = true;
    // 可刪除
    newOpts.inPurgeable = true;
    // 可共享
    newOpts.inInputShareable = true;
    // 轉成數(shù)組
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    sourceBm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] temp = baos.toByteArray();
    // 此時返回bm為空
    Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為
    float hh = targetHeight;
    float ww = targetWidth;
    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(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;
    }
    // 設置縮放比例
    newOpts.inSampleSize = be;
    // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設回false了
    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    // 壓縮好比例大小后再進行質(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;
  }
  /**
   * 只進行分辨率壓縮,不進行圖片的質(zhì)量壓縮
   *
   * @param sourceBm
   * @param targetWidth
   * @param targetHeight
   * @return
   */
  public static Bitmap zipPicWithoutCompress(Bitmap sourceBm, float targetWidth, float targetHeight) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
    newOpts.inJustDecodeBounds = true;
    // 可刪除
    newOpts.inPurgeable = true;
    // 可共享
    newOpts.inInputShareable = true;
    // 轉成數(shù)組
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    sourceBm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] temp = baos.toByteArray();
    // 此時返回bm為空
    Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設置為
    float hh = targetHeight;
    float ww = targetWidth;
    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(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;
    }
    // 設置縮放比例
    newOpts.inSampleSize = be;
    // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設回false了
    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    // 壓縮好比例大小后再進行質(zhì)量壓縮
    return bitmap;
  }
}

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • viewpager實現(xiàn)自動循環(huán)輪播圖

    viewpager實現(xiàn)自動循環(huán)輪播圖

    這篇文章主要為大家詳細介紹了viewpager實現(xiàn)自動循環(huán)輪播圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法

    Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法

    這篇文章主要介紹了Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Android Gradle多渠道打包的實現(xiàn)方法

    Android Gradle多渠道打包的實現(xiàn)方法

    這篇文章主要介紹了Android Gradle多渠道打包的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Android RecyclerView多類型布局卡片解決方案

    Android RecyclerView多類型布局卡片解決方案

    這篇文章主要介紹了Android RecyclerView多類型布局卡片解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Android中l(wèi)ayout屬性大全

    Android中l(wèi)ayout屬性大全

    這篇文章主要介紹了Android中l(wèi)ayout屬性含義及用法,較為詳細的總結分析了layout屬性相關用法,需要的朋友可以參考下
    2015-05-05
  • Android判斷當前App是在前臺還是在后臺

    Android判斷當前App是在前臺還是在后臺

    這篇文章主要為大家詳細介紹了Android判斷當前App是在前臺還是在后臺的方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 使用Flutter 構建Web應用邏輯解析

    使用Flutter 構建Web應用邏輯解析

    這篇文章主要為大家介紹了使用Flutter 構建Web應用邏輯解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android實現(xiàn)3秒鐘自動關閉界面

    Android實現(xiàn)3秒鐘自動關閉界面

    這篇文章主要為大家詳細介紹了Android實現(xiàn)3秒鐘自動關閉界面,以支付成功為例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Android自定義view實現(xiàn)動態(tài)柱狀圖

    Android自定義view實現(xiàn)動態(tài)柱狀圖

    這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)動態(tài)柱狀圖的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android自定義圓點指示器

    Android自定義圓點指示器

    這篇文章主要為大家詳細介紹了Android自定義圓點指示器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評論

云梦县| 宁国市| 宿迁市| 始兴县| 静宁县| 城固县| 金山区| 汶川县| 赣州市| 息烽县| 涟水县| 黑山县| 洛浦县| 博乐市| 葫芦岛市| 临桂县| 卢氏县| 铜川市| 高雄市| 凤庆县| 霍林郭勒市| 闽清县| 庄浪县| 永安市| 安新县| 日土县| 花垣县| 凤城市| 江门市| 华坪县| 闻喜县| 壤塘县| 时尚| 公安县| 北京市| 绥宁县| 英超| 丰县| 包头市| 江安县| 沿河|