【问题标题】:Android image orientation issue with custom camera activity自定义相机活动的Android图像方向问题
【发布时间】:2012-07-25 08:46:09
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

标签: android camera orientation


【解决方案1】:

所以你在相机的方向上遇到了一些问题。

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

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

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

【讨论】:

  • 似乎对我有用..再次粘贴它:labs.makemachine.net/2010/03/simple-android-photo-capture
  • 我已使用此代码在 picturecalback 方法中旋转图片,但有些无法正常工作,您能帮帮我吗?我会投票给您以寻求帮助
  • 究竟什么不适合你?这段代码用于调整方向。您可以轻松配置createBitmap函数和Matrix。
  • 这对我很有用。我需要弄清楚的一件事是获取位图实例,如下所示:bitmap = BitmapFactory.decodeFile(imagePath, options); 感谢分享这个全面的解决方案
  • 同样的问题我需要帮助stackoverflow.com/questions/28379130/…
【解决方案2】:

您需要从原始 JPEG 中读取元数据以验证照片的拍摄方向。

ExifInterface exif = new ExifInterface(SourceFileName);
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

来源:How to determine orientation of picture without ExifInterface?

编辑:回答您的编辑,您是否尝试过使用回调中传递的 Camera 对象可用的 getCameraInfo() 方法?它有你需要的信息吗?

来源:http://developer.android.com/reference/android/hardware/Camera.html

【讨论】:

【解决方案3】:
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(holder);
        Camera.Parameters parameters = mCamera.getParameters();
        if (this.getResources().getConfiguration().orientation !=
                Configuration.ORIENTATION_LANDSCAPE)
        {
            parameters.set("orientation", "portrait"); <----THis gets the job done!!!
            // For Android Version 2.2 and above
            mCamera.setDisplayOrientation(90);
            // For Android Version 2.0 and above
            parameters.setRotation(90);
        }


        // End Effects for Android Version 2.0 and higher
        mCamera.setParameters(parameters);
    }
    catch (IOException exception)
    {
        mCamera.release();
    }

}

【讨论】:

    【解决方案4】:

    删除setRequestedOrientation() 允许getWindowManager().getDefaultDisplay().getRotation() 提供正确的旋转。我猜设置请求的方向可以防止活动在配置更改时重绘自身,因此设备不知道任何类型的旋转更改。我现在唯一的问题是从 0 度方向的横向模式切换到横向模式 180 度旋转不会触发这个:

    @Override
    public void onConfigurationChanged(Configuration newconfig)
    {
        super.onConfigurationChanged(newconfig);
    
    }
    

    【讨论】:

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