Heelara
Heelara

Reputation: 979

Camera Preview takes some time to redraw on Orientation Change

I am working on a custom camera Android app. It works fine with the preview screen fits nicely with the screen size on both portrait and landscape modes. However, when changing phone orientation, there is a noticeable delay in preview redraw. Searching through the forums I came across others seeing similar issues when dealing with phone orientation change. Following some suggestion to avoid activity being destroyed and recreated, and camera being released and opened during such a change, I have added the following line to the AndroidManifest.xml:

        android:configChanges="orientation|screenSize">

From some debugging and testing, I can confirm that the activity is no longer being destroyed/created on orientation change. However, even with this addition, there is a noticeable delay when the preview redraws going from portrait to landscape, vice versa. Here is the code for onConfigurationChanged method within the Activity class:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mPreview.setCameraDisplayOrientation(this, 1, mCamera);
}

And within the SurfaceView class:

public void setCameraDisplayOrientation(Activity activity,
        int cameraId, android.hardware.Camera camera) {

    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result = (info.orientation + degrees) % 360;
    result = (360 - result) % 360;  // compensate the mirror
    camera.setDisplayOrientation(result);
}

And within surfaceChanged, I have:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    initialisePreview(w, h); // gets best preview size and sets camera parameters
    mCamera.startPreview();
    }

There is nothing new about the above setCameraDisplayOrientation method and was adapted from Android Developer website. I am not sure what else could be contributing to such a delay with redraw. If anyone has any thoughts, please advise. Thank you.

Upvotes: 1

Views: 1202

Answers (1)

Heelara
Heelara

Reputation: 979

Instead of handling screen orientation change as described above, I resorted to making Activity ignore orientation changes determined by orientation sensor during onCreate() using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

With this change, the preview continues on seamlessly when the phone orientation changes. This sppears counter intuitive, but it resolved my issue. Hope it helps someone.

Upvotes: 1

Related Questions