6

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

Here's my 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:

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)

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
someone
  • 427
  • 2
  • 11

3 Answers3

5

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(...);

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

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

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • I'll try this when I return to work June 8 and let you know if it works. (Have been stuck on this dummy API for almost 1 week) – Louis CAD May 27 '15 at 21:43
1

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

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!

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 :)

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
  • A dummy SurfaceTexture is a simpler approach, as oddly-sized and placed SurfaceViews have had problems on some devices and OS releases. – Eddy Talvala May 27 '15 at 19:42
0

This is not possible, you need an activity to display the camera in.

Consider a transparent activity that just holds the camera view.

r2DoesInc
  • 3,759
  • 3
  • 29
  • 60
  • I have used dummy surface earlier which captures a picture when phone is switched on.I think there should be some workaround to do it – someone Jul 18 '14 at 19:03
  • It is possible within a service as you can display something with WindowManager – Louis CAD May 06 '15 at 15:21