Skip to content

Instantly share code, notes, and snippets.

@toruuetani
Created March 10, 2016 08:25
Show Gist options
  • Save toruuetani/582a4d67d3c9a2a175cd to your computer and use it in GitHub Desktop.
Save toruuetani/582a4d67d3c9a2a175cd to your computer and use it in GitHub Desktop.
バーコード読取 by AVFoundation
import UIKit
import AVFoundation
class BarcodeEncodingViewController: UIViewController {
private let session = AVCaptureSession()
private var prevLayer: AVCaptureVideoPreviewLayer!
private var hView = UIView()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupCapture()
}
private func setupCapture() {
self.hView.autoresizingMask = [
UIViewAutoresizing.FlexibleTopMargin
, UIViewAutoresizing.FlexibleBottomMargin
, UIViewAutoresizing.FlexibleLeftMargin
, UIViewAutoresizing.FlexibleRightMargin
]
self.hView.layer.borderColor = UIColor.greenColor().CGColor
self.hView.layer.borderWidth = 3
self.view.addSubview(self.hView)
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var input: AVCaptureDeviceInput?
do {
input = try AVCaptureDeviceInput(device: device)
self.session.addInput(input)
} catch {
input = nil
}
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
self.session.addOutput(output)
output.metadataObjectTypes = output.availableMetadataObjectTypes
self.prevLayer = AVCaptureVideoPreviewLayer(session: self.session)
self.prevLayer.frame = self.view.bounds
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.addSublayer(self.prevLayer)
session.startRunning()//開始!
}
}
extension BarcodeEncodingViewController: AVCaptureMetadataOutputObjectsDelegate {
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
var highlightViewRect = CGRectZero
var barCodeObject : AVMetadataObject!
var detectionString : String!
//対応バーコードタイプ
let barCodeTypes = [
AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
//複数のバーコードの同時取得も可能
for metadata in metadataObjects {
for barcodeType in barCodeTypes {
if metadata.type == barcodeType {
barCodeObject = self.prevLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject)
highlightViewRect = barCodeObject.bounds
detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
self.session.stopRunning()
break
}
}
}
print(detectionString)
self.prevLayer.frame = highlightViewRect
self.view.bringSubviewToFront(self.hView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment