Camera orientation issue in Android
I am building an application that uses camera to take pictures. Here is my source code to do this:
File file = new File(Environment.getExternalStorageDirectory(),
imageFileName);
imageFilePath = file.getPath();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, ACTIVITY_NATIVE_CAMERA_AQUIRE);
On onActivityResult()
method, I use BitmapFactory.decodeStream()
to pickup the image.
When I run my application on Nexus one, it runs well. But when I run on Samsung Galaxy S or HTC Inspire 4G, the image's direction is not correct.
Image preview after shot --------- Real image on SD card
Image preview after shot --------- Real image on SD card
There are quite a few similar topics and issues around here. Since you're not writing your own camera, I think it boils down to this:
some devices rotate the image before saving it, while others simply add the orientation tag in the photo's exif data.
I'd recommend checking the photo's exif data and looking particularly for
ExifInterface exif = new ExifInterface(SourceFileName); //Since API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Since the photo is displaying correctly in your app, i'm not sure where the problem is, but this should definitely set you on the right path!
I just encountered the same issue, and used this to correct the orientation:
public void fixOrientation() {
if (mBitmap.getWidth() > mBitmap.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
}
}
If the width of the Bitmap is greater than the height, the returned image is in landscape, so I rotate it 90 degrees.
Hope it helps anyone else with this issue.
There are two things needed:
Camera preview need the same as your rotation. Set this by camera.setDisplayOrientation(result);
Save the picture captured as your camera preview. Do this via Camera.Parameters
.
int mRotation = getCameraDisplayOrientation();
Camera.Parameters parameters = camera.getParameters();
parameters.setRotation(mRotation); //set rotation to save the picture
camera.setDisplayOrientation(result); //set the rotation for preview camera
camera.setParameters(parameters);
Hope that helps.
链接地址: http://www.djcxy.com/p/41836.html上一篇: 三星Galaxy Tab纵向定位问题
下一篇: Android中的相机方向问题