0

I'm developing a small camera capture program in android.

I didn't use the Intent to open the camera and show it's images. I'm using the camera directly:

if (myPreviewRunning) {
            myCamera.stopPreview();
            myPreviewRunning = false;
        }

        Camera.Parameters parameters = myCamera.getParameters();
        display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        display = getWindowManager().getDefaultDisplay();

        Camera.Size optimalSize = getOptimalSize(parameters.getSupportedPreviewSizes(), width, height);

        parameters.setPreviewSize(optimalSize.width, optimalSize.height);
        if (display.getRotation() == Surface.ROTATION_0 ||display.getRotation() == Surface.ROTATION_270)
        {
            parameters.setPreviewSize(optimalSize.height, optimalSize.width);

            ViewGroup.LayoutParams layoutParams = mySurfaceView.getLayoutParams();
            layoutParams.width = optimalSize.height;
            layoutParams.height = optimalSize.width;

            mySurfaceView.setLayoutParams(layoutParams);


            myCamera.setDisplayOrientation(90);
        }
        else
        {
            myCamera.setDisplayOrientation(180);
            mySurfaceView.getLayoutParams().width = optimalSize.width;
            mySurfaceView.getLayoutParams().height = optimalSize.height;
        }

        myCamera.setParameters(parameters);
        myCamera.setPreviewDisplay(holder);
        myCamera.startPreview();

Until now, with this code I'm able to rotate the camera to a 'natural' behaviour, so when I rotate the cell phone to landscape, it rotates the image and you see it in landscape. But due to some reasons, I need the preview image to remain in portrait mode, even if I put landscape.

How can I force the camera preview to remain in portrait mode?

EDIT: AndroidManifest.xml

<activity
            android:name="CameraViewer"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
    </activity>

I want to be able to do this:

enter image description here

Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • have you added screenOrientation property to your activity declaration in manifest? – Gooziec Dec 20 '13 at 10:03
  • have you looked at http://stackoverflow.com/questions/17083366/how-to-set-captured-images-in-portrait-mode ? – Gooziec Dec 20 '13 at 10:07
  • But that is for the images I want to store. I want the preview to remain in portrait. – Sonhja Dec 20 '13 at 10:12
  • Your second illustration doesn't make sense:you can rotate and crop the preview as you like, but the picture you start with is always controlled by the hardware wiring, usually oblong as the device itself. – Alex Cohn Dec 20 '13 at 11:12
  • Why it has no sense? I want to keep the preview always in portrait mode. That is what I was trying to show. – Sonhja Dec 20 '13 at 11:31

0 Answers0