Last active
August 18, 2021 21:46
-
-
Save Raghvendra7/ff6335c47bbca04fb88d1cb76917d2e5 to your computer and use it in GitHub Desktop.
Scanning the barcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
import UIKit | |
class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
var captureSession: AVCaptureSession! | |
var previewLayer: AVCaptureVideoPreviewLayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.navigationItem.title = "Scanner" | |
let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: #selector(ScannerViewController.cancelAction(_:))) | |
self.navigationItem.leftBarButtonItem = cancelButton | |
view.backgroundColor = UIColor.blackColor() | |
captureSession = AVCaptureSession() | |
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) | |
let videoInput: AVCaptureDeviceInput | |
do { | |
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) | |
} catch { | |
return | |
} | |
if (captureSession.canAddInput(videoInput)) { | |
captureSession.addInput(videoInput) | |
} else { | |
failed(); | |
return; | |
} | |
let metadataOutput = AVCaptureMetadataOutput() | |
if (captureSession.canAddOutput(metadataOutput)) { | |
captureSession.addOutput(metadataOutput) | |
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) | |
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Code] | |
} else { | |
failed() | |
return | |
} | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); | |
previewLayer.frame = CGRectMake(0, ScreenConstant.height/2, ScreenConstant.width, 100); | |
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; | |
view.layer.addSublayer(previewLayer); | |
captureSession.startRunning(); | |
} | |
func failed() { | |
let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert) | |
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) | |
presentViewController(ac, animated: true, completion: nil) | |
captureSession = nil | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if (captureSession?.running == false) { | |
captureSession.startRunning(); | |
} | |
} | |
override func viewWillDisappear(animated: Bool) { | |
super.viewWillDisappear(animated) | |
if (captureSession?.running == true) { | |
captureSession.stopRunning(); | |
} | |
} | |
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { | |
captureSession.stopRunning() | |
if let metadataObject = metadataObjects.first { | |
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
foundCode(readableObject.stringValue); | |
} | |
dismissViewControllerAnimated(true, completion: nil) | |
} | |
func foundCode(code: String) { | |
print(code) | |
} | |
func cancelAction(sender:AnyObject){ | |
self.dismissViewControllerAnimated(true, completion: nil) | |
} | |
override func prefersStatusBarHidden() -> Bool { | |
return true | |
} | |
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { | |
return .Portrait | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi this is class for creating scanner in ur app
Here ScreenConstant is struct
struct ScreenConstant {
static var width = UIScreen.mainScreen().bounds.size.width
static var height = UIScreen.mainScreen().bounds.size.height
}
Support AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Code