Code:
public void buttonLogic() {
Button button = (Button) findViewById(R.id.cameraButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Taking a picture!");
//create intent and send picture through intent as extra, created as temp file on device
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(getApplicationContext())));
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
final File file = getTempFile(this);
try {
//retrieve bitmap for picture taken
Bitmap originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
Bitmap rotatedBitmap = null;
//get exif data and orientation to determine auto-rotation for picture
ExifInterface exif = new ExifInterface("" + file);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
//rotate picture certain # of degrees depending on orientation then sets new matrix
Matrix matrix = new Matrix();
switch (orientation) {
case 3:
matrix.postRotate(180);
rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
break;
case 6:
matrix.postRotate(90);
rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
break;
case 8:
matrix.postRotate(270);
rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);
break;
}
Intent i = new Intent(getBaseContext(), PictureActivity.class);
//assigns to global bitmap variable then goes to intent
GlobalClass.img = rotatedBitmap;
startActivity(i);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
I've been able to adjust the picture if it gets automatically rotated using EXIF
data. But when I take a picture with the front-camera
it flips it horizontally. How do I detect if the picture was taken with the front-facing
camera so I can flip it back. I have code to flip it back done but not to detect properly if the front-camera was used. Thanks