【问题标题】:React native camera with a transparent view for barcode scanner mask使用条码扫描仪掩码的透明视图反应本机相机
【发布时间】:2018-07-08 16:26:33
【问题描述】:

如何在 react-native-camera 上添加遮罩?

我正在使用 react-native-camera 为 React Native QRCode 扫描仪应用程序构建 UI。

相机顶部的叠加蒙版应为浅灰色,但中间部分必须保持透明(透视)。

但是当我改变外罩的背景颜色时,它似乎也影响了中心部分。我的意思是,当然,它在它的子视图后面。

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

<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>

知道如何完成这项工作吗?

【问题讨论】:

    标签: css react-native react-native-camera


    【解决方案1】:

    您需要使用一些掩码的原生视图(iOs、Android)并将您的内容包装在其中

    或者你可以使用我们的小库来处理这个https://github.com/ibitcy/react-native-hole-view

    【讨论】:

      【解决方案2】:

      你可以使用这个:
      react-native-barcode-mask

      【讨论】:

        【解决方案3】:

        我终于弄明白了。这个想法是像汉堡一样创建 3 行,然后在运行时计算高度和宽度。

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

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

        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' },
        });

        更新: 不同品牌手机之间的高度比例变化取决于它使用物理/软按钮。我用flex代替了固定高度。

        【讨论】:

        • 渲染函数中的高度和宽度仅用于演示目的。这些应该在consturctor 内完成
        • @random1234 是的,方形只是用户的参考点。我还尝试了另一个 QC 代码扫描程序包(通常是 react-native-camera 的包装器),由于其背后的本机代码,它们都扫描完整图像。因此,如果您的框架中有多个 QC 代码。扫描正确的会有些麻烦。
        • 所以没有办法让它只扫描摄像机视图的一部分?
        • @random1234 我没能做到。但是从用户的角度来看,他们根本不会注意到。
        • 如果 QR 码彼此靠近并位于上方,它们会这样做 =(
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        • 2019-04-04
        • 1970-01-01
        相关资源
        最近更新 更多