merge device orientation branch

This commit is contained in:
Zack Story
2016-04-23 13:02:29 -07:00
7 changed files with 270 additions and 6 deletions
@@ -224,6 +224,32 @@ public class RCTCamera {
}
}
public void adjustCameraRotationToDeviceOrientation(int type, int deviceOrientation)
{
Camera camera = _cameras.get(type);
if (null == camera) {
return;
}
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
int rotation;
int orientation = cameraInfo.info.orientation;
if (cameraInfo.info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
rotation = (orientation + deviceOrientation * 90) % 360;
} else {
rotation = (orientation - deviceOrientation * 90 + 360) % 360;
}
cameraInfo.rotation = rotation;
Camera.Parameters parameters = camera.getParameters();
parameters.setRotation(cameraInfo.rotation);
try {
camera.setParameters(parameters);
} catch (Exception e) {
e.printStackTrace();
}
}
private void adjustPreviewLayout(int type) {
Camera camera = _cameras.get(type);
if (null == camera) {
@@ -53,10 +53,12 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
public static final int MEDIA_TYPE_VIDEO = 2;
private final ReactApplicationContext _reactContext;
private RCTSensorOrientationChecker _sensorOrientationChecker;
public RCTCameraModule(ReactApplicationContext reactContext) {
super(reactContext);
_reactContext = reactContext;
_sensorOrientationChecker = new RCTSensorOrientationChecker(_reactContext);
}
@Override
@@ -173,17 +175,26 @@ 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);
}
});
}
public void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
if (null == camera) {
promise.reject("No camera found.");
return;
}
if (options.getBoolean("playSoundOnCapture")) {
MediaActionSound sound = new MediaActionSound();
sound.play(MediaActionSound.SHUTTER_CLICK);
}
RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation);
RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
@@ -0,0 +1,84 @@
/**
* Created by rpopovici on 23/03/16.
*/
package com.lwansbrough.RCTCamera;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import com.facebook.react.bridge.ReactApplicationContext;
interface RCTSensorOrientationListener {
void orientationEvent();
}
public class RCTSensorOrientationChecker {
int mOrientation = 0;
private SensorEventListener mSensorEventListener;
private SensorManager mSensorManager;
private RCTSensorOrientationListener mListener = null;
public RCTSensorOrientationChecker( ReactApplicationContext reactContext) {
mSensorEventListener = new Listener();
mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE);
}
/**
* Call on activity onResume()
*/
public void onResume() {
mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
/**
* Call on activity onPause()
*/
public void onPause() {
mSensorManager.unregisterListener(mSensorEventListener);
}
private class Listener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (x<5 && x>-5 && y > 5)
mOrientation = 0;
else if (x<-5 && y<5 && y>-5)
mOrientation = 3;
else if (x<5 && x>-5 && y<-5)
mOrientation = 2;
else if (x>5 && y<5 && y>-5)
mOrientation = 1;
if (mListener != null) {
mListener.orientationEvent();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
public int getOrientation() {
return mOrientation;
}
public void registerOrientationListener(RCTSensorOrientationListener listener) {
this.mListener = listener;
}
public void unregisterOrientationListener() {
mListener = null;
}
}