【问题标题】:Captured Photo orientation is changing in android捕获的照片方向在 android 中发生变化
【发布时间】:2012-06-14 04:21:08
【问题描述】:

我正在通过单击按钮打开相机应用程序。并在下一个活动中显示拍摄的照片。但拍摄的照片旋转了 90 度。当我在捕获图像后在视图中显示图像时,它的方向始终是横向的。为什么在人像模式下拍摄照片时,照片没有以人像模式显示?

点击按钮:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(APP_DIR+"/latest.png")));       
startActivityForResult(i, CAPTURE_PHOTO_CONSTANT);

onActvityresult 内部:

bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png");
startActivity(new Intent(this, DisplayActivity.class));

显示拍摄的照片:

photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp));

【问题讨论】:

标签: android bitmap camera


【解决方案1】:

我在三星手机上遇到了同样的问题。显然三星手机设置了 EXIF 方向标签,而不是旋转单个像素。使用 BitmapFactory 读取位图不支持这个标签。我发现这个问题的解决方案是使用Activity 的 onActivityResult 方法中的 ExifInterface。它检查与从相机捕获的图像的 URI 关联的方向。

                        int rotate = 0;
                        try {
                            getContentResolver().notifyChange(imageUri, null);
                            File imageFile = new File(imagePath);
                            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;
                            }
                            Log.v(Common.TAG, "Exif orientation: " + orientation);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        /****** Image rotation ****/
                        Matrix matrix = new Matrix();
                        matrix.postRotate(orientation);
                        Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);

【讨论】:

  • 最后需要旋转位图吗?
  • 最后需要旋转位图。
  • 谢谢。有用。但我有一个小小的疑问。我们在捕获图像后旋转图像。它是否也会旋转在三星设备以外的其他设备中拍摄的照片?如果照片在 XXXX 设备中正确拍摄怎么办?
  • 在其他设备中 intorientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);函数返回 0 角度,因此 switch 语句永远不会被调用,因此方向保持不变。
  • 我面临严重的代码问题.. 三星 Galaxy s4 设备正在重新启动.. 你能帮我吗?为什么会这样?
【解决方案2】:

请改用 Glide。它本身固定方向,与您使用的手机无关。这是一个示例

Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury);

【讨论】:

  • 很好的答案赛达
  • 它在原始图像中工作。但是调整图像大小后它不起作用
【解决方案3】:

用位图和位图的路径调用这个方法

   getRotateImage(path, bm)

在任何 util 类中添加 getRotateImage 方法作为静态方法并使用它。

public static Bitmap getRotateImage(String photoPath, Bitmap bitmap) throws IOException {
        ExifInterface ei = new ExifInterface(photoPath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap rotatedBitmap = null;
        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = bitmap;
        }

        return rotatedBitmap;

    }


  public static Bitmap rotateImage(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);
    }

【讨论】:

    【解决方案4】:

    我得到了一些工作。当我在横向模式下拍照时,一切都像以前一样正常。如果我以人像模式拍摄照片,我需要将相机倒置并且照片看起来不错。如果您注意到我通过在每个方向前面添加“ExifInterface”来稍微更改您的代码。我使用了以下代码:

        try {
          ExifInterface exif = new ExifInterface(filename); 
          int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                              ExifInterface.ORIENTATION_NORMAL);
          int rotate = 0;
             switch(orientation) {
                case  ExifInterface.ORIENTATION_ROTATE_270:
                     rotate-=90;break;
                case  ExifInterface.ORIENTATION_ROTATE_180:
                     rotate-=90;break;
                case  ExifInterface.ORIENTATION_ROTATE_90:
                     rotate-=90;break;
                }
                  Log.d("Fragment", "EXIF info for file " + filename + ": " + rotate);
             } catch (IOException e) {
                 Log.d("Fragment", "Could not get EXIF info for file " + filename + ": " + e);
             }
    

    【讨论】:

      【解决方案5】:

      你可以简单地旋转它

      在 onActvityresult 中获取使用此代码拍摄的图像的方向 ....

      File imageFile = new File(imageUri.toString());
             ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
             int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
             int rotate = 0;
             switch(orientation) {
               case ORIENTATION_ROTATE_270:
                   rotate-=90;break;
               case ORIENTATION_ROTATE_180:
                   rotate-=90;break
               case ORIENTATION_ROTATE_90:
                   rotate-=90;break
             }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多