Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "jp.ne.sapporoworks.AVCaptureMetadataOutputSample-EAN13CodeReader";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -279,7 +279,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "jp.ne.sapporoworks.AVCaptureMetadataOutputSample-EAN13CodeReader";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
21 changes: 10 additions & 11 deletions AVCaptureMetadataOutputSample_EAN13CodeReader/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
let captureSession = AVCaptureSession()

// 入力(背面カメラ)
let videoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
let videoInput = try! AVCaptureDeviceInput.init(device: videoDevice)
let videoDevice = AVCaptureDevice.default(for: AVMediaType.video)
let videoInput = try! AVCaptureDeviceInput.init(device: videoDevice!)
captureSession.addInput(videoInput)

// 出力(ビデオデータ)
Expand All @@ -37,7 +37,7 @@ class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
// メタデータを検出した際のデリゲート設定
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
// EAN-13コードの認識を設定
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code]
metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.ean13, AVMetadataObject.ObjectType.ean8]

// 検出エリアのビュー
let x: CGFloat = 0.05
Expand All @@ -54,11 +54,10 @@ class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
metadataOutput.rectOfInterest = CGRect(x: y,y: 1-x-width,width: height,height: width)

// プレビュー
if let videoLayer = AVCaptureVideoPreviewLayer.init(session: captureSession) {
videoLayer.frame = previewView.bounds
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewView.layer.addSublayer(videoLayer)
}
let videoLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoLayer.frame = previewView.bounds
videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewView.layer.addSublayer(videoLayer)

// セッションの開始
DispatchQueue.global(qos: .userInitiated).async {
Expand All @@ -69,11 +68,11 @@ class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
timer.fire()
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
// 複数のメタデータを検出できる
for metadata in metadataObjects as! [AVMetadataMachineReadableCodeObject] {
// EAN-13Qコードのデータかどうかの確認
if metadata.type == AVMetadataObjectTypeEAN13Code || metadata.type == AVMetadataObjectTypeEAN8Code{
if metadata.type == AVMetadataObject.ObjectType.ean13 || metadata.type == AVMetadataObject.ObjectType.ean8 {
if metadata.stringValue != nil {
// 検出データを取得
counter = 0
Expand All @@ -89,7 +88,7 @@ class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
}
}

func update(tm: Timer) {
@objc func update(tm: Timer) {
counter += 1
print(counter)
if 1 < counter {
Expand Down