Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking picture from non-activity

Tags:

android

camera

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

like image 700
someone Avatar asked Jul 18 '14 18:07

someone


2 Answers

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.

like image 95
Eddy Talvala Avatar answered Nov 14 '22 23:11

Eddy Talvala


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

like image 26
Louis CAD Avatar answered Nov 15 '22 00:11

Louis CAD