自定义相机活动的Android图像方向问题 [英] Android image orientation issue with custom camera activity

查看:16
本文介绍了自定义相机活动的Android图像方向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义相机活动来处理我在调用 Intent 图像捕获时遇到的一些问题.用户可以选择保存图像或仅使用从 OnPictureTakenCallback 返回的数据.

I wrote a custom camera activity to handle some issues I've been having with certain android devices when calling intent image capture. The user is able to either select save image or just use the data returned back from the OnPictureTakenCallback.

我遇到的问题是相对于拍摄方向正确显示图像.我通过调用 SetRequestedOrientation 强制活动以纵向显示.

The problem I'm having is displaying the image correctly with respect to the orientation it was taken. I force the activity to be displayed in portrait by calling SetRequestedOrientation.

我如何知道用户拍照时相机的正确方向?即用户可以旋转 90 度(人像)拍摄照片.

How would I know the correct Orientation the camera was in when the user took the picture? i.e. The user could take the picture at a rotation of 90 (portrait).

我尝试在窗口管理器的默认显示上使用 getRotation(),但将请求的方向设置为仅返回 Surface.ROTATION_0 的纵向.

I've tried to get to use the getRotation() on the window manager's default display, but with setting the requested orientation to portrait that only returns Surface.ROTATION_0.

更新:为了澄清我的另一个问题,如果用户不保存图像,我如何仅从图片回调中的 byte[] 数据确定方向?

Update: To clarify my other issue, how could I determine the orientation from just the byte[] data in the picture callback if the user were to not save the image?

更新:使用此代码尝试以下答案后,我得到的只是 ExifInterface.ORIENTATION_NORMAL.我还更改了代码以仅保存从相机返回的文件,因为我不确定是否有一种简单的方法可以通过 byte[] 数据确定方向.

Update: After trying the answers below with this code all I'm getting is ExifInterface.ORIENTATION_NORMAL. I've also changed my code to just save the file returned from the camera as I'm not sure there is an easy way to determine the orientation with just having the byte[] data.

    private PictureCallback mPicture = new PictureCallback() 
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
                    "MyApp");
            if(!directory.exists())
            {
                if(!directory.mkdirs())
                {
                    Log.d("CAMERA", "Unable to create directory to save photos.");
                    return;
                }
            }
            File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data);
            fos.close();
            ExifInterface exif = new ExifInterface(file.getCanonicalPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int rotate = 0;

            switch (orientation) 
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                   rotate = 90;
                   break; 
                case ExifInterface.ORIENTATION_ROTATE_180:
                   rotate = 180;
                   break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                   rotate = 270;
                   break;
                case default:
                   break;
             }
        }
    };

推荐答案

所以你遇到了相机方向的问题.

SO you are facing some issue with the orientation of the camera.

此链接显示了一个简单的相机捕捉活动的示例应用程序:http://labs.makemachine.net/2010/03/simple-android-photo-capture/

This link shows an example app of a simple camera capture activity : http://labs.makemachine.net/2010/03/simple-android-photo-capture/

也许您应该尝试通过执行以下操作来固定方向:

Maybe you should try fixing the orientation by doing something like this :

          ExifInterface exif = new ExifInterface(_path);
          int exifOrientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);

          int rotate = 0;

          switch (exifOrientation) {
          case ExifInterface.ORIENTATION_ROTATE_90:
          rotate = 90;
          break; 

         case ExifInterface.ORIENTATION_ROTATE_180:
         rotate = 180;
         break;

         case ExifInterface.ORIENTATION_ROTATE_270:
         rotate = 270;
         break;
         }

           if (rotate != 0) {
          int w = bitmap.getWidth();
          int h = bitmap.getHeight();

// Setting pre rotate
          Matrix mtx = new Matrix();
          mtx.preRotate(rotate);

         // Rotating Bitmap & convert to ARGB_8888, required by tess
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
         bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
       }

这篇关于自定义相机活动的Android图像方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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