The QR Code Scanner package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

103 line
3.3 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. case "pauseCamera":
  49. self?.pauseCamera()
  50. case "resumeCamera":
  51. self?.resumeCamera()
  52. default:
  53. result(FlutterMethodNotImplemented)
  54. return
  55. }
  56. })
  57. return previewView
  58. }
  59. func setDimensions(width: Double, height: Double) -> Void {
  60. previewView.frame = CGRect(x: 0, y: 0, width: width, height: height)
  61. scanner = MTBBarcodeScanner(previewView: previewView)
  62. MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable)
  63. }
  64. func flipCamera(){
  65. if let sc: MTBBarcodeScanner = scanner {
  66. if sc.hasOppositeCamera() {
  67. sc.flipCamera()
  68. }
  69. }
  70. }
  71. func flipFlash(){
  72. if let sc: MTBBarcodeScanner = scanner {
  73. if sc.hasTorch() {
  74. sc.toggleTorch()
  75. }
  76. }
  77. }
  78. func pauseCamera() {
  79. if let sc: MTBBarcodeScanner = scanner {
  80. if sc.isScanning() {
  81. sc.freezeCapture()
  82. }
  83. }
  84. }
  85. func resumeCamera() {
  86. if let sc: MTBBarcodeScanner = scanner {
  87. if !sc.isScanning() {
  88. sc.unfreezeCapture()
  89. }
  90. }
  91. }
  92. }