Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lockAndroidOrientation option #117

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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