android – 从非活动中拍照

前端之家收集整理的这篇文章主要介绍了android – 从非活动中拍照前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在我的代码中使用虚拟表面.它在运行4.2.1的Canvas HD中运行良好但是当我的nexus 5 / S 3上部署了相同的应用程序时,它在camera.takepicture上提供RunTimeException

这是我的代码

  {
    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上的许多问题后,我发现thisthis,但两个片段都用于活动.

我怎么能在我的应用程序中使用这样的代码,以便我可以从背景中捕获图片

最佳答案
您不应该使用虚拟SurfaceView,因为Android检查是SurfaceView显示在屏幕上.

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

如果您计划仅支持5.0设备,或者它是一个不太可用的新应用程序,您应该考虑使用新的Android Camera2 API,它修复了旧API的许多错误.

希望它能帮到你:)

猜你在找的Android相关文章