[iOS] QR코드 리더기
2024. 1. 6. 15:24
AVCaptureVideoPreviewLayer와 AVCaptureSession 선언
private var videoLayer = AVCaptureVideoPreviewLayer()
private var captureSession = AVCaptureSession()
기기가 회전될 때 View도 인식, 그리고 QR코드를 인식하기 위해 함수 선언
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Camera View rotate Notification
NotificationCenter.default.addObserver(self,
selector: #selector(orientationChanged(_:)),
name: UIDevice.orientationDidChangeNotification,
object: nil)
startReading() // 인식 성공
}
override func viewWillDisappear(_ animated: Bool) {
// Camera View rotate Notification
NotificationCenter.default.removeObserver(self,
name: UIDevice.orientationDidChangeNotification,
object: nil)
stopOnlyReading() // 인식 종료
}
// 기기가 회전할 때
@objc func orientationChanged(_ notification: Notification){
if(UIDevice.current.userInterfaceIdiom == .pad){ // 아이패드 사용
if let connection = videoLayer.connection{
let currentConnection = UIDevice.current.orientation
switch currentConnection {
case .portrait:
connection.videoOrientation = .portrait
case .landscapeLeft:
connection.videoOrientation = .landscapeRight
case .landscapeRight:
connection.videoOrientation = .landscapeLeft
default:
connection.videoOrientation = .portrait
}
}
}
}
인식할 때 함수
// 인식 시작
private func startReading(){
DispatchQueue.main.async {
guard let captureDevice = AVCaptureDevice.default(for: .video) else{return}
var input: AVCaptureDeviceInput?
do{
input = try AVCaptureDeviceInput(device: captureDevice)
}
catch let e{
NSLog("Failed to AVCaptureDeviceInput \(e.localizedDescription)")
return
}
self.captureSession = AVCaptureSession()
self.captureSession.addInput(input!)
let captureMetadataOutput = AVCaptureMetadataOutput()
self.captureSession.addOutput(captureMetadataOutput)
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = [.qr]
self.videoLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.videoLayer.videoGravity = .resizeAspectFill
self.videoLayer.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width,
height: self.view.frame.size.height - (self.tabBarController?.tabBar.frame.height ?? 0.0))
self.ViewScanner.layer.addSublayer(self.videoLayer)
DispatchQueue.global(qos: .background).async {
self.captureSession.startRunning()
}
}
}
결과값 처리
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
if let metadataObject = metadataObjects.first{
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
let stringValue = readableObject.stringValue else{return}
self.performSelector(onMainThread: #selector(authenticationAndStopReading),
with: nil,
waitUntilDone: false)
qrCodeString = stringValue // 결과값
}
}
출처
728x90
'iOS > Swift' 카테고리의 다른 글
[iOS] 아이폰에서 Dump뜨기 (0) | 2024.01.13 |
---|---|
[Swift] Dispatch (2) (0) | 2024.01.07 |
[iOS] LocalAuthentication (0) | 2024.01.06 |
[Swift] URL, URLComponents (0) | 2023.12.30 |
[Swift] Dispatch (1) (0) | 2023.11.23 |