从非活动中拍照 [英] Taking picture from non-activity

查看:68
本文介绍了从非活动中拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用了虚拟表面。在运行4.2.1的Canvas HD中可以正常工作,但是当在我的nexus 5 / S 3上部署相同的应用程序时,它会在camera.takepicture上给出RunTimeException

I am using dummy surface in my code.It's working fine in Canvas HD running 4.2.1 but when the same app is deployed on my nexus 5/S 3 it gives RunTimeException on camera.takepicture

这是我的代码

  {
    camera = Camera.open(cameraId);
    if (camera != null)
   {
    SurfaceView dummy = new SurfaceView(context);
    try {
    camera.setPreviewDisplay(dummy.getHolder());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    camera.startPreview();
    camera.takePicture(null, null, new PhotoHandler(context));

    }

Logcat:

07-17 22:46:49.281: E/AndroidRuntime(481): FATAL EXCEPTION: main
07-17 22:46:49.281: E/AndroidRuntime(481): Process: com.example.ex, PID: 481
07-17 22:46:49.281: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex/com.example.ex.MainActivity}: java.lang.RuntimeException: takePicture failed
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:136)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:5001)
07-17 22:46:49.281: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:515)
07-17 22:46:49.281: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-17 22:46:49.281: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-17 22:46:49.281: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481): Caused by: java.lang.RuntimeException: takePicture failed
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.hardware.Camera.native_takePicture(Native Method)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.hardware.Camera.takePicture(Camera.java:1245)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.hardware.Camera.takePicture(Camera.java:1190)
07-17 22:46:49.281: E/AndroidRuntime(481):  at com.example.ex.MainActivity.capturephoto(MainActivity.java:63)
07-17 22:46:49.281: E/AndroidRuntime(481):  at com.example.ex.MainActivity.onCreate(MainActivity.java:26)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.Activity.performCreate(Activity.java:5231)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-17 22:46:49.281: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

在搜寻并搜索了许多关于stackoverflow的问题后,我发现 ,但这两个代码段都用于活动中。

After googling and searching many questions on stackoverflow I found this and this but both snippets are used in activties .

我该如何在我的应用中使用这样的代码,以便可以从背景中捕获图片

How could I use such code in my app so that I can capture picture from background

推荐答案

您不应使用虚拟的SurfaceView,因为Android会检查是否在屏幕上显示了SurfaceView。

You shouldn't use a dummy SurfaceView as Android checks is the SurfaceView is displayed on the screen.

相反,使用WindowManager在类型为SYSTEM_OVERLAY的SurfaceView上显示预览。将您的SurfaceView宽度和高度设置为1px,将不透明度设置为0以隐藏预览,就是这样!

Instead, display the preview on a SurfaceView with WindowManager, with type SYSTEM_OVERLAY. Set your SurfaceView width and height to 1px and the opacity to 0 to hide the preview, and that's it!

如果您计划仅支持5.0+设备,或者即将推出的新应用,您应该考虑使用新的android Camera2 API,该API修复了旧API的许多错误。

If you plan to support only 5.0+ devices, or it's a new app that won't be available too soon, you should consider using the new android Camera2 API, which fixed many mistakes of the older API.

希望它会为您提供帮助:)

Hope it will help you :)

这篇关于从非活动中拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆