2

나는 카메라를 열고 사진을 찍을 수 있습니다. 사진은 SD 카드에 2448x3264 픽셀의 전체 크기로 저장됩니다. 어떻게 내 응용 프로그램에서이 구성 할 수 있습니다 90x90 픽셀 크기 및 2448x3264 픽셀 크기로 그림을 저장하려면? 내 응용 프로그램에서 안드로이드에 사용자 정의 크기로 캡처 한 이미지를 저장하는 방법

카메라를 열고 난 다음 방법 사용 이미지를 캡처 :

/* 
* 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.createScaledBitmap (largeBitmap, height, width, true); – sirFunkenstine

+0

SirFrankenstine, 도와 줘서 고마워. 아래 내 대답을 참조하십시오 – user1953173

+0

아무도 이것으로 나를 도울 수 있습니까? 캡처 한 이미지의 저장된 축소판 만 사용자 지정 목록보기에서 사용하기를 원합니다. – user1953173

답변

0

여기서는 촬영 된 사진의 SDCard에 저장된 경로를 가져다가 필요한 크기의 이미지를 Bitmap으로 반환하는 메서드를 제공합니다. 이제 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

는 사람이 같은 문제가 wherre 다른 question입니다. 그는 다음을 사용합니다.

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

아니요, MediaStore.ACTION_IMAGE_CAPTURE 의도를 사용할 때 사진 크기를 제어 할 수 없습니다. "custom camera"을 구현하고 mine을 비롯하여 인터넷에 많은 샘플이 있습니다.

onPictureTaken()에 수신 된 바이트 배열은 Jpeg 버퍼입니다. 이미지 조작을 위해이 자바 패키지를 보자 : http://mediachest.sourceforge.net/mediautil/ (안드로이드 포트 on GitHub이있다). Jpeg을 비트 맵으로 디코딩하지 않고도 축소 할 수있는 매우 강력하고 효율적인 방법이 있습니다.