Reputation: 729
Can someone provide me some code sample about capturing a "full" image from a camera and then converting it in "startActivityForResult" as a byte, and also as a bitmap to display in imageView. Any help will be truly appreciated.
Sam
Upvotes: 1
Views: 536
Reputation: 5093
It's a little tricky dealing with the camera. Here is a piece of code that I had written for an app.
private final int RECEIVE_CAMERA_PICTURE = 10;
private Uri mCameraUri = null;
mCameraUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);
mtimeCameraAcessed = System.currentTimeMillis();
startActivityForResult(cameraIntent, RECEIVE_CAMERA_PICTURE);
Now to process the image capture, the following should be placed in the onActivityResult() method. You will notice that I have put in a check to fetch the orientation of the image being captured. This value will be helpful in displaying the image via an ImageView in a different activity:
int orientation = -10;
Intent displayCameraPictureIntent = new Intent(MainActivity.this, FilterActivity.class);
displayCameraPictureIntent.setData(mCameraUri);
String filePath = OtherUtils.getRealPathFromURI(mContext, mCameraUri);
long fileSize = new File(filePath).length();
Cursor mediaCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Images.ImageColumns.ORIENTATION, MediaStore.MediaColumns.SIZE }, MediaStore.MediaColumns.DATE_ADDED + ">=?", new String[]{String.valueOf(mtimeCameraAcessed/1000 - 1)}, MediaStore.MediaColumns.DATE_ADDED + " desc");
//ensure that the app doesn't consume inappropriate data
if (mediaCursor != null && mtimeCameraAcessed != 0 && mediaCursor.getCount() != 0 ) {
while(mediaCursor.moveToNext()){
long size = mediaCursor.getLong(1);
//Extra check to make sure that we are getting the orientation from the proper file
if(size == fileSize){
orientation = mediaCursor.getInt(0);
break;
}
}
}
displayCameraPictureIntent.putExtra("orientationValue", orientation);
startActivity(displayCameraPictureIntent);
Now, to display the image, inside a new activity:
private Bitmap imageBitmap = null;
Uri imageUri = getIntent().getData();
shareImageUri = imageUri;
try {
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (Exception e) {
// TODO Auto-generated catch block
}
// check whether image is in landscape or portrait mode
if(getIntent().getIntExtra("orientationValue", 1) == 90) {
//image is in portrait. So, rotate the image by 90degrees so that it is displayed in portrait mode
}
//now, set the bitmap to the appropriate imageView. You might want to scale the bitmap, to avoid
//memory issues.
You would also want to check this link, which explains the orientation aspect of this piece of code: Images taken with ACTION_IMAGE_CAPTURE always returns 1 for ExifInterface.TAG_ORIENTATION on some newer devices
Please let me know, if you have any further questions or concerns. If instead, you want to use the Camera api, I forewarn you that it is a mighty challenging task, but definitely plausible.
Upvotes: 2
Reputation: 1081
You've probably seen this but. There's a great tutorial here.
http://developer.android.com/reference/android/hardware/Camera.html
For Bitmaps http://developer.android.com/reference/android/graphics/BitmapFactory.html
I actually have an app that does that, I can send it to you later when I get home from work.
Upvotes: 2