Android 相机表面视图方向
好的,我有一个扩展 SurfaceView 并覆盖 surfaceChanged 的类
- 只需调用 startPreview
surfaceCreated - 打开相机,编辑参数*,设置surfaceHolder
surfaceDestroyed - 调用 stopPreview,释放相机,
这一切都很好,因为当方向为纵向时:
来自 surfaceCreated *
m_camera = Camera.open();
Camera.Parameters p = m_camera.getParameters();
if (getResources().getConfiguration().orientation !=
Configuration.ORIENTATION_LANDSCAPE)
{
p.set("orientation", "portrait");
// CameraApi is a wrapper to check for backwards compatibility
if (CameraApi.isSetRotationSupported())
{
CameraApi.setRotation(p, 90);
}
}
但是,每次方向更改时,它都会调用 Camera.open()...正如您所知,这是一个相当昂贵的操作,导致过渡并不那么顺利。
当我强制方向为横向时,预览非常棒。创建仅被调用一次,这是有效的,因为预览是横向的,相机始终是用户看到的。但是,我需要一种方法来设置纵向拍摄的实际照片的方向。但当我强制横向时,表面永远不会被重新创建,并且当相机处于纵向状态时,参数永远不会设置。
那么我怎样才能(仅)执行以下操作之一?
当方向改变时,在 onDestroy 和 onCreate 之间按住 m_camera,以便平滑过渡
强制横向并检测方向以另一种方式更改...如果纵向拍摄,则旋转最终拍摄的图片。
另外,如果我偏离了基地,有人可以给我指出更好的方向吗?谢谢。
Ok so I have a class that extends SurfaceView and overrides
surfaceChanged - just calls startPreview
surfaceCreated - opens camera, edits params *, sets surfaceHolder
surfaceDestroyed - calls stopPreview, release camera
this all work great because when the orientation is Portrait:
from surfaceCreated *
m_camera = Camera.open();
Camera.Parameters p = m_camera.getParameters();
if (getResources().getConfiguration().orientation !=
Configuration.ORIENTATION_LANDSCAPE)
{
p.set("orientation", "portrait");
// CameraApi is a wrapper to check for backwards compatibility
if (CameraApi.isSetRotationSupported())
{
CameraApi.setRotation(p, 90);
}
}
However, everytime the orientation changes it calls Camera.open()... which as you may know is quite an expensive operation, causing the transitions to be not so smooth.
When i force the orientation to landscape, the preview is great. Create only gets called once which works because the preview is in landscape the camera is always what the user sees. However, I need a way to set the orientation of the actual picture taken when in portrait. When I force landscape though, the surface never gets recreated and the parameters are never set when the camera is held in portrait.
So how can I do one of the following (exclusively)?
Hold onto m_camera between onDestroy and onCreate when orientation changes so that the transition is smooth
Force landscape and detect orientation changes another way... rotating the final snaped picture if held in portrait.
Also, if I am off base can someone point me in a better direction? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我实现它的方式:
然后在你的 PictureCallback 设置元数据来指示旋转级别:
我希望它有帮助。
更新 现在,当出现基于横向的设备时,需要在 OrientationEventListener 中进行额外检查。
完整代码(LC 有点浪费,但很容易演示该方法)
The way I implemented it:
And then in your PictureCallback set metadata to indicate rotation level:
I hope it helps.
UPDATE Now when landscape based devices are appearing an additional check for it is required in OrientationEventListener.
Full code (a bit wasteful by LC, but easily demonstrates the approach)
您是否考虑过使用API文档中提供的标准方法,您可以在surfaceChanged上调用该方法?您可以将度数存储在全局变量中,以便稍后保存图片时使用。还可以对相机变量执行简单的空检查,这样您就不会在 surfaceCreated 中再次创建它。
Have you considered using the standard method thats provided in the API doc, which you can call on surfaceChanged? You could store the degrees in a global variable to later use when saving the picture. Also could do a simple null checker on your camera variable, so you don't create it again in surfaceCreated.
正如您从其他答案中看到的,这段代码变得非常复杂。您可能想要研究使用库来帮助您提供此功能,例如,CWAC-Camera 支持 OS 2.3 及更高版本(希望您现在可以放弃 OS 2.1 和 OS 2.2 支持):
https://github.com/commonsguy/cwac-camera
CWAC-Camera 支持锁定相机预览横向,并会自动将图像旋转到适合您的正确方向。如果您想了解所有需要解决的设备特定问题,请浏览项目问题解决了,在我看来,这是尝试使用库而不是维护所有这些代码并自行测试的更多原因。
As you've seen from the other answers, this code gets very complicated. You may want to investigate using a library to help you provide this feature, for example, CWAC-Camera supports OS 2.3 and up (hopefully you can drop OS 2.1 and OS 2.2 support now):
https://github.com/commonsguy/cwac-camera
CWAC-Camera supports locking the camera preview to landscape, and will auto-rotate images into the correction orientation for you. Browse the project issues if you want a taste of all the device specific problems that need to be solved, which IMO are more reasons for trying to use a library instead of maintaining all this code and testing yourself.