Android中的相机方向问题

Ngu*_*inh 100 android orientation android-camera-intent

我正在构建一个使用相机拍照的应用程序.这是我的源代码:

        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);
Run Code Online (Sandbox Code Playgroud)

onActivityResult()方法上,我BitmapFactory.decodeStream()用来拾取图像.

当我在Nexus上运行我的应用程序时,它运行良好.但是当我在三星Galaxy S或HTC Inspire 4G上运行时,图像的方向是不正确的.

  • 使用纵向模式拍摄时,真实图像(保存在SD卡上)始终旋转90度.

拍摄后的图像预览 SD卡上的真实图像

拍摄后的图像预览--------- SD卡上的真实图像

  • 用横向模式捕捉,一切都很好.

拍摄后的图像预览 SD卡上的真实图像

拍摄后的图像预览--------- SD卡上的真实图像

小智 50

这里有很多类似的主题和问题.既然你没有自己编写相机,我认为可以归结为:

一些设备在保存之前旋转图像,而其他设备只是在照片的exif数据中添加方向标记.

我建议查看照片的exif数据并特别注意

ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Run Code Online (Sandbox Code Playgroud)

由于照片在您的应用中正确显示,我不确定问题出在哪里,但这绝对应该让您走在正确的道路上!

  • 这似乎不适用于某些设备,它为所有方向返回0 ..我知道它发生在Galaxy S Infuse和Sony Xperia Arc,以及S II ..有趣的是,当从图库中选择这些相同的图像时,内容提供商有正确的定位价值.任何想法? (32认同)
  • @Abhijit是的我尝试解决这个问题(使用Android打开的机票等等)而且我认为我找到了一个合理的解决方案来处理具有正确和错误方向信息的两部手机.看看我在这里发布到我自己的问题的详细答案; http://stackoverflow.com/a/8864367/137404 (3认同)
  • 它总是在我的三星 Galaxy S4 上返回 0 (ExifInterface.ORIENTATION_UNDEFINED) ... (2认同)

小智 28

我刚遇到同样的问题,并用它来纠正方向:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果位图的宽度大于高度,则返回的图像是横向的,因此我将其旋转90度.

希望它可以帮助其他人解决这个问题.

  • 如果图像实际上是*在风景中拍摄的,该怎么办?您的代码仍会旋转它.这不是问题的答案. (17认同)
  • 但是,如果应用程序强制肖像,你仍然可以拍摄风景图片(宽度>高度),它将被旋转...至少我正在设置screenOrientation ="肖像"的一切...相机仍然可以采取景观图片. (5认同)

小智 21

有两件事需要:

  1. 相机预览需要与旋转相同.设置它camera.setDisplayOrientation(result);

  2. 将拍摄的照片保存为相机预览.通过这样做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);
    
    Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 这假设您直接使用Camera类,在使用ACTION_IMAGE_CAPTURE意图时无法指定相同的选项. (10认同)

小智 10

            int rotate = 0;
            try {
                File imageFile = new File(sourcepath);
                ExifInterface exif = new ExifInterface(
                        imageFile.getAbsolutePath());
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)


Pak*_*toV 7

另一种选择是在结果屏幕中旋转位图,如下所示:

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

img.setImageDrawable(bmd);
Run Code Online (Sandbox Code Playgroud)

  • 这种方法不起作用,因为它还可以从正确处理方向的设备旋转图像. (30认同)