Android 不使用 SurfaceHolder 拍照
当尝试从服务(无 GUI)拍照时,在某些设备上,我收到 RuntimeException。但是,如果我使用 SurfaceHolder 从 Activity 中拍照,它就会起作用。
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
SurfaceView mview = new SurfaceView(context);
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null,null,iccb);
此序列在大多数设备上运行良好,但特别是在 Motorola Droid 上,对 takePicture 方法(实际上是本机方法)的调用将抛出 RuntimeException。还尝试了camera.setPreviewDisplay(null),但它也不起作用。
但是,如果我在 Activity 中使用真正的 SurfaceView,它在 Droid 上也可以正常工作,但我需要从后台服务中执行此操作。
您是否认为这种行为是出于隐私原因,以至于没有人可以在不实际显示图像的情况下拍照?这对我来说没有多大意义,因为我可以加载一个具有 0 大小预览的虚拟活动,并且它可以工作,但我希望不必这样做。
LogCat 输出:
10-24 12:20:57.838 D/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:189 enableMsgType - Preview callback not enabled 1c2
10-24 12:20:57.838 E/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:2448 takePicture - takePicture called with image buffer 0x0
10-24 12:20:57.838 D/AndroidRuntime(29248): Shutting down VM
10-24 12:20:57.838 W/dalvikvm(29248): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
10-24 12:20:57.846 E/AndroidRuntime(29248): FATAL EXCEPTION: main
10-24 12:20:57.846 E/AndroidRuntime(29248): java.lang.RuntimeException: takePicture failed
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.native_takePicture(Native Method)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:829)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:793)
非常感谢任何帮助
When trying to take a picture from a Service (no GUI), on some devices, I get a RuntimeException. However if I take a picture from an Activity using a SurfaceHolder, it will work.
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
SurfaceView mview = new SurfaceView(context);
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null,null,iccb);
This sequence works fine on most devices but, in particular, on Motorola Droid, the call to takePicture method (actually the native method) will throw a RuntimeException. Also tried camera.setPreviewDisplay(null), but it won't work either.
However, if I use real SurfaceView from an Activity it also works fine on the Droid but I would need to do it from a background service.
Do you think that this behavior is shown due to privacy reasons so that noone can take pictures without actually displaying the image? This makes no much sense to me because I could load up a dummy activity with a 0 size preview and it works but I'd love not having to do it.
LogCat output:
10-24 12:20:57.838 D/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:189 enableMsgType - Preview callback not enabled 1c2
10-24 12:20:57.838 E/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:2448 takePicture - takePicture called with image buffer 0x0
10-24 12:20:57.838 D/AndroidRuntime(29248): Shutting down VM
10-24 12:20:57.838 W/dalvikvm(29248): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
10-24 12:20:57.846 E/AndroidRuntime(29248): FATAL EXCEPTION: main
10-24 12:20:57.846 E/AndroidRuntime(29248): java.lang.RuntimeException: takePicture failed
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.native_takePicture(Native Method)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:829)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:793)
Any help is very much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个安全问题。不应该允许您在没有预览视图的情况下使用相机。这应该可以防止在用户不知情的情况下恶意使用相机,例如出于间谍目的。
It's a security issue. You shouldn't be alowed to use the camera without hacing a preview view. This should prevent malicious use of camera without the knowledge of the user, for example for spying purposes.
似乎大多数现代 Android 设备现在都会检查
SurfaceView
是否存在,并且宽度和高度不为零(我的三星 Galaxy SII 在这种情况下会发出警告,并且不会捕获照片)。有趣的是,如果SurfaceView
的宽度和高度为1dip
那么它就可以正常工作。由于
SurfaceView
的宽度和高度均为 1 像素,因此很难看到,尽管它可以正常工作,但它仍然属于“安全问题”类别。It seems most of the modern Android devices now check that a
SurfaceView
exists and has a non-zero width and height (my Samsung Galaxy SII spews warnings in such a circumstance and won't capture the photo). Interestingly if theSurfaceView
has a width and height of1dip
then it works just fine.With a width and height of 1 pixel the
SurfaceView
is incredibly difficult to see and whilst it works, it still falls into the category of "security issue".