Fix startPreview and stopPreview on Android

This commit is contained in:
Nicolas Morin 2018-01-23 20:53:28 +01:00
parent 3646f5f148
commit 286071c035
5 changed files with 64 additions and 39 deletions

View File

@ -696,28 +696,6 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
}
}
@ReactMethod
public void stopPreview(ReadableMap options) {
RCTCamera instance = RCTCamera.getInstance();
if (instance == null) return;
Camera camera = instance.acquireCameraInstance(options.getInt("type"));
if (camera == null) return;
camera.stopPreview();
}
@ReactMethod
public void startPreview(ReadableMap options) {
RCTCamera instance = RCTCamera.getInstance();
if (instance == null) return;
Camera camera = instance.acquireCameraInstance(options.getInt("type"));
if (camera == null) return;
camera.startPreview();
}
@ReactMethod
public void hasFlash(ReadableMap options, final Promise promise) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));

View File

@ -6,7 +6,6 @@ package com.lwansbrough.RCTCamera;
import android.content.Context;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.ViewGroup;
import android.view.WindowManager;
@ -144,6 +143,16 @@ public class RCTCameraView extends ViewGroup {
}
}
public void stopPreview() {
if (_viewFinder == null) return;
_viewFinder.stopPreview();
}
public void startPreview() {
if (_viewFinder == null) return;
_viewFinder.startPreview();
}
private boolean setActualDeviceOrientation(Context context) {
int actualDeviceOrientation = getDeviceOrientation(context);
if (_actualDeviceOrientation != actualDeviceOrientation) {

View File

@ -135,13 +135,13 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
RCTCamera.getInstance().setZoom(_cameraType, zoom);
}
private void startPreview() {
public void startPreview() {
if (_surfaceTexture != null) {
startCamera();
}
}
private void stopPreview() {
public void stopPreview() {
if (_camera != null) {
stopCamera();
}

View File

@ -2,16 +2,22 @@ package com.lwansbrough.RCTCamera;
import android.support.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.*;
import com.facebook.react.uimanager.annotations.ReactProp;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class RCTCameraViewManager extends ViewGroupManager<RCTCameraView> {
private static final String REACT_CLASS = "RCTCamera";
public static final int COMMAND_STOP_PREVIEW = 1;
public static final int COMMAND_START_PREVIEW = 2;
@Override
public String getName() {
return REACT_CLASS;
@ -22,6 +28,33 @@ public class RCTCameraViewManager extends ViewGroupManager<RCTCameraView> {
return new RCTCameraView(context);
}
@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of(
"stopPreview",
COMMAND_STOP_PREVIEW,
"startPreview",
COMMAND_START_PREVIEW);
}
@Override
public void receiveCommand(RCTCameraView view, int commandType, @Nullable ReadableArray args) {
Assertions.assertNotNull(view);
switch (commandType) {
case COMMAND_STOP_PREVIEW: {
view.stopPreview();
return;
}
case COMMAND_START_PREVIEW: {
view.startPreview();
return;
}
default:
throw new IllegalArgumentException(
String.format("Unsupported command %d received by %s.", commandType, getClass().getSimpleName()));
}
}
@ReactProp(name = "aspect")
public void setAspect(RCTCameraView view, int aspect) {
view.setAspect(aspect);

View File

@ -6,12 +6,14 @@ import {
NativeModules,
Platform,
StyleSheet,
findNodeHandle,
requireNativeComponent,
ViewPropTypes,
PermissionsAndroid,
ActivityIndicator,
View,
Text,
UIManager,
} from 'react-native';
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
@ -300,10 +302,10 @@ export default class Camera extends Component {
startPreview() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
CameraManager.startPreview({
type: props.type,
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.RCTCamera.Commands.startPreview,
);
} else {
CameraManager.startPreview();
}
@ -311,10 +313,10 @@ export default class Camera extends Component {
stopPreview() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
CameraManager.stopPreview({
type: props.type,
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.RCTCamera.Commands.stopPreview,
);
} else {
CameraManager.stopPreview();
}
@ -342,16 +344,19 @@ export default class Camera extends Component {
return CameraManager.hasFlash();
}
setZoom(zoom) {
setZoom(zoom) {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
return CameraManager.setZoom({
type: props.type,
}, zoom);
return CameraManager.setZoom(
{
type: props.type,
},
zoom,
);
}
return CameraManager.setZoom(zoom);
}
return CameraManager.setZoom(zoom);
}
}
export const constants = Camera.constants;