React Native在应用中使用摄像头作为后台服务捕捉视频。

问题描述 投票:0回答:1

我想知道,在使用应用程序时,是否有办法用手机的前置摄像头拍摄视频记录。相机不应该在应用程序的UI中打开。

框架 - React Native

react-native android-camera
1个回答
1
投票

你可以渲染摄像机,但隐藏 <View><RNCamera>.

我做了这个代码来实现它。

import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { RNCamera } from 'react-native-camera';

export default class App extends Component {
  takePicture = async () => {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      const data = await this.camera.takePictureAsync(options);

      console.log(data.uri); // log picture encoded in base64 data format.
    }
  };

  componentDidMount() {
    setTimeout(() => this.takePicture(), 500); // delay while camera is loading, then take picture.
  }

  render() {
    return (
      <View style={styles.container}>
        <RNCamera
          style={styles.camera}
          ref={ref => { this.camera = ref; }}
          type={RNCamera.Constants.Type.front}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    height: 1,
    width: 1,
    opacity: 0,
  },
  camera: {
    height: 1,
    width: 1,
    opacity: 0,
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.