如果我将活动的方向限制为仅在清单中为人像,我能否获得在我的应用中拍摄的照片的方向? [英] Can I get the orientation of a photo taken in my app if I limit the activity's orientation to portrait only in my manifest?

查看:87
本文介绍了如果我将活动的方向限制为仅在清单中为人像,我能否获得在我的应用中拍摄的照片的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在我的应用程序中完全支持横向UI,但是我希望能够自动旋转用户在横向模式下拍摄的照片. 当前,如果用户以横向模式拍摄照片,则它会像照人像一样保留在屏幕上(照片的水平是垂直的).

I don't want to support landscape UI at all across my app, but I want to be able to automatically rotate the photos users take in landscape mode. Currently if a user takes a photo in landscape mode, it remains on screen as if it was taken in portrait (the horizon in the photo is vertical).

我试图像这样从系统中获取方向:

I've tried to get the orientation from the system like this:

val display = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val screenOrientation = display.rotation
Log.d("orientation", screenOrientation.toString())

但是它不起作用,每次都会返回0.

But it doesn't work and just gives back 0 every time.

我尝试了其他一些解决方案,但无法使其正常工作.

I've tried a few other solutions but couldn't get them to work.

我想知道,我是在浪费时间试图解决这个问题吗?如果我的活动仅以人像模式进行操作,甚至可以知道拍摄照片的方向吗? 那里有几篇关于相机方向的文章,但没有谈论他们活动中的方向是否被锁定.

I am wondering, am I wasting my time trying to figure this out? Is it even possible to know the orientation in which the photo was captured if my activity only operates in portrait mode? There are a few articles out there talking about camera orientation but they don't talk about whether the orientation in their activity is locked or not.

谢谢.

推荐答案

    //Yes, find the Orientation Using ExifInterface
       try{
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                                ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);

     // dont know exactly but, orientation for landscape may be 180 or 270.
     //  From the orientation value you can convert image according to orientation and can be set it vertically, horrizentally as below

             int rotate = 0;
             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.e(Common.TAG, "Exif orientation: " + orientation);
            }
       catch (Exception e) {
           e.printStackTrace();
       }

      // Now Change image as your wish...
      // Image rotation //
      Matrix matrix = new Matrix();
      matrix.postRotate(orientation);
      Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);  

这篇关于如果我将活动的方向限制为仅在清单中为人像,我能否获得在我的应用中拍摄的照片的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆