Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Several Camera API Questions

I am using the code provided at the following URL to try to work with the Android Camera API:

http://marakana.com/forums/android/examples/39.html

This has raised several questions that I have tried in vain to find the answers to so far.

1) My application needs to be in portrait orientation, but all the code examples I have seen (included that at the URL mentioned above) all seem to depend on landscape orientation. In fact, no matter what I have tried so far, it seems that landscape is inescapable. I have tried forcing the parameters in surfaceCreated(...) like so:

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.set("rotation", "90");
camera.setParameters(parameters);

I have tried doing the same thing in surfaceChanged(...). Of course, I am also setting my orientation to portrait in the manifest as follows:

android:screenOrientation="portrait"

Does anyone have any suggestions on what I'm doing wrong and how to fix it?

2) Another question I have has to do with releasing the camera resources. In the code from the article referenced above, the following are called in the surfaceDestroyed(...) method:

camera.stopPreview();
camera = null;

There is nothing to release the camera resources, so after you have run this application, any subsequent application that uses the camera will not work. In am attempt to fix this, I added a call to release the resources, like this:

camera.stopPreview();
camera.release();
camera = null;

The problem there, though, is that when I close the application, I get a "Force Close" with the following exception in LogCat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
    at android.hardware.Camera.setHasPreviewCallback(Native Method)
    at android.hardware.Camera.access$600(Camera.java:58)
    at android.hardware.Camera.$EventHandler.handleMessage(Camera.java:344)
    at android.os.Handler.dispatchMessage(Handler.java.99)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lanf.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

Can anyone tell me why I can't release the camera resources there?

Thank you all in advance. This camera thing is driving me nuts.

P.S. I am testing all of this on an HTC Evo.

like image 466
mahdaeng Avatar asked Jan 21 '23 17:01

mahdaeng


2 Answers

For (1) if you are developing for api level 9 you can try the code that is posted on the developer website.

For (2) you need to make sure that you do the following:

    if (mCamera != null) {
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }

Basically before you call release() you need to setPreviewCallback to null. THis is a known issue with the camera stack

like image 64
YSD Avatar answered Jan 28 '23 01:01

YSD


About your second question: here someone posted a work-around.

like image 33
eduardo Avatar answered Jan 28 '23 00:01

eduardo