Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the Android-camera to portrait in surfaceview?

I am coding for an android tablet and i want my app to use the portrait view of the surfaceView camera preview. It is landscape by default, and I tried the following code to rotate to portrait view:

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 we aren't landscape (the default), tell the camera we want portrait mode
  if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
    params.set("orientation", "portrait"); // "landscape"
    // And Rotate the final picture if possible
    // This works on 2.0 and higher only
    //params.setRotation(90);
    // Use reflection to see if it exists and to call it so you can support older versions
      try {
        Method rotateSet = Camera.Parameters.class.getMethod("setRotation", new Class[] { Integer.TYPE } );
        Object arguments[] = new Object[] { new Integer(270) };
        rotateSet.invoke(params, arguments);
      } catch (NoSuchMethodException nsme) {
        // Older Device
        Log.v("CameraView","No Set Rotation");
      } catch (IllegalArgumentException e) {
        Log.v("CameraView","Exception IllegalArgument");
      } catch (IllegalAccessException e) {
        Log.v("CameraView","Illegal Access Exception");
      } catch (InvocationTargetException e) {
        Log.v("CameraView","Invocation Target Exception");
      }
  }
  mCamera.setParameters(params);
  try{
    mCamera.setPreviewDisplay(holder);
  } catch (IOException exception) {
    mCamera.release();
    mCamera = null;
  }
}

But it doesn't work. Could anybody fix it please?

like image 227
VIGNESH.SRINIVASAGAN Avatar asked Aug 23 '12 10:08

VIGNESH.SRINIVASAGAN


People also ask

How do I fix the camera orientation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

What is SurfaceView?

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.


1 Answers

You will probably want to use the setDisplayOrientation function as follows:

public void surfaceCreated(SurfaceHolder holder) {
    if (Build.VERSION.SDK_INT >= 8) mCamera.setDisplayOrientation(90);
}

Using the camera params.set("orientation"... stuff is not consistent across devices and is really pre-SDK 8 language.

like image 115
Daniel Smith Avatar answered Oct 02 '22 18:10

Daniel Smith