Camera Orientation in android 2.3.6

I ran into the problem of displaying a camera preview on an Android 2.3.6 device. I used this code to change the orientation of the camera, but for 2.3.6 it does not work, and for other versions it works correctly.

In device 2.2.1

In device 2.3.6

The first image in the device 2.2.1, the second image in the device 2.3.6. I want the camera preview to be the same as in 2.2.1 for device 2.3.6. Below is my code

if (Integer.parseInt(Build.VERSION.SDK) >= 8) { mCamera.setDisplayOrientation(90); } else { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { p.set("orientation", "portrait"); p.set("rotation", 90); } if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { p.set("orientation", "landscape"); p.set("rotation", 90); } } 

And I pointed out the portrait orientation of the screen in the manifest. Give me back the solution. Thanks in advance.

+4
source share
1 answer

Please see below code:

 private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) { int rotation = getWindowManager().getDefaultDisplay() .getRotation(); int degrees = 0; int result; if(Build.VERSION.SDK_INT>10){ android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); 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; } if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); }else{ if(cameraId==CAMERA_FACING_BACK){ camera.setDisplayOrientation(90); }else{ camera.setDisplayOrientation(270); } } } 

You can get the camera id using

 @TargetApi(Build.VERSION_CODES.GINGERBREAD) private Camera openFrontFaceCamera() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { Camera.getCameraInfo( camIdx, cameraInfo ); if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) { try { mCameraId=camIdx; cam = Camera.open( camIdx ); } catch (RuntimeException e) { Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } @TargetApi(Build.VERSION_CODES.GINGERBREAD) private Camera openRearFaceCamera() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { Camera.getCameraInfo( camIdx, cameraInfo ); if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK ) { try { mCameraId=camIdx; cam = Camera.open( camIdx ); } catch (RuntimeException e) { Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } 
0
source

All Articles