I will demonstrate the mechanisms you can use to control the camera and take
photos within your applications
- Using Intents to take pictures
The easiest way to take a picture using the device camera is using the
ACTION_IMAGE_CAPTURE Media Store static constant in an Intent passed to
startActivityForResult.
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
TAKE_PICTURE);
This will launch the camera Activity, allowing users to modify the image
settings manually, and preventing you from having to rewrite the entire
camera application.
The image capture action supports two modes, thumbnail and full image.
➤ Thumbnail By default, the picture taken by the image capture action will
return a thumbnail Bitmap in the data extra within the Intent parameter
returned in onActivityResult.
As shown in Listing 11-11, call getParcelableExtra specifying the extra
name data on the Intent parameter to return the thumbnail as a Bitmap.
➤ Full image If you specify an output URI using a MediaStore.EXTRA_OUTPUT
extra in the launch Intent, the full-size image taken by the camera will
be saved to the specified location.
In this case no thumbnail will be returned in the Activity result callback
and the result Intent data will be null.
- Below example shows how to use the image capture action to capture either
a thumbnail or full image using an Intent.
private static int TAKE_PICTURE = 1;
private Uri outputFileUri;
private void getThumbailPicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
}
private void saveFullImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
Uri imageUri = null;
// Check if the result includes a thumbnail Bitmap
if (data != null) {
if (data.hasExtra("data")) {
Bitmap thumbnail = data.getParcelableExtra("data");
// TODO Do something with the thumbnail
}
}
else {
// TODO Do something with the full image stored
// in outputFileUri
}
}
}
- The End -
댓글 없음:
댓글 쓰기