Android 圖片的裁剪與相機調用

有時候我們需要的圖片並不適合我們想要的大小, 那麼我們就可以用到系統自帶的圖片裁剪功能, 把規定範圍的圖像給剪出來。

 

  貼上部分代碼:

 

  1. //調用圖庫  
  2. Intent intent = new Intent();  
  3. intent.setType("image/*");  
  4. intent.putExtra("crop""true");    // crop=true 有這句才能出來最後的裁剪頁面.  
  5. intent.putExtra("aspectX", 5);      // 這兩項爲裁剪框的比例.  
  6. intent.putExtra("aspectY", 4);  
  7. //輸出地址  
  8. intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")  
  9. intent.putExtra("outputFormat""JPEG");//返回格式                        
 
  1. startActivityForResult(Intent.createChooser(intent, "選擇圖片"), 1); 


 

  1. //調用相機  
  2. Intent intent = new Intent(  
  3.     MediaStore.ACTION_IMAGE_CAPTURE, null);  
  4. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(  
  5.     "SDCard/1.jpg")));  
  6. startActivityForResult(intent, 2);  


在調用了以上任意一種方法後, 系統會返回onActivityResult, 我們在這個方法中處理就可以了

 

 
    1.     /** 
    2.      * 獲取返回的相片 
    3.      */  
    4.     @Override  
    5.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
    6.     {  
    7.         if (resultCode == 0)  
    8.             return;  
    9.   
    10.         if (requestCode == 2)//調用系統裁剪  
    11.         {  
    12.             File picture = new File("SDCard/1.jpg");       
    13.                         startPhotoZoom(Uri.fromFile(picture));   
    14.         } else if (requestCode == PHOTO_CODE)//得到裁剪後的圖片  
    15.         {  
    16.             try  
    17.             {  
    18.                 BitmapFactory.Options options = new BitmapFactory.Options();  
    19.                 options.inSampleSize = 2;  
    20.                 Bitmap bitmap = BitmapFactory.decodeFile("SDCard/1.jpg", options);  
    21.   
    22.                 if (bitmap != null)//保存圖片  
    23.                 {  
    24.                     mCacheBitmap = bitmap;  
    25.   
    26.                     FileOutputStream fos = null;  
    27.                     fos = new FileOutputStream("SDCard/1.jpg");  
    28.                     mCacheBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);  
    29.                 }  
    30.   
    31.                   
    32.             } catch (Exception e)  
    33.             {  
    34.                 // TODO: handle exception  
    35.             }  
    36.         }  
    37.   
    38.         super.onActivityResult(requestCode, resultCode, data);  
    39.     }  
    40.       
    41.     /** 
    42.      * 裁剪圖片 
    43.      * @param uri 
    44.      */  
    45.     public void startPhotoZoom(Uri uri) {       
    46.         Intent intent = new Intent("com.android.camera.action.CROP");       
    47.         intent.setDataAndType(uri, "image/*");  
    48.         intent.putExtra("crop""true");// crop=true 有這句才能出來最後的裁剪頁面.  
    49.         intent.putExtra("aspectX"5);// 這兩項爲裁剪框的比例.  
    50.         intent.putExtra("aspectY"4);// x:y=1:2  
    51.         intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")));  
    52.         intent.putExtra("outputFormat""JPEG");//返回格式     
    53.         startActivityForResult(intent, PHOTO_CODE); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章