简体   繁体   English

如何在Android中设置相机方向?

[英]How to set camera orientation in Android?

I've looked at other questions where people have asked this, but I wasn't sure what the best way to solve this problem was. 我已经看过其他人问过的其他问题,但是我不确定解决此问题的最佳方法是什么。 After going through Android's guide to implement the camera, the camera preview I have is locked into landscape. 阅读完Android指南以实现相机后,我拥有的相机预览被锁定为横向。 I wanted to know how to adjust the camera orientation to mirror the orientation of the phone. 我想知道如何调整相机的方向以反映手机的方向。

I tried this 我试过了

camera.setDisplayOrientation(90);

And that locked the camera into portrait mode, so I need another solution (preferably one that works for most devices). 并将相机锁定为纵向模式,因此我需要另一种解决方案(最好是适用于大多数设备的解决方案)。

Also, how can I make it so the saved image shows up the same way as the preview? 另外,如何使保存的图像显示与预览相同的方式? Right now if I use that line of code above to make the preview go to portrait, the saved image is still rotated for a landscape. 现在,如果我使用上面的代码行使预览变为纵向,则保存的图像仍将旋转为横向。

 Camera.Parameters params= mCamera.getParameters();
 params.set("rotation", 90); 
 mCamera.setParameters(params);

You need to take the orientation into account when determining what preview size to choose. 确定要选择的预览大小时,需要考虑方向。 For example, the getOptimalPreviewSize() from the CameraPreview of ApiDemos is oblivious to orientation, simply because their version of that app has the orientation locked to landscape. 例如, CameraPreview的CameraPreview中的getOptimalPreviewSize()忽略了方向,这仅仅是因为其应用程序的版本已将方向锁定为横向。 If you wish to let the orientation float, you need to reverse the target aspect ratio to match. 如果要让方向浮动,则需要反转目标纵横比以匹配。 So, where getOptimalPreviewSize() has this 因此,在getOptimalPreviewSize()具有此位置的地方

double targetRatio=(double)width / height;

And

if (displayOrientation == 90 || displayOrientation == 270) {
  targetRatio=(double)height / width;
}

where displayOrientation is a value from 0 to 360, that I am determining from about 100 lines of some seriously ugly code, which is why I am wrapping all of this up in a reusable component that I'll publish shortly. 其中displayOrientation是一个介于0到360之间的值,该值是我从大约100行严重丑陋的代码中确定的,这就是为什么我将所有这些都包装在我不久将要发布的可重用组件中的原因。

Second, you need to take that display orientation into account when controlling the aspect ratio of the SurfaceView / TextureView that you are using. 其次,在控制所使用的SurfaceView / TextureView的纵横比时,需要考虑该显示方向。 The CameraPreview activity from ApiDemos has its own Preview ViewGroup that handles the aspect ratio, and there you need to reverse the aspect ratio for use in portrait: CameraPreview从活动ApiDemos都有自己的预览ViewGroup处理该宽高比,在那里你需要扭转在肖像使用长宽比:

if (displayOrientation == 90
        || displayOrientation == 270) {
      previewWidth=mPreviewSize.height;
      previewHeight=mPreviewSize.width;
    }
    else {
      previewWidth=mPreviewSize.width;
      previewHeight=mPreviewSize.height;
    }

where displayOrientation is that same value (90 and 270 being portrait and reverse-portrait respectively, and note that I haven't tried getting reverse-portrait or reverse-landscape to work, so there may be more tweaking required). 其中displayOrientation是相同的值(90和270分别是纵向和反向肖像,请注意,我没有尝试使反向肖像或反向风景正常工作,因此可能需要进行更多调整)。

Third - you must start the preview before calling setPictureSize() on the Camera.Parameters . 三-你必须在调用之前启动预览setPictureSize()Camera.Parameters Otherwise, it's as if the aspect ratio of the picture is applied to the preview frames, screwing things up. 否则,就好像将图片的纵横比应用于预览帧一样,搞砸了。

Camera.Parameters parameters=camera.getParameters();
Camera.Size pictureSize=getHost().getPictureSize(parameters);

parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

camera.setParameters(parameters);
camera.startPreview();

parameters=camera.getParameters();
parameters.setPictureSize(pictureSize.width, pictureSize.height);
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);

I fixed this using the Google's camera app. 我使用Google的相机应用程序修复了此问题。 It gets the phone's orientation by using a sensor and then sets the EXIF tag appropriately. 它通过使用传感器获取手机的方向,然后适当设置EXIF标签。 The JPEG which comes out of the camera is not oriented automatically. 相机发出的JPEG不会自动定位。

Also, the camera preview works properly only in the landscape mode. 另外,相机预览仅在横向模式下才能正常工作。 If you need your activity layout to be oriented in portrait, you will have to do it manually using the value from the orientation sensor. 如果您需要将活动布局定向为纵向,则必须使用方向传感器中的值手动进行。

I'd recommend checking the photo's exif data and looking particularly for 我建议检查照片的exif数据,并特别寻找

ExifInterface exif = new ExifInterface(SourceFileName);   //API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

Leave comment and Let me know if this helps you. 发表评论,让我知道这是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM