-->

lock your camera view to landscape mode in android

2020-07-16 02:42发布

问题:

I want to lock my camera view to "Landscape" mode.When I click on the simple button in my app, that time device's camera will open and and that camera should be locked to "Landscape mode". Can anyone know the solution of this problem?

I am using this code inside "CaptureImageActivity.java" activity. So after execution of this activity my system's camera will open.

btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i=new Intent(android.provider.MediaStore. ACTION_IMAGE_CAPTURE);
            i.putExtra(android.provider.MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


            startActivityForResult(i,1000);

        }
    });

And I want to lock that camera view to "LandScape Mode". And as per solution

<activity android:name=".CaptureImageActivity"
    android:screenOrientation="landscape"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Only "CaptureImageActivity" activity is locked to "LandScape Mode".

回答1:

Add this into your activity inside manifest file

android:screenOrientation="portrait"


回答2:

see this link to lock your camera orientation,use setDisplayOrientation (int degrees)

documentation can be found on following link : http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29

for more details see this answer :

How to set Android camera orientation properly?



回答3:

By using setCameraDisplayOrientation=
public static 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;
     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);
 }