Reputation: 988
I am developing an app using android native camera. I can successfully take a photo and store and display on ImageView. I am testing on HTC desire Z, Nexus S and Motorola Zoom. It works on all three devices except one issue on Nexus S. When I take a photo using Nexus S, the preview image has been rotated by 90 degrees.
Can you please let me know how to over come this issue?
My code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
imgTakenPhoto = (ImageView) findViewById(R.id.imageView);
getPhotoClick();
}
private File getTempFile()
{
return new File(Environment.getExternalStorageDirectory(), "image.tmp");
}
private void getPhotoClick()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
}
InputStream is=null;
@Override
protected void onActivityResult(int requestcode, int resultCode, Intent data){
if (requestcode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
File file=getTempFile();
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(is==null){
try {
Uri u = data.getData();
is=getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Bitmap thumbnail = BitmapFactory.decodeStream(is);
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
I have taken help from Problems saving a photo to a file
Many Thanks
Anyone please?
Upvotes: 1
Views: 729
Reputation: 988
Following function will detect the orientation of an image.
public static int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
}
On your code, use this function to rotate the bitmap image. Util class can be found in https://android.googlesource.com/platform/packages/apps/Camera.git
if(degree != 0){
bmp = Util.rotate(bmp, degree);
}
Remember this will only correct the orientation of preview of image. I hope this will help you.
Upvotes: 0
Reputation: 1566
this problem with preview occurs on some phones. The only simple workaround I know is to set landscape mode in onCreate():
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Another (probably overkill) possibility is to overlay another surface on top of the preview surface, create custom PreviewCallback and rotate and draw each frame manually to this surface, but there are problems with performance and screen size.
Upvotes: 1