Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release ios 3.0 #17

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cordova-plugin-qr-scanner
Cordova plugin for QR code scanner
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"platforms": [
"android",
"ios"

]
},
"description": "A starting point for a Cordova Plugin"
Expand Down
8 changes: 8 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
<resource-file src="res/drawable-xxhdpi/ic_action_arrow_right.png" target="res/drawable-xxhdpi/ic_action_arrow_right.png" />
<resource-file src="res/layout/qr_scanner_dialog.xml" target="res/layout/qr_scanner_dialog.xml" />
</platform>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="qrScanner">
<param name="ios-package" value="qrScanner" />
</feature>
</config-file>
<source-file src="src/ios/QRScannerPlugin.swift" />
</platform>
</plugin>
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/ios/.DS_Store
Binary file not shown.
181 changes: 181 additions & 0 deletions src/ios/QRScannerPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import Foundation
import AVFoundation


@objc(QRScannerPlugin) class QRScannerPlugin : CDVPlugin,AVCaptureMetadataOutputObjectsDelegate {

class CameraView: UIView {
var videoPreviewLayer:AVCaptureVideoPreviewLayer?

//Interface Method.:
func interfaceOrientationToVideoOrientation(_ orientation : UIInterfaceOrientation) -> AVCaptureVideoOrientation {
switch (orientation) {
case UIInterfaceOrientation.portrait:
return AVCaptureVideoOrientation.portrait;
case UIInterfaceOrientation.portraitUpsideDown:
return AVCaptureVideoOrientation.portraitUpsideDown;
case UIInterfaceOrientation.landscapeLeft:
return AVCaptureVideoOrientation.landscapeLeft;
case UIInterfaceOrientation.landscapeRight:
return AVCaptureVideoOrientation.landscapeRight;
default:
return AVCaptureVideoOrientation.portraitUpsideDown;
}
}

override func layoutSubviews() {
super.layoutSubviews();
if let sublayers = self.layer.sublayers {
for layer in sublayers {
layer.frame = self.bounds;
}
}

self.videoPreviewLayer?.connection?.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.shared.statusBarOrientation);
}

func addPreviewLayer(_ previewLayer:AVCaptureVideoPreviewLayer?) {
previewLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer!.frame = self.bounds
self.layer.addSublayer(previewLayer!)
self.videoPreviewLayer = previewLayer;
}

func removePreviewLayer() {
if self.videoPreviewLayer != nil {
self.videoPreviewLayer!.removeFromSuperlayer()
self.videoPreviewLayer = nil
}
}
}

var cameraView: CameraView!
var captureSession:AVCaptureSession?
var captureVideoPreviewLayer:AVCaptureVideoPreviewLayer?
var metaOutput: AVCaptureMetadataOutput?

var currentCamera: Int = 0;
var frontCamera: AVCaptureDevice?
var backCamera: AVCaptureDevice?

var scanning: Bool = false
var paused: Bool = false
var nextScanningCommand: CDVInvokedUrlCommand?

enum QRScannerError: Int32 {
case unexpected_error = 0,
camera_access_denied = 1,
camera_access_restricted = 2,
back_camera_unavailable = 3,
front_camera_unavailable = 4,
camera_unavailable = 5,
scan_canceled = 6,
light_unavailable = 7,
open_settings_unavailable = 8
}

enum CaptureError: Error {
case backCameraUnavailable
case frontCameraUnavailable
case couldNotCaptureInput(error: NSError)
}

enum LightError: Error {
case torchUnavailable
}

override func pluginInitialize() {
super.pluginInitialize()
NotificationCenter.default.addObserver(self, selector: #selector(pageDidLoad), name: NSNotification.Name.CDVPageDidLoad, object: nil)
self.cameraView = CameraView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
self.cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight];
}

func sendErrorCode(command: CDVInvokedUrlCommand, error: QRScannerError){
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.rawValue)
commandDelegate!.send(pluginResult, callbackId:command.callbackId)
}

// utility method
@objc func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
if #available(iOS 8.0, *) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
if (background != nil) {
background!()
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay * Double(NSEC_PER_SEC)) {
if(completion != nil){
completion!()
}
}
}
} else {
// Fallback for iOS < 8.0
if(background != nil){
background!()
}
if(completion != nil){
completion!()
}
}
}


@objc(qrScanner:)
func qrScanner (_ command: CDVInvokedUrlCommand,_ commond: startScanner,_ commond: stopScanner){
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: "")
commandDelegate!.send(pluginResult, callbackId:command.callbackId)

}
//Check method for startscanner and stopscanner.
func boolean execute(Action:String, JSONArray:[args]) -> bool{
if (action.equals(ACTION_QR_SCANNER)){
String type = args.String(0)
switch (type) {

case START_SCANNING:
startScanner();
break;

case STOP_SCANNING:
stopScanner();

break;

}
}
return true

}

// startScanner Method.
@objc(startScanner:)

func startScanner(_ command: CDVInvokedUrlCommand) {
var pluginResult:CDVPluginResult = CDVPluginResult.init(status: CDVCommandStatus_ERROR)
let startScanner = command.arguments[0] as? String ?? ""
let screenTitle = command.arguments[1] as? String ?? "Scan QR Code."
let displayText = command.arguments[2] as? String ?? "Point your phone to the QR code to scan it"
let displayTextColor = command.arguments[3] as? String ?? "0b0b0b"
let buttonText = command.arguments[4] as? String ?? "I don't have a QR Code"
let showButton = command.arguments[5] as? Bool ?? false
let isRtl = command.arguments[6] as? Bool ?? false

print("Start Scanning Successfully.")
pluginResult = CDVPluginResult.init(status: CDVCommandStatus_OK, messageAs: "Start Scanning Successfully.")
self.commandDelegate.send(pluginResult, callbackId: command.callbackId)

}

}

// stopScanner Method.
@objc(stopScanner:)
func stopScanner(_ command: CDVInvokedUrlCommand) {

print("Stop Scanning Successfully.")

let pluginResult:CDVPluginResult = CDVPluginResult.init(status: CDVCommandStatus_OK, messageAs: "Stop Scanning Successfully.")
self.commandDelegate.send(pluginResult, callbackId: command.callbackId)

}