2012-09-02 93 views
0

我想拍攝指定尺寸的圖像並將其保存在SD卡上的所需位置。我正在使用intent.putExtra通過默認的相機應用程序拍攝圖像。修改Android中攝像頭拍攝的圖像的尺寸

這裏去的代碼

public void onClick(View v) { 
    //Setting up the URI for the desired location 
    imageFile = "bmp"+v.getId()+".png"; 
    File f = new File (folder,imageFile); 
    imageUri = Uri.fromFile(f); 

    //Setting the desired size parameters 
    private Camera mCamera;  
    Camera.Parameters parameters = mCamera.getParameters(); 
    parameters.setPreviewSize(width, height); 
    mCamera.setParameters(parameters);  

    //Passing intent.PutExtras to defaul camera activity 
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); 
    startActivityForResult(i,CAMERA_PIC_REQUEST); 
} 




@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
    return; 
} 

拍攝的圖像後,相機activiy力關閉。 是否可以通過這種方式修改默認相機活動拍攝的圖像的大小?

或者單獨的相機應用程序是必要的?

+0

我們想不通爲什麼有問題如果你不告訴我們什麼問題*是*。 – Eric

+0

我編輯了這個問題,請看一下 –

+0

你可以用強制關閉的完整日誌來編輯你的問題嗎?而且,如果你能夠閱讀它們,那麼突出顯示那部分代碼也是有用的。 – Eric

回答

0

如果你的圖像保存爲一個文件,從文件中創建位圖,並用這種方法減少它的大小和該位圖傳遞到您的活動:

public static Bitmap decodeFile(File file, int requiredSize) { 
    try { 

     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(file), null, o); 

     // The new size we want to scale to 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < requiredSize 
        || height_tmp/2 < requiredSize) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(file), null, 
       o2); 
    } catch (FileNotFoundException e) { 
    } 
    return null; 
} 
相關問題