2

中保存拍攝的圖像,我可以打開相機並拍照。該照片在SD卡上以2448x3264像素的全尺寸存儲。我如何在我的應用程序中配置此項,以將圖片保存爲90x90像素的大小而不是2448x3264像素?如何在我的應用程序中使用自定義尺寸在android

打開相機和拍攝的圖像我使用下面的方法:

/* 
* Capturing Camera Image will lauch camera app requrest image capture 
*/ 
private void captureImage() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
} 

private Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

private File getOutputMediaFile(int type) { 
    // External sdcard location 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory 
      (Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); 
    } 
    else { 
     return null; 
    } 

    return mediaFile; 
} 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 

/*    
       try { 
        decodeUri(this, fileUri, 90, 90); 
       } catch (FileNotFoundException e) { 

        e.printStackTrace(); 
       } 
*/ 

       // successfully captured the image 
       Toast.makeText(getApplicationContext(), 
         "Picture successfully captured", Toast.LENGTH_SHORT).show(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT).show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

public static Bitmap decodeUri(Context c, Uri uri, final int requiredWidth, final int requiredHeight) throws FileNotFoundException { 

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

     o.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o); 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 

     while(true) { 
      if(width_tmp/2 < requiredWidth || height_tmp/2 < requiredHeight) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 

     // get the file url 
     fileUri = savedInstanceState.getParcelable("file_uri"); 
    } 

我希望S.O.可以幫助我。我試圖將捕獲的圖像加載到一個小圖像視圖中,看起來像that。在此先感謝

+0

Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap,height,width,true); – sirFunkenstine

+0

sirFrankenstine,謝謝你的幫助。請看下面我的回答 – user1953173

+0

沒有人能幫我解決這個問題嗎?我想要的只是保存的捕獲圖像的縮略圖,以便在自定義列表視圖中使用它。 – user1953173

回答

0

在這裏,我給一個方法,將採取保存路徑SDCard的拍攝圖片,並將返回所需的大小的圖像作爲位圖。現在你要做的只是在SDCard上傳遞圖像路徑並獲得調整大小的圖像。

private Bitmap processTakenPicture(String fullPath) { 

    int targetW = 90; //your required width 
    int targetH = 90; //your required height 

    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(fullPath, bmOptions); 

    int scaleFactor = 1; 
    scaleFactor = calculateInSampleSize(bmOptions, targetW, targetH); 

    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor * 2; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(fullPath, bmOptions); 

    return bitmap; 
} 

private 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) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
    return inSampleSize; 
} 
+0

謝謝你,哈米德,但我想保存在自定義大小的開始。那就是我想要的。有沒有一個選項可以做到這一點?我正在使用simplecursor適配器來加載自定義列表視圖中的信息。這意味着,我必須給一個資源ID(例如:R.id.image) – user1953173

0

你讀了原始圖像後,您可以使用:

Bitmap.createScaledBitmap(photo, width, height, true); 
+0

感謝你的幫助。我想將它保存在自定義大小中。看到我上面的答案。 – user1953173

0

這裏是另一個question wherre一個人有同樣的問題。他使用以下內容。

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE); 
1

不,您在使用MediaStore.ACTION_IMAGE_CAPTURE意圖時無法控制圖片大小。如果您實施"custom camera"(並且互聯網上有大量工作示例),則可以實現此目標,其中包括mine

onPictureTaken()中收到的字節數組是Jpeg緩衝區。看看這個Java包的圖像處理:http://mediachest.sourceforge.net/mediautil/(有一個Android端口on GitHub)。有很強大和有效的方法來縮小Jpeg,而不將其解碼成Bitmap並返回。

相關問題