在android中,拍摄的照片方向正在改变

在android中,拍摄的照片方向正在改变,android,bitmap,camera,Android,Bitmap,Camera,我正在点击一个按钮打开相机应用程序。并在下一个活动中显示捕获的照片。但是拍摄的照片旋转了90度。当我捕获图像后在视图中显示它时,它的方向始终是横向的。为什么在纵向模式下拍摄照片时,照片不会以纵向模式显示 单击一个按钮: Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(n

我正在点击一个按钮打开相机应用程序。并在下一个活动中显示捕获的照片。但是拍摄的照片旋转了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));

你可以简单地旋转它

在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
       }

我在三星手机上也遇到了同样的问题。显然,三星手机设置了EXIF方向标签,而不是旋转单个像素。使用BitmapFactory读取位图不支持此标记。我发现此问题的解决方案是在活动的onActivityResult方法中使用ExiFinInterface。该方法检查与相机捕获图像的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);

我让它有些工作。当我在横向模式下拍照时,一切都像以前一样正常。如果我在肖像模式下拍照,我需要将相机倒置,照片看起来很好。如果您注意到我通过在每个方向前面添加“ExiFinInterface”稍微更改了代码。我使用了以下代码:

    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);
         }

请改用滑翔。它本身固定了方向,与您使用的手机无关。这是一个样品

Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury);
使用位图和位图的路径调用此方法

在任何util类中将getRotateImage方法添加为static并使用它


是否需要在结尾旋转位图?您需要在结尾旋转位图。谢谢。它起作用了。但我有一点怀疑。我们在捕获图像后旋转图像。它是否也会旋转其他三星设备中拍摄的照片?如果照片在XXXX设备中被正确捕获怎么办?在其他设备中int-orientation=exif.getAttributeInt(ExifInterface.TAG\u-orientation,ExifInterface.orientation\u-NORMAL);函数返回0角度,所以永远不会调用switch语句,所以方向保持原样。我面临着严重的代码问题。。三星galaxy s4设备重新启动。。你能帮助我吗?为什么会发生这种情况?这里的答案可能重复。我得到的定向旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转旋转
   getRotateImage(path, bm)
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);
    }