欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android中怎么實(shí)現(xiàn)一個圖片壓縮工具類

本篇文章為大家展示了Android中怎么實(shí)現(xiàn)一個圖片壓縮工具類,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

目前創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、福州網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

具體如下:

package com.sanweidu.TddPay.util2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImaZipUtil {
  /**
   * 壓縮圖片到指定寬高,并進(jìn)行質(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 設(shè)回true了
    newOpts.inJustDecodeBounds = true;
    // 可刪除
    newOpts.inPurgeable = true;
    // 可共享
    newOpts.inInputShareable = true;
    // 轉(zhuǎn)成數(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)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為
    float hh = targetHeight;
    float ww = targetWidth;
    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進(jìn)行計算即可
    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;
    }
    // 設(shè)置縮放比例
    newOpts.inSampleSize = be;
    // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    // 壓縮好比例大小后再進(jìn)行質(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;
  }
  /**
   * 只進(jìn)行分辨率壓縮,不進(jìn)行圖片的質(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 設(shè)回true了
    newOpts.inJustDecodeBounds = true;
    // 可刪除
    newOpts.inPurgeable = true;
    // 可共享
    newOpts.inInputShareable = true;
    // 轉(zhuǎn)成數(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)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為
    float hh = targetHeight;
    float ww = targetWidth;
    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進(jìn)行計算即可
    // 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;
    }
    // 設(shè)置縮放比例
    newOpts.inSampleSize = be;
    // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
    bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length, newOpts);
    // 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮
    return bitmap;
  }
}

上述內(nèi)容就是Android中怎么實(shí)現(xiàn)一個圖片壓縮工具類,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁標(biāo)題:Android中怎么實(shí)現(xiàn)一個圖片壓縮工具類
瀏覽路徑:http://chinadenli.net/article46/gidceg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站內(nèi)鏈、服務(wù)器托管、標(biāo)簽優(yōu)化搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作