The QR Code Scanner package for Appeto SDK.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

83 linhas
2.8 KiB

  1. //
  2. // QRView.swift
  3. // flutter_qr
  4. //
  5. // Created by Julius Canute on 21/12/18.
  6. //
  7. import Foundation
  8. import MTBBarcodeScanner
  9. public class QRView:NSObject,FlutterPlatformView {
  10. @IBOutlet var previewView: UIView!
  11. var scanner: MTBBarcodeScanner?
  12. var registrar: FlutterPluginRegistrar
  13. var channel: FlutterMethodChannel
  14. public init(withFrame frame: CGRect, withRegistrar registrar: FlutterPluginRegistrar, withId id: Int64){
  15. self.registrar = registrar
  16. previewView = UIView(frame: frame)
  17. channel = FlutterMethodChannel(name: "net.touchcapture.qr.flutterqr/qrview_\(id)", binaryMessenger: registrar.messenger())
  18. }
  19. func isCameraAvailable(success: Bool) -> Void {
  20. if success {
  21. do {
  22. try scanner?.startScanning(resultBlock: { codes in
  23. if let codes = codes {
  24. for code in codes {
  25. let stringValue = code.stringValue!
  26. self.channel.invokeMethod("onRecognizeQR", arguments: stringValue)
  27. }
  28. }
  29. })
  30. } catch {
  31. NSLog("Unable to start scanning")
  32. }
  33. } else {
  34. UIAlertView(title: "Scanning Unavailable", message: "This app does not have permission to access the camera", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "Ok").show()
  35. }
  36. }
  37. public func view() -> UIView {
  38. channel.setMethodCallHandler({
  39. [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
  40. switch(call.method){
  41. case "setDimensions":
  42. var arguments = call.arguments as! Dictionary<String, Double>
  43. self?.setDimensions(width: arguments["width"] ?? 0,height: arguments["height"] ?? 0)
  44. case "flipCamera":
  45. self?.flipCamera()
  46. case "flipFlash":
  47. self?.flipFlash()
  48. default:
  49. result(FlutterMethodNotImplemented)
  50. return
  51. }
  52. })
  53. return previewView
  54. }
  55. func setDimensions(width: Double, height: Double) -> Void {
  56. previewView.frame = CGRect(x: 0, y: 0, width: width, height: height)
  57. scanner = MTBBarcodeScanner(previewView: previewView)
  58. MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable)
  59. }
  60. func flipCamera(){
  61. if let sc: MTBBarcodeScanner = scanner {
  62. if sc.hasOppositeCamera() {
  63. sc.flipCamera()
  64. }
  65. }
  66. }
  67. func flipFlash(){
  68. if let sc: MTBBarcodeScanner = scanner {
  69. if sc.hasTorch() {
  70. sc.toggleTorch()
  71. }
  72. }
  73. }
  74. }