中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android圖片壓縮

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

1、質(zhì)量壓縮方法

通過compress的方法只是減小了文件的大小,但是并不能保證減低bitmap文件解碼后在內(nèi)存的占用量。

private Bitmap compressImage(Bitmap image) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  //質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
  image.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
  int options = 100;
  while ( baos.toByteArray().length / 1024>100) { //循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮  
   baos.reset();//重置baos即清空baos
   //這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
   image.compress(Bitmap.CompressFormat.JPEG, options, baos); 
   options -= 10;//每次都減少10
  }
  //把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 
  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數(shù)據(jù)生成圖片
  return bitmap;
 }

2、按比例大小壓縮方法

    對Bitmap形式的圖片進(jìn)行壓縮, 也就是通過設(shè)置采樣率, 減少Bitmap的像素, 從而減少了它所占用的內(nèi)存

     ① 根據(jù)路徑獲取圖片并壓縮

private Bitmap getimage(String srcPath) {

  BitmapFactory.Options newOpts = new BitmapFactory.Options();

  //開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了

  newOpts.inJustDecodeBounds = true;//只讀邊,不讀內(nèi)容
  Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時(shí)返回bm為空

  newOpts.inJustDecodeBounds = false;
  int w = newOpts.outWidth;
  int h = newOpts.outHeight;
  //現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為
  float hh = 800f;//這里設(shè)置高度為800f
  float ww = 480f;//這里設(shè)置寬度為480f

  //縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
  int be = 1;//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;//設(shè)置縮放比例
  //重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
  bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
  return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(zhì)量壓縮

 }

  ② 根據(jù)Bitmap圖片壓縮

 private Bitmap comp(Bitmap image) {

  ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  if( baos.toByteArray().length / 1024>1024) {//判斷如果圖片大于1M,進(jìn)行壓縮避免在生成圖片(BitmapFactory.decodeStream)時(shí)溢出 

   baos.reset();//重置baos即清空baos

   image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//這里壓縮50%,把壓縮后的數(shù)據(jù)存放到baos中

  }

  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());

  BitmapFactory.Options newOpts = new BitmapFactory.Options();

  //開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了

  newOpts.inJustDecodeBounds = true;

  Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

  newOpts.inJustDecodeBounds = false;

  int w = newOpts.outWidth;

  int h = newOpts.outHeight;

  //現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為

  float hh = 800f;//這里設(shè)置高度為800f

  float ww = 480f;//這里設(shè)置寬度為480f

  //縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可

  int be = 1;//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;//設(shè)置縮放比例

  //重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了

  isBm = new ByteArrayInputStream(baos.toByteArray());

  bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

  return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(zhì)量壓縮

 }

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字

下一篇:Android獲取指定文件大小