美文网首页
Flutter自定义扫码插件

Flutter自定义扫码插件

作者: 树生1995 | 来源:发表于2018-11-10 16:07 被阅读511次
    Screenshot_20181110-160005.jpg

    github地址:https://github.com/leyan95/qrcode_scanner

    class QrcodeScanPlugin(val activity: Activity) : MethodCallHandler,
            PluginRegistry.ActivityResultListener {
      private var result: Result? = null
    
      companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar): Unit {
          val channel = MethodChannel(registrar.messenger(), "com.leyan.qrcode_scan")
          val plugin = QrcodeScanPlugin(registrar.activity())
          channel.setMethodCallHandler(plugin)
          registrar.addActivityResultListener(plugin)
    
          ZXingLibrary.initDisplayOpinion(registrar.activity())
        }
      }
    
      override fun onMethodCall(call: MethodCall, result: Result): Unit {
        if (call.method == "scan") {
          this.result = result
          showBarcodeView()
        } else {
          result.notImplemented()
        }
      }
    
      private fun showBarcodeView() {
        val intent = Intent(activity, CaptureActivity::class.java)
        activity.startActivityForResult(intent, 100)
      }
    
      override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
        if (code == 100) {
          if (resultCode == Activity.RESULT_OK && data != null) {
            val bundle = data.extras
            if (bundle != null) {
              if (bundle.getInt(RESULT_TYPE) == RESULT_SUCCESS) {
                val barcode = bundle.getString(CodeUtils.RESULT_STRING)
                barcode?.let {
                  this.result?.success(barcode)
                }
              }
            }
          } else {
            val errorCode = data?.getStringExtra("ERROR_CODE")
            this.result?.error(errorCode, null, null)
          }
          return true
        }
        return false
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Flutter自定义扫码插件

          本文链接:https://www.haomeiwen.com/subject/pdgzxqtx.html