如何在react-native 摄像头上添加遮罩?

我正在使用React Native camera为React Native QRCode scanner应用程序构建UI.

相机顶部的叠加遮罩应为浅灰色,但中间部分必须保持透明(透明).

但当我改变外面具的背景色时,它似乎也会影响中心部分.我的意思是,当然,这是在它的 children 观背后.

下面的代码是快照的简化版本.

<Camera
  ref={cam => {
    this.camera = cam;
  }}
  onBarCodeRead={this._onBarCodeRead}
  style={styles.cameraView}
  aspect={Camera.constants.Aspect.fill}
  playSoundOnCapture
>
  <View
    style={{
      position: 'absolute',
      top: 0,
      left: 0,
      width: '100%',
      height: '100%',
      backgroundColor: 'rgba(0.2, 0.2, 0.2, 0.2)',
      alignItems: 'center',
      justifyContent: 'space-around',
    }}
  >
    <View
      style={{
        width: 300,
        height: 300,
        backgroundColor: 'transparent',
        borderColor: 'white',
        borderWidth: 1,
      }}
    />
  </View>
</Camera>

The basic idea is here

知道怎么做吗?

推荐答案

我终于弄明白了.我们的 idea 是像汉堡一样创建3行,然后在运行时计算高度和宽度.

中间一行有3个视图组件,中间一行有透明背景和白色边框.

(值300来自中心视图(透明区域)的大小,我将其除以10来计算FlexBox的较小比率)

Barcode mask demo

export default class CameraScreen extends React.Component<any, any> {
  render() {
    const { height, width } = Dimensions.get('window');
    const maskRowHeight = Math.round((AppStore.height - 300) / 20);
    const maskColWidth = (width - 300) / 2;

    return (
      <View style={styles.container}>
        <Camera
          ref={cam => {
            this.camera = cam;
          }}
          onBarCodeRead={this._onBarCodeRead}
          style={styles.cameraView}
          aspect={Camera.constants.Aspect.fill}
          playSoundOnCapture
        >
          <View style={styles.maskOutter}>
            <View style={[{ flex: maskRowHeight  }, styles.maskRow, styles.maskFrame]} />
             <View style={[{ flex: 30 }, styles.maskCenter]}>
             <View style={[{ width: maskColWidth }, styles.maskFrame]} />
             <View style={styles.maskInner} />
            <View style={[{ width: maskColWidth }, styles.maskFrame]} />
          </View>
        <View style={[{ flex: maskRowHeight }, styles.maskRow, styles.maskFrame]} />
      </View>
        </Camera>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  cameraView: {
    flex: 1,
    justifyContent: 'flex-start',
  },
  maskOutter: {
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    alignItems: 'center',
    justifyContent: 'space-around',
  },
  maskInner: {
    width: 300,
    backgroundColor: 'transparent',
    borderColor: 'white',
    borderWidth: 1,
  },
  maskFrame: {
    backgroundColor: 'rgba(1,1,1,0.6)',
  },
  maskRow: {
    width: '100%',
  },
  maskCenter: { flexDirection: 'row' },
});

Update:

React-native相关问答推荐

ITMS—91053:缺少API声明—ReactNative

@react-native-community/cli-platform-ios/native_modules' 来自哪里?

React native + react-query + onError 不捕获并处理错误?

在 React Native 中打开后日期 Select 器不关闭

我们应该在 react-native 移动应用程序中使用哪个图标库

使用 React Native 下载数据文件以供离线使用

PanResponder 在第二次拖动时将 Animated.View 捕捉回原始位置

开发 React Native Apps 时如何在 Android 模拟器窗口中重新加载?

如何获取 React Native 应用程序的崩溃日志(log)?

你如何使 react-native react-navigation 标签栏透明

自从升级到 Xcode 10.2 我不能再通过 cli 运行 react-native run-ios

submit提交后如何让 React Native TextInput 保持焦点?

如何判断 React Native 中的 AsyncStorage 中是否存在密钥? getItem() 总是返回一个promise对象

如何在 array.map 中的每个元素之后添加逗号,除了 React JSX 中的最后一个元素

React Native 使视图 Hug键盘顶部

如何在 React Native 中获取电话号码?

如何在react-native中显示toast消息

在 React Native ScrollView 中检测滚动结束,对齐页面

居中图像 React Native 加载屏幕

如何在本机代码(Java、ObjectiveC/Swift)中读取 AsyncStorage