android bitmap compress(圖片壓縮)代碼_Android

來源:互聯網
上載者:User

android的照相功能隨著手機硬體的發展,變得越來越強大,能夠找出很高解析度的圖片。
有些情境中,需要照相併且上傳到服務,但是由於圖片的大小太大,那麼就上傳就會很慢(在有些網路情況下),而且很耗流量,要想速度快,那麼就需要減小圖片的大小。減少圖片的大小有兩種方法,1. 照小圖片; 2. 壓縮大圖片。 照相時擷取小圖片一般不太符合要求,因為,圖片的清晰度會很差,但是這種情況有個好處就是應用速度會快些; 壓縮圖片,就是把大圖片壓縮小,降低圖片的品質,在一定範圍內,降低圖片的大小,並且滿足需求(圖片仍就清晰)。下面組要是介紹圖片的壓縮:


1. 照相請查看http://www.jb51.net/article/37760.htm ->想要儲存圖片到制定目錄,啟動Camera應用時,需要指定檔案
2. 壓縮過程:
    2.1 從圖片路徑中讀取圖片(圖片很大,不能全部加在到記憶體中處理,要是全部載入到記憶體中會記憶體溢出)
[java]

複製代碼 代碼如下:

final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, 480, 800);

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false;

    Bitmap bm = BitmapFactory.decodeFile(filePath, options);

 final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(filePath, options);

  // Calculate inSampleSize
  options.inSampleSize = calculateInSampleSize(options, 480, 800);

  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;

  Bitmap bm = BitmapFactory.decodeFile(filePath, options);


2.2 處理圖片旋轉 
[java]
複製代碼 代碼如下:

int degree = readPictureDegree(filePath);
        bm = rotateBitmap(bm,degree) ;

int degree = readPictureDegree(filePath);
  bm = rotateBitmap(bm,degree) ;[java] view plaincopyprint?private static int readPictureDegree(String path) {  
           int degree  = 0;  
           try {  
                   ExifInterface exifInterface = new ExifInterface(path);  
                   int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
                   switch (orientation) {  
                   case ExifInterface.ORIENTATION_ROTATE_90:  
                           degree = 90;  
                           break;  
                   case ExifInterface.ORIENTATION_ROTATE_180:  
                           degree = 180;  
                           break;  
                   case ExifInterface.ORIENTATION_ROTATE_270:  
                           degree = 270;  
                           break;  
                   }  
           } catch (IOException e) {  
                   e.printStackTrace();  
           }  
           return degree;  
       } 

private static int readPictureDegree(String path) {
        int degree  = 0;
        try {
                ExifInterface exifInterface = new ExifInterface(path);
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
        } catch (IOException e) {
                e.printStackTrace();
        }
        return degree;
    }


[java]
複製代碼 代碼如下:

view plaincopyprint?private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
        if(bitmap == null)
            return null ;

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        // Setting post rotate to 90 
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
  if(bitmap == null)
   return null ;

  int w = bitmap.getWidth();
  int h = bitmap.getHeight();

  // Setting post rotate to 90
  Matrix mtx = new Matrix();
  mtx.postRotate(rotate);
  return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
 }


2.3壓縮圖片      
[java]
複製代碼 代碼如下:

bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是壓縮率,表示壓縮70%; 如果不壓縮是100,表示壓縮率為0

bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是壓縮率,表示壓縮70%; 如果不壓縮是100,表示壓縮率為0


完整的方法代碼:
[java]
複製代碼 代碼如下:

public static Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize 
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set 
        options.inJustDecodeBounds = false;

        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        if(bm == null){
            return  null;
        }
        int degree = readPictureDegree(filePath);
        bm = rotateBitmap(bm,degree) ;
        ByteArrayOutputStream baos = null ;
        try{
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

        }finally{
            try {
                if(baos != null)
                    baos.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bm ;

    }

public static Bitmap getSmallBitmap(String filePath) {

  final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(filePath, options);

  // Calculate inSampleSize
  options.inSampleSize = calculateInSampleSize(options, 480, 800);

  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;

  Bitmap bm = BitmapFactory.decodeFile(filePath, options);
  if(bm == null){
   return  null;
  }
  int degree = readPictureDegree(filePath);
  bm = rotateBitmap(bm,degree) ;
  ByteArrayOutputStream baos = null ;
  try{
   baos = new ByteArrayOutputStream();
   bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

  }finally{
   try {
    if(baos != null)
     baos.close() ;
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return bm ;

 }


[java]
複製代碼 代碼如下:

view plaincopyprint?private static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image 
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and 
            // width 
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will 
            // guarantee 
            // a final image with both dimensions larger than or equal to the 
            // requested height and width. 
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
        }

        return inSampleSize;
    }

private static int calculateInSampleSize(BitmapFactory.Options options,
   int reqWidth, int reqHeight) {
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  int inSampleSize = 1;

  if (height > reqHeight || width > reqWidth) {

   // Calculate ratios of height and width to requested height and
   // width
   final int heightRatio = Math.round((float) height
     / (float) reqHeight);
   final int widthRatio = Math.round((float) width / (float) reqWidth);

   // Choose the smallest ratio as inSampleSize value, this will
   // guarantee
   // a final image with both dimensions larger than or equal to the
   // requested height and width.
   inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
  }

  return inSampleSize;
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.