Android: a few camera API questions

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

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

This raised several questions, which I tried in vain to find answers so far.

1) My application should be in portrait orientation, but all the code examples that I saw (including the URL above) all seem to depend on the landscape orientation. In fact, no matter what I have tried so far, it seems that the landscape is inevitable. I tried to force the parameters in the surfaceCreated(...)following way:

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

I tried to do the same in surfaceChanged(...). Of course, I also set my portrait orientation in the manifest as follows:

android:screenOrientation="portrait"

- , , ?

2) , . , , surfaceDestroyed(...) :

camera.stopPreview();
camera = null;

, , , . , , :

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

, , , "Force Close" 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)

- , ?

. .

P.S. HTC Evo.

+3
4

(1), api 9, , - .

(2) , :

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

release() PreviewCallback null.

+8

: - .

+1

Make this code:

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
    this.getHolder().removeCallback(this);
    mCamera.stopPreview();

    mCamera.release();
    mCamera = null;
  Log.e("surfaceDestroyed", "surfaceDestroyed");

}

Then reinitialize the camera in function onResume.

0
source
public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Because the CameraDevice object is not a shared resource, it very
    // important to release it when the activity is paused.
    mCamera.setPreviewCallback(null);
    mCamera.stopPreview();
    mCamera.release();
    mCamera = null;
}
0
source

Source: https://habr.com/ru/post/1785498/


All Articles