Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating a Camera SurfaceView to portrait

I have looked up a few posts on changing the orientation of the camera with a surface view, but I have taken my code from the examples at:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

The function that provides the dimensions looks like this...

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

My problem is that when I change the orientation of the device, the preview picture stays landscape. I tried setting the orientation of the camera but this resulted in very strange results. Does anyone know what I need to change to have this rotate properly?

like image 482
frak Avatar asked Sep 17 '11 14:09

frak


1 Answers

Try this out, but I tried in Samsung Galaxy Tab

public void surfaceCreated(SurfaceHolder holder)
{
   // The Surface has been created, acquire the camera and tell it where to draw.
   mCamera = Camera.open();

   Parameters params = mCamera.getParameters();

   if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
   {
    params.set("orientation", "portrait");
    mCamera.setDisplayOrientation(90);
   }

    try
      {
      mCamera.setPreviewDisplay(holder);
      }
      catch (IOException exception)
      {
        mCamera.release();
        mCamera = null;
      }

}
like image 122
VIGNESH.SRINIVASAGAN Avatar answered Sep 20 '22 12:09

VIGNESH.SRINIVASAGAN