React-Native-Camera亚马逊Kindle 7(5.1.1)后置摄像头捕获稳定的绿色

时间:2018-06-10 10:14:23

标签: react-native react-native-android react-native-camera

出于某种原因,我用于测试运行最新更新(Android 5.1.1)的2个Amazon Kindles在使用React-Native-Camera捕获时只产生稳定的绿色。

我还在我的Xiaomi Mi6Mi5以及Asus Zen 8" Tablet上进行了测试,一切正常,但是Kindle产生了这个令人满意的结果......什么&#39非常奇怪的是取景器很好,它看起来好像是拍照但不是。前置摄像头也很好。

使用react-native-camera: ^1.1.4

Capture.js

import React, { Component } from 'react';
import { StyleSheet, Text, View, ActivityIndicator } from 'react-native';
import { Avatar } from 'react-native-elements';
import { RNCamera } from 'react-native-camera';
import { inject, observer } from 'mobx-react/native';
import ImagePicker from 'react-native-image-crop-picker';

let Type = null;

const typeArr = [
  { Name: 'Front', Type: RNCamera.Constants.Type.front },
  { Name: 'Back', Type: RNCamera.Constants.Type.back },
  { Name: null, Type: RNCamera.Constants.Type.back },
];

const styles = StyleSheet.create({
  entryTitle: {
    fontSize: 22,
    fontWeight: '700',
  },
  container: {
    flex: 1,
    flexDirection: 'column',
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  loading: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'rgba(255, 255, 255, 0.8)',
  },
});

@inject('store')
@observer
export default class Capture extends Component {
  constructor(props) {
    super(props);

    this.state = { Type: RNCamera.Constants.Type.back, CaptureInProgress: false };
    Type =
      this.props.navigation.state.params.Type == null
        ? null
        : this.props.navigation.state.params.Type;
  }

  state = {
    Type: typeArr.find(element => element.Name === Type).Type,
  };

  barcodeScanned(response) {
    this.props.store.CaptureStore.captureData = response.data;
    this.props.navigation.state.params.AfterCapture();
    this.props.navigation.goBack();
  }

  takePicture = async function () {
    if (this.camera) {
      this.setState({ CaptureInProgress: true });

      const options = { quality: 0.5, base64: true, fixOrientation: true };
      const data = await this.camera.takePictureAsync(options);
      this.props.store.CaptureStore.captureData = data.base64;
      this.props.navigation.state.params.AfterCapture();
      this.setState({ CaptureInProgress: false });

      this.props.navigation.goBack();
    }
  };

  openGallery() {
    ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true,
      includeBase64: true,
    }).then((image) => {
      this.props.store.CaptureStore.captureData = image.data;
      this.props.navigation.state.params.AfterCapture();
      this.props.navigation.goBack();
    });
  }

  switchCamera() {
    if (this.state.Type === RNCamera.Constants.Type.back) {
      this.setState({ Type: RNCamera.Constants.Type.front });
    } else {
      this.setState({ Type: RNCamera.Constants.Type.back });
    }
  }

  renderTakePhotoButton() {
    if (this.props.navigation.state.params.Mode === 'photo') {
      return (
        <View
          style={{
            flex: 1,
            flexDirection: 'row',
            justifyContent: 'center',
            alignItems: 'center',
          }}
        >
          <Avatar
            medium
            rounded
            icon={{ name: 'refresh', color: 'grey', type: 'font-awesome' }}
            onPress={() => this.switchCamera()}
            activeOpacity={1}
          />
          <Avatar
            large
            rounded
            icon={{ name: 'camera', color: 'grey' }}
            onPress={() => this.takePicture()}
            activeOpacity={1}
          />
          <Avatar
            medium
            rounded
            icon={{ name: 'folder-open-o', color: 'grey', type: 'font-awesome' }}
            onPress={() => this.openGallery()}
            activeOpacity={1}
          />
        </View>
      );
    }
    return null;
  }

  render() {
    return (
      <View style={styles.container}>
        <View
          style={{
            height: '10%',
            padding: 10,
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          <Text style={styles.entryTitle}>{this.props.navigation.state.params.Title}</Text>
        </View>

        <View
          style={{
            height: this.props.navigation.state.params.Mode === 'photo' ? '75%' : '90%',
            flexDirection: 'column',
          }}
        >
          <RNCamera
            ref={(ref) => {
              this.camera = ref;
            }}
            style={styles.preview}
            barCodeTypes={
              this.props.navigation.state.params.Mode === 'qr'
                ? [RNCamera.Constants.BarCodeType.qr]
                : []
            }
            type={this.state.Type}
            // flashMode={RNCamera.Constants.FlashMode.on}
            permissionDialogTitle="Permission to use camera"
            permissionDialogMessage="We need your permission to use your camera phone"
            onBarCodeRead={response => this.barcodeScanned(response)}
          />
        </View>

        {this.renderTakePhotoButton()}

        {this.state.CaptureInProgress && (
          <View style={styles.loading}>
            <ActivityIndicator size="large" color="#0000ff" />
          </View>
        )}
      </View>
    );
  }
}

具体来说,这一切都发生在&#39; takePicture&#39;中,afterCapture只处理暂时存在于CaptureStore中的base64的消耗......

1 个答案:

答案 0 :(得分:0)

已在React-Native-Camera 1.1.5中修复