拍照非activtyactivty

2023-09-05 09:45:01 作者:相亲↘丨排队℡

我使用虚拟表面在我的code.It的做工精细的画布HD运行4.2.1但是,当相同的应用程序部署在我的关系5 / S 3它给出的RuntimeException的camera.takepicture

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

下面是我的code

  {
    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的:

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)

谷歌搜索和搜索上的计算器,我发现了很多问题this和this但两个片段是用在工作之外的活动。

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

我怎么可以用这样的code。在我的应用程序,这样我可以捕获从背景图像

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

推荐答案

您可以使用一个虚拟的表面纹理,而不是一个SurfaceView。这是可靠的,对老相机API工作正常:

You can use a dummy SurfaceTexture instead of a SurfaceView. This is reliable and works fine on the old camera API:

mDummyTexture = new SurfaceTexture(1); // pick a random argument for texture id
mCamera.setPreviewTexture(mDummyTexture);
mCamera.startPreview();
mCamera.takePicture(...);

只要确保保持mDummyTexture作为类的成员,或者你可能会废弃的错误。

Just make sure to keep mDummyTexture as a member of your class, or you may get abandoned surface errors.

只要你不叫#表面纹理updateTexImage(),你不需要OpenGL上下文。

As long as you don't call SurfaceTexture#updateTexImage(), you do not need an OpenGL context.