Android:几个相机 API 问题
我正在使用以下 URL 提供的代码来尝试使用 Android 相机 API:
http ://marakana.com/forums/android/examples/39.html
这提出了几个问题,到目前为止我一直徒劳地试图找到答案。
1) 我的应用程序需要处于纵向,但我见过的所有代码示例(包括上面提到的 URL 中的代码示例)似乎都依赖于横向。事实上,无论我迄今为止如何尝试,风景似乎都是不可避免的。我尝试像这样强制在 surfaceCreated(...)
中使用参数:
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.set("rotation", "90");
camera.setParameters(parameters);
我尝试在 surfaceChanged(...)
中执行相同的操作。当然,我还在清单中将方向设置为纵向,如下所示:
android:screenOrientation="portrait"
有人对我做错了什么以及如何解决它有任何建议吗?
2)我的另一个问题与释放相机资源有关。在上面引用的文章的代码中,在 surfaceDestroyed(...)
方法中调用了以下内容:
camera.stopPreview();
camera = null;
没有任何东西可以释放相机资源,因此在运行此应用程序后,任何后续应用程序使用相机的将无法工作。为了解决这个问题,我添加了一个调用来释放资源,如下所示:
camera.stopPreview();
camera.release();
camera = null;
但是,那里的问题是,当我关闭应用程序时,我在 LogCat 中收到“强制关闭”,但出现以下异常:
FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
at android.hardware.Camera.setHasPreviewCallback(Native Method)
at android.hardware.Camera.access$600(Camera.java:58)
at android.hardware.Camera.$EventHandler.handleMessage(Camera.java:344)
at android.os.Handler.dispatchMessage(Handler.java.99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lanf.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
谁能告诉我我为什么不能在那里释放相机资源?
预先感谢大家。这个相机的东西快把我逼疯了。
PS 我正在 HTC Evo 上测试所有这些。
I am using the code provided at the following URL to try to work with the Android Camera API:
http://marakana.com/forums/android/examples/39.html
This has raised several questions that I have tried in vain to find the answers to so far.
1) My application needs to be in portrait orientation, but all the code examples I have seen (included that at the URL mentioned above) all seem to depend on landscape orientation. In fact, no matter what I have tried so far, it seems that landscape is inescapable. I have tried forcing the parameters in surfaceCreated(...)
like so:
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.set("rotation", "90");
camera.setParameters(parameters);
I have tried doing the same thing in surfaceChanged(...)
. Of course, I am also setting my orientation to portrait in the manifest as follows:
android:screenOrientation="portrait"
Does anyone have any suggestions on what I'm doing wrong and how to fix it?
2) Another question I have has to do with releasing the camera resources. In the code from the article referenced above, the following are called in the surfaceDestroyed(...)
method:
camera.stopPreview();
camera = null;
There is nothing to release the camera resources, so after you have run this application, any subsequent application that uses the camera will not work. In am attempt to fix this, I added a call to release the resources, like this:
camera.stopPreview();
camera.release();
camera = null;
The problem there, though, is that when I close the application, I get a "Force Close" with the following exception in LogCat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
at android.hardware.Camera.setHasPreviewCallback(Native Method)
at android.hardware.Camera.access$600(Camera.java:58)
at android.hardware.Camera.$EventHandler.handleMessage(Camera.java:344)
at android.os.Handler.dispatchMessage(Handler.java.99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lanf.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Can anyone tell me why I can't release the camera resources there?
Thank you all in advance. This camera thing is driving me nuts.
P.S. I am testing all of this on an HTC Evo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 (1),如果您正在开发 api 级别 9,您可以尝试 开发者网站。
对于(2),您需要确保执行以下操作:
基本上在调用release() 之前,您需要将PreviewCallback 设置为null。这是相机堆栈的一个已知问题
For (1) if you are developing for api level 9 you can try the code that is posted on the developer website.
For (2) you need to make sure that you do the following:
Basically before you call release() you need to setPreviewCallback to null. THis is a known issue with the camera stack
关于你的第二个问题:
此处有人发布了解决方法。
About your second question:
here someone posted a work-around.
执行以下代码:
然后,在
onResume
函数中重新初始化相机。Do this code:
Then, re initialize the camera in
onResume
function.