1,條形碼(一維碼)的掃描讀取
原來寫過一篇文章,介紹如何使用攝像頭掃描讀取二維碼:Swift - 二維碼QRCode的讀取(從圖片讀取 ,或通過攝像頭掃描)要通過攝像頭讀取條形碼,只需要將原來二維碼讀取代碼中 metadataObjectTypes 做如下修改即可:
self.output.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code]
2,拉近鏡頭,改善條形碼讀取效果
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate,
UIAlertViewDelegate{
var scanRectView:UIView!
var device:AVCaptureDevice!
var input:AVCaptureDeviceInput!
var output:AVCaptureMetadataOutput!
var session:AVCaptureSession!
var preview:AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
fromCamera()
}
//通過攝像頭掃描
func fromCamera() {
do{
self.device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
self.input = try AVCaptureDeviceInput(device: device)
self.output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
self.session = AVCaptureSession()
if UIScreen.mainScreen().bounds.size.height<500 {
self.session.sessionPreset = AVCaptureSessionPreset640x480
}else{
self.session.sessionPreset = AVCaptureSessionPresetHigh
}
self.session.addInput(self.input)
self.session.addOutput(self.output)
self.output.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code]
//計算中間可探測區域
let windowSize:CGSize = UIScreen.mainScreen().bounds.size;
let scanSize:CGSize = CGSizeMake(windowSize.width*3/4,
windowSize.width*3/4);
var scanRect:CGRect = CGRectMake((windowSize.width-scanSize.width)/2,
(windowSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
//計算rectOfInterest 注意x,y交換位置
scanRect = CGRectMake(scanRect.origin.y/windowSize.height,
scanRect.origin.x/windowSize.width,
scanRect.size.height/windowSize.height,
scanRect.size.width/windowSize.width);
//設置可探測區域
self.output.rectOfInterest = scanRect
self.preview = AVCaptureVideoPreviewLayer(session:self.session)
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill
self.preview.frame = UIScreen.mainScreen().bounds
self.view.layer.insertSublayer(self.preview, atIndex:0)
//添加中間的探測區域綠框
self.scanRectView = UIView();
self.view.addSubview(self.scanRectView)
self.scanRectView.frame = CGRectMake(0, 0, scanSize.width, scanSize.height);
self.scanRectView.center = CGPointMake(
CGRectGetMidX(UIScreen.mainScreen().bounds),
CGRectGetMidY(UIScreen.mainScreen().bounds));
self.scanRectView.layer.borderColor = UIColor.greenColor().CGColor
self.scanRectView.layer.borderWidth = 1;
//開始捕獲
self.session.startRunning()
//放大
do {
try self.device!.lockForConfiguration()
} catch _ {
NSLog("Error: lockForConfiguration.");
}
self.device!.videoZoomFactor = 1.5
self.device!.unlockForConfiguration()
}catch _ as NSError{
//打印錯誤消息
let errorAlert = UIAlertView(title: "提醒",
message: "請在iPhone的\"設置-隱私-相機\"選項中,允許本程序訪問您的相機",
delegate: self,
cancelButtonTitle: "確定")
errorAlert.show()
}
}
//攝像頭捕獲
func captureOutput(captureOutput: AVCaptureOutput!,
didOutputMetadataObjects metadataObjects: [AnyObject]!,
fromConnection connection: AVCaptureConnection!) {
var stringValue:String?
if metadataObjects.count > 0 {
let metadataObject = metadataObjects[0]
as! AVMetadataMachineReadableCodeObject
stringValue = metadataObject.stringValue
if stringValue != nil{
self.session.stopRunning()
}
}
self.session.stopRunning()
//輸出結果
let alertView = UIAlertView(title: "二維碼", message: stringValue,
delegate: self, cancelButtonTitle: "確定")
alertView.show()
}
//消息框確認後消失
func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
//繼續掃描
self.session.startRunning()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}