简体   繁体   English

条码扫描器反应原生 android UI 组件未显示

[英]Barcode scanner react-native android UI component not showing up

I'm trying to build a custom native UI components for android in react-native.我正在尝试在 react-native 中为 android 构建自定义本机 UI 组件。 In particular, I'm trying to build a simple barcodescanner using this library: https://github.com/dm77/barcodescanner特别是,我正在尝试使用此库构建一个简单的条码扫描仪: https : //github.com/dm77/barcodescanner

I've followed the steps on the react native guide here and also read this blog post .我已经按照此处的 react native 指南中的步骤操作,并阅读了这篇博文

The problem is that I can see the View of the component with its background.问题是我可以看到带有背景的组件的视图。 The camera is not opening though.虽然相机没有打开。 I can't figure out wether I'm missing something from the api or if I'm using them wrong.我不知道我是否从 api 中遗漏了一些东西,或者我是否使用错误。

I've set up a sample repo for the issue: https://github.com/themutt/react-native-zbar-camera-test我已经为这个问题设置了一个示例仓库: https : //github.com/themutt/react-native-zbar-camera-test

and you can find on the bottom of the post the screenshot of the results (I'm expecting a view with a camera but I've just the background of the view instead)你可以在帖子的底部找到结果的截图(我期待一个带相机的视图,但我只是视图的背景)

build.gradle构建.gradle

dependencies {
  compile fileTree(dir: "libs", include: ["*.jar"])
  compile "com.android.support:appcompat-v7:23.0.1"
  compile "com.facebook.react:react-native:+"  // From node_modules
  compile 'me.dm7.barcodescanner:zbar:1.9.8'
}

MainActivity.java主活动.java

package com.zbarcamera;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from 
 JavaScript.
     * This is used to schedule rendering of the component.
     */
     @Override
     protected String getMainComponentName() {
         return "ZBarCamera";
    }
}

MainApplication.java主应用程序

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new ScannerReactPackage()
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

ReactBarcodeManagerView.java ReactBarcodeManagerView.java

public class ReactBarcodeViewManager extends SimpleViewManager<ZBarScannerView> {
    public static final String REACT_CLASS = "ZBarCamera";

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public ZBarScannerView createViewInstance(final ThemedReactContext context) {
        final ZBarScannerView mScannerView = new ZBarScannerView(context);
        mScannerView.startCamera();
        mScannerView.setResultHandler(new ZBarScannerView.ResultHandler() {
            @Override
            public void handleResult(Result result) {
                WritableMap event = Arguments.createMap();
                event.putString("barcode", result.getContents());
                context.getJSModule(RCTEventEmitter.class).receiveEvent(
                        mScannerView.getId(),
                        "topChange",
                        event
                );
            }
        });
        return mScannerView;
    }

}

ScannerReactPackage.java ScannerReactPackage.java

public class ScannerReactPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(
            ReactApplicationContext reactContext) {
        return Arrays.<ViewManager>asList(
                new ReactBarcodeViewManager()
        );
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}

AndroidManifest.xml AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />

On the javascript side:在javascript方面:

Barcode.js条码.js

import React from 'react'
import PropTypes from 'prop-types'
import { requireNativeComponent, Text, View } from 'react-native'

class Barcode extends React.Component {
    constructor(props) {
        super(props)
        this._onChange = this._onChange.bind(this)
    }
    _onChange(event) {
        if (!this.props.onBarcodeRead) {
            return
        }
        this.props.onBarcodeRead(event.nativeEvent)
    }

    render() {
        return (
            <ZBarCamera 
                {...this.props} 
                style={{width: '100%', height: '50%', backgroundColor: 'tomato'}} 
                onChange={this._onChange} 
            />
        )
    }
}

Barcode.propTypes = {
    onBarcodeRead: PropTypes.func,
    ...View.propTypes
}

const ZBarCamera = requireNativeComponent('ZBarCamera', Barcode, {
    nativeOnly: {
        onChange: true
    }
})

export default Barcode

And on my index.android.js在我的 index.android.js 上

export default class ZBarCamera extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Barcode
          onBarcodeRead={(data) => console.log(data)}
        />
      </View>
    );
  }
}

截图预览

I also ran out of ideas to get that barcode scanner to work. 我也想尽办法使条形码扫描仪正常工作。 My solution was to switch to the react-native-camera 我的解决方案是切换到react-native-camera

HTH, Mike HTH,迈克

set width and height in styles在样式中设置宽度和高度

<BarcodeScanner
        onBarCodeRead={barcodeReceived}
        style={{flex: 1, width: Dimensions.get('window').width, height: 300}}
        torchMode={'off'}
        cameraType={'back'}
      />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM