Skip to content

Commit

Permalink
add lockAndroidOrientation option
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewHaisting committed Mar 26, 2021
1 parent 8ec4752 commit fc18d32
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Starts the camera preview instance.
| storeToFile | boolean | (optional) Capture images to a file and return back the file path instead of returning base64 encoded data, default false. |
| disableExifHeaderStripping | boolean | (optional) Disable automatic rotation of the image, and let the browser deal with it, default true (applicable to the android and ios platforms only) |
| disableAudio | boolean | (optional) Disables audio stream to prevent permission requests, default false. (applicable to web only) |
| lockAndroidOrientation | boolean | (optional) Locks device orientation when camer is showing, default false. (applicable to Android only) |

<!-- <strong>Options:</strong>
All options stated are optional and will default to values here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Point;
import android.hardware.Camera;
Expand All @@ -19,11 +20,9 @@
import com.getcapacitor.PluginMethod;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.io.File;
import java.util.List;

@NativePlugin(
permissions = {
Expand All @@ -40,6 +39,9 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
private static String VIDEO_FILE_EXTENSION = ".mp4";
static final int REQUEST_CAMERA_PERMISSION = 1234;

// keep track of previously specified orientation to support locking orientation:
private int previousOrientationRequest = -1;

private CameraActivity fragment;
private int containerViewId = 20;

Expand Down Expand Up @@ -100,6 +102,9 @@ public void stop(final PluginCall call) {
public void run() {
FrameLayout containerView = getBridge().getActivity().findViewById(containerViewId);

// allow orientation changes after closing camera:
getBridge().getActivity().setRequestedOrientation(previousOrientationRequest);

if (containerView != null) {
((ViewGroup)getBridge().getWebView().getParent()).removeView(containerView);
getBridge().getWebView().setBackgroundColor(Color.WHITE);
Expand Down Expand Up @@ -213,6 +218,8 @@ private void startCamera(final PluginCall call) {
final Boolean toBack = call.getBoolean("toBack", false);
final Boolean storeToFile = call.getBoolean("storeToFile", false);
final Boolean disableExifHeaderStripping = call.getBoolean("disableExifHeaderStripping", true);
final Boolean lockOrientation = call.getBoolean("lockAndroidOrientation", false);
previousOrientationRequest = getBridge().getActivity().getRequestedOrientation();

fragment = new CameraActivity();
fragment.setEventListener(this);
Expand All @@ -228,6 +235,11 @@ private void startCamera(final PluginCall call) {
@Override
public void run() {
DisplayMetrics metrics = getBridge().getActivity().getResources().getDisplayMetrics();
// lock orientation if specified in options:
if (lockOrientation) {
getBridge().getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}

// offset
int computedX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, x, metrics);
int computedY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, y, metrics);
Expand Down

0 comments on commit fc18d32

Please sign in to comment.