fix snapshot orientation when device orientation is locked (#232)

* use device orientation on android only with auto orientation

* guard against missing playSoundOnCapture and quality props in options

* add video orientation support

* use device orientation on ios only with auto orientation
This commit is contained in:
Radu-Marius Popovici
2016-05-10 11:05:03 -07:00
committed by Zack Story
parent f9137824a0
commit 0c3dba1ff6
5 changed files with 83 additions and 38 deletions
@@ -132,6 +132,10 @@ public class RCTCamera {
return result;
}
public int getOrientation() {
return _orientation;
}
public void setOrientation(int orientation) {
if (_orientation == orientation) {
return;
@@ -13,6 +13,7 @@ import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.Surface;
import com.facebook.react.bridge.*;
import javax.annotation.Nullable;
@@ -36,11 +37,11 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
public static final int RCT_CAMERA_CAPTURE_TARGET_DISK = 1;
public static final int RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL = 2;
public static final int RCT_CAMERA_CAPTURE_TARGET_TEMP = 3;
public static final int RCT_CAMERA_ORIENTATION_AUTO = 0;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_LEFT = 1;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_RIGHT = 2;
public static final int RCT_CAMERA_ORIENTATION_PORTRAIT = 3;
public static final int RCT_CAMERA_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 4;
public static final int RCT_CAMERA_ORIENTATION_AUTO = Integer.MAX_VALUE;
public static final int RCT_CAMERA_ORIENTATION_PORTRAIT = Surface.ROTATION_0;
public static final int RCT_CAMERA_ORIENTATION_PORTRAIT_UPSIDE_DOWN = Surface.ROTATION_180;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_LEFT = Surface.ROTATION_90;
public static final int RCT_CAMERA_ORIENTATION_LANDSCAPE_RIGHT = Surface.ROTATION_270;
public static final int RCT_CAMERA_TYPE_FRONT = 1;
public static final int RCT_CAMERA_TYPE_BACK = 2;
public static final int RCT_CAMERA_FLASH_MODE_OFF = 0;
@@ -176,16 +177,21 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
@ReactMethod
public void capture(final ReadableMap options, final Promise promise) {
_sensorOrientationChecker.onResume();
_sensorOrientationChecker.registerOrientationListener(new RCTSensorOrientationListener() {
@Override
public void orientationEvent() {
int deviceOrientation = _sensorOrientationChecker.getOrientation();
_sensorOrientationChecker.unregisterOrientationListener();
_sensorOrientationChecker.onPause();
captureWithOrientation(options, promise, deviceOrientation);
}
});
int orientation = options.hasKey("orientation") ? options.getInt("orientation") : RCTCamera.getInstance().getOrientation();
if (orientation == RCT_CAMERA_ORIENTATION_AUTO) {
_sensorOrientationChecker.onResume();
_sensorOrientationChecker.registerOrientationListener(new RCTSensorOrientationListener() {
@Override
public void orientationEvent() {
int deviceOrientation = _sensorOrientationChecker.getOrientation();
_sensorOrientationChecker.unregisterOrientationListener();
_sensorOrientationChecker.onPause();
captureWithOrientation(options, promise, deviceOrientation);
}
});
} else {
captureWithOrientation(options, promise, orientation);
}
}
public void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) {
@@ -195,8 +201,16 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
return;
}
if (options.hasKey("playSoundOnCapture") && options.getBoolean("playSoundOnCapture")) {
MediaActionSound sound = new MediaActionSound();
sound.play(MediaActionSound.SHUTTER_CLICK);
}
if (options.hasKey("quality")) {
RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
}
RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation);
RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
@@ -9,6 +9,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Surface;
import com.facebook.react.bridge.ReactApplicationContext;
@@ -51,13 +52,13 @@ public class RCTSensorOrientationChecker {
float y = event.values[1];
if (x<5 && x>-5 && y > 5)
mOrientation = 0;
mOrientation = Surface.ROTATION_0; // portrait
else if (x<-5 && y<5 && y>-5)
mOrientation = 3;
mOrientation = Surface.ROTATION_270; // right
else if (x<5 && x>-5 && y<-5)
mOrientation = 2;
mOrientation = Surface.ROTATION_180; // upside down
else if (x>5 && y<5 && y>-5)
mOrientation = 1;
mOrientation = Surface.ROTATION_90; // left
if (mListener != null) {
mListener.orientationEvent();