同步代码

This commit is contained in:
weilu 2020-04-10 15:11:30 +08:00
parent d36a12a9c6
commit 234183a26a
5 changed files with 81 additions and 138 deletions

View File

@ -1,92 +0,0 @@
package com.apptreesoftware.barcodescan
import android.app.Activity
import android.content.Intent
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.Registrar
class BarcodeScanPlugin(): MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware {
private var result : Result? = null
private var channel: MethodChannel? = null
private var activity: Activity? = null
constructor(activity: Activity?) : this() {
this.activity = activity
}
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
if (registrar.activity() != null) {
val plugin = BarcodeScanPlugin(registrar.activity())
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
}
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "scan") {
this.result = result
showBarcodeView()
} else {
result.notImplemented()
}
}
private fun showBarcodeView() {
activity?.let {
val intent = Intent(it, BarcodeScannerActivity::class.java)
it.startActivityForResult(intent, 100)
}
}
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
if (code == 100) {
if (resultCode == Activity.RESULT_OK) {
val barcode = data?.getStringExtra("SCAN_RESULT")
barcode?.let { this.result?.success(barcode) }
} else {
val errorCode = data?.getStringExtra("ERROR_CODE")
this.result?.error(errorCode, null, null)
}
return true
}
return false
}
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(binding.binaryMessenger, "com.apptreesoftware.barcode_scan")
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
val plugin = BarcodeScanPlugin(binding.activity)
channel?.setMethodCallHandler(plugin)
binding.addActivityResultListener(plugin)
}
override fun onDetachedFromActivity() {
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}
}

View File

@ -2,7 +2,9 @@ package de.mintware.barcode_scan
import android.app.Activity
import android.content.Intent
import android.util.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
@ -10,48 +12,81 @@ import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.Registrar
class BarcodeScanPlugin(private val registrar: Registrar) : MethodCallHandler, PluginRegistry.ActivityResultListener {
var result: Result? = null
class BarcodeScanPlugin(): MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware {
private var result : Result? = null
private var channel: MethodChannel? = null
private var activity: Activity? = null
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "de.mintware.barcode_scan")
val plugin = BarcodeScanPlugin(registrar)
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
}
constructor(activity: Activity?) : this() {
this.activity = activity
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "scan") {
this.result = result
showBarcodeView()
} else {
result.notImplemented()
}
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "de.mintware.barcode_scan")
if (registrar.activity() != null) {
val plugin = BarcodeScanPlugin(registrar.activity())
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
}
}
private fun showBarcodeView() {
if (registrar.activity() == null) {
Log.e("BarcodeScanPlugin", "plugin can't launch scan activity, because plugin is not attached to any activity.")
return
}
val intent = Intent(registrar.activity(), BarcodeScannerActivity::class.java)
registrar.activity().startActivityForResult(intent, 100)
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "scan") {
this.result = result
showBarcodeView()
} else {
result.notImplemented()
}
}
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
if (code == 100) {
if (resultCode == Activity.RESULT_OK) {
val barcode = data?.getStringExtra("SCAN_RESULT")
barcode?.let { this.result?.success(barcode) }
} else {
val errorCode = data?.getStringExtra("ERROR_CODE")
this.result?.error(errorCode, null, null)
}
return true
}
return false
private fun showBarcodeView() {
activity?.let {
val intent = Intent(it, BarcodeScannerActivity::class.java)
it.startActivityForResult(intent, 100)
}
}
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
if (code == 100) {
if (resultCode == Activity.RESULT_OK) {
val barcode = data?.getStringExtra("SCAN_RESULT")
barcode?.let { this.result?.success(barcode) }
} else {
val errorCode = data?.getStringExtra("ERROR_CODE")
this.result?.error(errorCode, null, null)
}
return true
}
return false
}
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(binding.binaryMessenger, "de.mintware.barcode_scan")
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
val plugin = BarcodeScanPlugin(binding.activity)
channel?.setMethodCallHandler(plugin)
binding.addActivityResultListener(plugin)
}
override fun onDetachedFromActivity() {
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}
}

View File

@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.barcodescanexample">
package="com.apptreesoftware.barcodescanexample">
<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
@ -20,7 +20,7 @@
android:hardwareAccelerated="true"
android:exported="true"
android:launchMode="singleTop"
android:name="com.apptreesoftware.barcodescanexample.EmbeddingV1Activity"
android:name=".EmbeddingV1Activity"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">
@ -33,7 +33,7 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:name="com.apptreesoftware.barcodescanexample.MainActivity"
android:name=".MainActivity"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">
<!-- Specify that the launch screen should continue being displayed -->

View File

@ -1,6 +1,6 @@
package de.mintware.barcode_scan_example
package com.apptreesoftware.barcodescanexample
import com.apptreesoftware.barcodescan.BarcodeScanPlugin
import de.mintware.barcode_scan.BarcodeScanPlugin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

View File

@ -1,6 +1,6 @@
name: barcode_scan
description: A flutter plugin for scanning 2D barcodes and QRCodes.
version: 1.0.0
description: A flutter plugin for scanning 2D barcodes and QRCodes via camera.
version: 2.0.0
author: Matthew Smith<matthew.smith@apptreesoftware.com>
homepage: https://github.com/apptreesoftware/flutter_barcode_reader
@ -20,7 +20,7 @@ flutter:
plugin:
platforms:
android:
package: com.apptreesoftware.barcodescan
package: de.mintware.barcode_scan
pluginClass: BarcodeScanPlugin
ios:
pluginClass: BarcodeScanPlugin