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.
 
 
 
 
 
 

69 lines
2.4 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. default:
  47. result(FlutterMethodNotImplemented)
  48. return
  49. }
  50. })
  51. return previewView
  52. }
  53. func setDimensions(width: Double, height: Double) -> Void {
  54. previewView.frame = CGRect(x: 0, y: 0, width: width, height: height)
  55. scanner = MTBBarcodeScanner(previewView: previewView)
  56. MTBBarcodeScanner.requestCameraPermission(success: isCameraAvailable)
  57. }
  58. func flipCamera(){
  59. scanner?.flipCamera()
  60. }
  61. }