Motorola Droid 上的相机预览
我们的应用程序显示相机预览,它似乎在除 Motorola Droid 之外的所有手机上都能正常工作,在摩托罗拉 Droid 上,当我们设置相机参数时,我们会遇到运行时异常:
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:611)
at com.highwaynorth.andrometer.CameraPreviewSurfaceView.surfaceChanged(CameraPreviewSurfaceView.java:57)
at android.view.SurfaceView.updateWindow(SurfaceView.java:460)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
at android.view.ViewGroup.drawChild(ViewGroup.java:1525)
以下是 surfaceChanged() 的代码,主要取自 APIDemos
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewFormat(PixelFormat.YCbCr_422_SP);
parameters.setPreviewFrameRate(1);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
有谁知道什么我们设置参数的方式是否有问题,会导致摩托罗拉 Droid 出现异常?
Our application displays a camera preview and it seems to work fine on all phones except for the Motorola Droid where we get a runtime exception when we set the camera parameters:
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:611)
at com.highwaynorth.andrometer.CameraPreviewSurfaceView.surfaceChanged(CameraPreviewSurfaceView.java:57)
at android.view.SurfaceView.updateWindow(SurfaceView.java:460)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
at android.view.ViewGroup.drawChild(ViewGroup.java:1525)
Here is the code for surfaceChanged() which is mostly taken from APIDemos
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewFormat(PixelFormat.YCbCr_422_SP);
parameters.setPreviewFrameRate(1);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
Does anyone know what is wrong with how we are setting the parameters that would be causing the exception on the Motorola Droid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以告诉你,你的问题出在以下两行之一:
我知道这一点,因为其余的代码正是我在书中的一些相机示例中所做的,并且它们已经在 DROID 上进行了测试。
您可能希望在 Camera.Parameters 对象上使用
getSupportedPreviewFormats()
和getSupportedPreviewFrameRates()
来查看相关设备是否支持您寻求的格式和帧速率。请注意,这些方法对于 Android 2.0 来说是新的,因此它们可以在 DROID/Milestone(大概还有 Nexus One)上工作,但在撰写本文时不能用于其他方法。如果您的目标是较旧的 Android API 版本,则需要使用反射或一些类加载技巧来让这些方法在 Android 2.0 上运行,并在较旧的版本上被跳过。I can tell you your problem is with one of the following two lines:
I know this, because the rest of that code is just what I do in some camera samples in my book, and they've been tested on a DROID.
You may wish to use
getSupportedPreviewFormats()
andgetSupportedPreviewFrameRates()
on your Camera.Parameters object, to see if the device in question supports the format and frame rate you seek. Note that those methods are new to Android 2.0, so they'll work on the DROID/Milestone (and, presumably, the Nexus One), but nothing else at the time of this writing. If you are targeting older Android API versions, you'll need to use reflection or some classloading tricks to get these methods to work on Android 2.0 and be skipped on older versions.您应该检查哪些预览格式可用,以确保您可以在尽可能多的设备上运行。
看起来DROID支持
PixelFormat.YCbCr_422_I
PixelFormat.YCbCr_420_SP
您可以使用以下方法来获取可用格式的列表。
getSupportedPreviewFormats()
像素格式
You should check what preview formats are available to make sure you can run on as many devices as possible.
It looks like DROID supports
PixelFormat.YCbCr_422_I
PixelFormat.YCbCr_420_SP
you can use the following method to get a list of available formats.
getSupportedPreviewFormats()
Pixel Formats
您可能需要调查的另一件事是:
我在运行 Motoblur 并更新至 2.3 的设备(特别是 Verizon 的 Droid2、DroidX 和 Atrix)上遇到此问题。
相机参数很好,但在layout/capture.xml中,ViewfinderView的背景设置为透明:
嗯,看起来Android 2.3上的Motoblur透明并不是那么透明...
删除解决了我的问题。
从ViewFinder中
Another thing you may want to investigate:
I am experiencing this issue for devices running Motoblur and updated to 2.3 (especially Droid2, DroidX and Atrix with Verizon).
The Camera parameters were fine, but in layout/capture.xml the background of the ViewfinderView is set to transparent:
Well, it looks that transparent for Motoblur on Android 2.3 is not that transparent...
removing
from the ViewFinder solved my problem.