Android图像定向问题与自定义相机活动

Android图像定向问题与自定义相机活动,android,camera,orientation,Android,Camera,Orientation,我写了一个定制的摄像头活动来处理我在调用intent image capture时遇到的某些android设备问题。用户可以选择保存图像,也可以只使用从onPictureTakeCallback返回的数据 我遇到的问题是,根据拍摄的方向正确显示图像。我通过调用SetRequestedOrientation强制以纵向显示活动 当用户拍照时,我如何知道相机的正确方向? i、 e.用户可以旋转90度(纵向)拍照 我尝试在窗口管理器的默认显示上使用getRotation(),但将请求的方向设置为仅返回S

我写了一个定制的摄像头活动来处理我在调用intent image capture时遇到的某些android设备问题。用户可以选择保存图像,也可以只使用从
onPictureTakeCallback
返回的数据

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

当用户拍照时,我如何知道相机的正确方向? i、 e.用户可以旋转90度(纵向)拍照

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

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

更新:在用这段代码尝试了下面的答案之后,我得到的是ExifInterface.ORIENTATION\u 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;
             }
        }
    };

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

ExifInterface exif = new ExifInterface(SourceFileName);
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
资料来源:

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


来源:

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

此链接显示了一个简单的摄像头捕获活动的示例应用程序:

也许你应该试着通过这样做来修正方向:

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

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

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

}
public void surfaceCreated(SurfaceHolder持有者){
试一试{
mCamera.setPreviewDisplay(支架);
Camera.Parameters=mCamera.getParameters();
如果(this.getResources().getConfiguration().orientation=
配置。方向(景观)
{

参数设置(“方向”、“纵向”);您应该能够调用“方向”在CameroRainfo上。根据文档,这是从API级别9开始提供的,即Android 2.3。我的应用程序的API级别最低为8。似乎对我有效。再次粘贴:我使用此代码在picturecalback方法中旋转图片,但有些方法不起作用。你能帮我吗?我会投票给你帮助。你到底不起作用的是什么u?这段代码用于调整方向。您可以轻松配置
createBitmap
函数和
Matrix
。这对我来说非常有用。我需要弄清楚的一件事是获取位图实例,如下所示:bitmap=BitmapFactory.decodeFile(imagePath,options);感谢分享此综合解决方案同一问题我需要帮助,有人能帮我吗
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();
    }

}