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
}
}
网友评论