步骤一:

gradle集成Zxing

加入以下代码

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

步骤二:

生成控件调用

步骤三:设置照相机权限:

步骤四:加入实例来获取扫描结果:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);    if(result != null) {        if(result.getContents() == null) {            Toast.makeText(this, "扫码取消!", Toast.LENGTH_LONG).show();        } else {            Toast.makeText(this, "扫描成功,条码值: " + result.getContents(), Toast.LENGTH_LONG).show();        }    } else {        // This is important, otherwise the result will not be passed to the fragment        super.onActivityResult(requestCode, resultCode, data);    }}

步骤五:控件调用

public void onScanBarcode(View v){    IntentIntegrator integrator = new IntentIntegrator(this);    integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);    integrator.setPrompt("扫描条形码");    integrator.setCameraId(0);    integrator.setBeepEnabled(false);    integrator.initiateScan();}public void onScanQrcode(View v){    IntentIntegrator integrator = new IntentIntegrator(this);    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);    integrator.setPrompt("扫描二维码");    integrator.setCameraId(0);    integrator.setBeepEnabled(false);    integrator.initiateScan();}

声明:

IntentIntegrator integrator = new IntentIntegrator(this);
// 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
        
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("扫描条形码");
        integrator.setCameraId(0);  // 使用默认的相机
        
integrator.setBeepEnabled(false); // 扫到码后播放提示音
        
integrator.initiateScan();

 

上面为属性值



更多相关文章

  1. 在ubuntu下编译android和linux版webrtc的步骤
  2. AndroidStudio中使用SVG
  3. android 使用google钱包 步骤
  4. android studio 报错Failed to save settings解决方法
  5. react-native-splash-screen--RN 0.5以上报错集合
  6. Android(安卓)NDK *** could not be resolved
  7. Android(安卓)NDK开发篇(三):NDK开发步骤
  8. Android——GridView
  9. 自定义Dialog步骤

随机推荐

  1. SeekBar自定义样式最优实现
  2. Android(安卓)笔记
  3. Android常用屏幕适配方式
  4. Android基础————UI布局之UI美化分解
  5. Android画图之Matrix(二)
  6. Android中RelativeLayout布局各个xml相对
  7. 屏幕适配
  8. Retrofit系列文章翻译1—开始创建android
  9. Android(安卓)AIDL使用
  10. 学习Android界面开发: Shape Drawable