国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之圖片壓縮工具類完整實例

2019-10-22 18:22:38
字體:
來源:轉載
供稿:網友

本文實例講述了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 {  /**   * 壓縮圖片到指定寬高,并進行質量壓縮,最終大小保持在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;    // 轉成數組    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;    // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置為    float hh = targetHeight;    float ww = targetWidth;    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數據進行計算即可    int be = 1;// be=1表示不縮放    // 如果寬度大的話根據寬度固定大小縮放    if (w > h && w > ww) {      be = (int) (newOpts.outWidth / ww);    } else if (w < h && h > hh) {      // 如果高度高的話根據寬度固定大小縮放      be = (int) (newOpts.outHeight / hh);    }    if (be <= 0) {      be = 1;    }    // 設置縮放比例    newOpts.inSampleSize = be;    // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);    // 壓縮好比例大小后再進行質量壓縮    return compressImage(bitmap);  }  /**   * @Description 質量壓縮方法   * @author XiongJie   * @param image   * @return   */  public static Bitmap compressImage(Bitmap image) {    ByteArrayOutputStream baos = new ByteArrayOutputStream();    // 質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);    int options = 100;    // 循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮    while (baos.toByteArray().length / 1024 > 100) {      // 重置baos即清空baos      baos.reset();      // 這里壓縮options%,把壓縮后的數據存放到baos中      image.compress(Bitmap.CompressFormat.JPEG, options, baos);      // 每次都減少10      options -= 10;    }    // 把壓縮后的數據baos存放到ByteArrayInputStream中    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());    // 把ByteArrayInputStream數據生成圖片    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);    return bitmap;  }  /**   * 只進行分辨率壓縮,不進行圖片的質量壓縮   *   * @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;    // 轉成數組    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;    // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置為    float hh = targetHeight;    float ww = targetWidth;    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數據進行計算即可    // be=1表示不縮放    int be = 1;    if (w > h && w > ww) {      // 如果寬度大的話根據寬度固定大小縮放      be = (int) (newOpts.outWidth / ww);    } else if (w < h && h > hh) {      // 如果高度高的話根據寬度固定大小縮放      be = (int) (newOpts.outHeight / hh);    }    if (be <= 0) {      be = 1;    }    // 設置縮放比例    newOpts.inSampleSize = be;    // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);    // 壓縮好比例大小后再進行質量壓縮    return bitmap;  }}

 

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和顺县| 汨罗市| 昆明市| 靖西县| 汽车| 通道| 平阳县| 普宁市| 右玉县| 定襄县| 高安市| 柳江县| 焉耆| 福建省| 荆门市| 汶上县| 罗山县| 北宁市| 嘉峪关市| 霍城县| 巴南区| 化德县| 华阴市| 大邑县| 克东县| 乐山市| 杂多县| 工布江达县| 比如县| 阳曲县| 通州市| 项城市| 绥滨县| 台中市| 修文县| 闽侯县| 泸定县| 嘉义市| 都匀市| 商丘市| 琼海市|