mirror of
https://github.com/status-im/react-native-camera.git
synced 2026-08-31 13:41:08 +00:00
@@ -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
|
||||
@@ -70,6 +72,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
|
||||
return Collections.unmodifiableMap(new HashMap<String, Object>() {
|
||||
{
|
||||
put("Aspect", getAspectConstants());
|
||||
put("BarCodeType", getBarCodeConstants());
|
||||
put("Type", getTypeConstants());
|
||||
put("CaptureQuality", getCaptureQualityConstants());
|
||||
put("CaptureMode", getCaptureModeConstants());
|
||||
@@ -89,6 +92,14 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, Object> getBarCodeConstants() {
|
||||
return Collections.unmodifiableMap(new HashMap<String, Object>() {
|
||||
{
|
||||
// @TODO add barcode types
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, Object> getTypeConstants() {
|
||||
return Collections.unmodifiableMap(new HashMap<String, Object>() {
|
||||
{
|
||||
@@ -165,27 +176,38 @@ 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
|
||||
public void onPictureTaken(byte[] data, Camera camera) {
|
||||
camera.stopPreview();
|
||||
camera.startPreview();
|
||||
WritableMap response = new WritableNativeMap();
|
||||
switch (options.getInt("target")) {
|
||||
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
|
||||
String encoded = Base64.encodeToString(data, Base64.DEFAULT);
|
||||
promise.resolve(encoded);
|
||||
response.putString("data", encoded);
|
||||
promise.resolve(response);
|
||||
break;
|
||||
case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL:
|
||||
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
|
||||
@@ -194,7 +216,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
|
||||
_reactContext.getContentResolver(),
|
||||
bitmap, options.getString("title"),
|
||||
options.getString("description"));
|
||||
promise.resolve(url);
|
||||
response.putString("path", url);
|
||||
promise.resolve(response);
|
||||
break;
|
||||
case RCT_CAMERA_CAPTURE_TARGET_DISK:
|
||||
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
||||
@@ -212,7 +235,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
|
||||
} catch (IOException e) {
|
||||
promise.reject("Error accessing file: " + e.getMessage());
|
||||
}
|
||||
promise.resolve(Uri.fromFile(pictureFile).toString());
|
||||
response.putString("path", Uri.fromFile(pictureFile).toString());
|
||||
promise.resolve(response);
|
||||
break;
|
||||
case RCT_CAMERA_CAPTURE_TARGET_TEMP:
|
||||
File tempFile = getTempMediaFile(MEDIA_TYPE_IMAGE);
|
||||
@@ -231,7 +255,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule {
|
||||
} catch (IOException e) {
|
||||
promise.reject("Error accessing file: " + e.getMessage());
|
||||
}
|
||||
promise.resolve(Uri.fromFile(tempFile).toString());
|
||||
response.putString("path", Uri.fromFile(tempFile).toString());
|
||||
promise.resolve(response);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user