Skip to content

Commit

Permalink
[Example] Update the app to allow scanning multiple documents
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmazeev committed Feb 2, 2025
1 parent 8eb7e9b commit 4de797a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
21 changes: 13 additions & 8 deletions Example/MRZScannerExample/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
//

@preconcurrency import AVFoundation
import CoreImage
@preconcurrency import CoreImage
import ConcurrencyExtras

actor Camera {
nonisolated let captureSession = AVCaptureSession()
final class Camera: Sendable {
let captureSession = AVCaptureSession()
private let outputSampleBufferDelegate = OutputSampleBufferDelegate()

var imageStream: AsyncStream<CIImage>? {
var imageStream: AsyncStream<CIImage> {
.init { continuation in
outputSampleBufferDelegate.continuation = continuation
outputSampleBufferDelegate.continuation.withValue { $0 = continuation }

continuation.onTermination = { _ in
self.outputSampleBufferDelegate.continuation.withValue { $0 = nil }
}
}
}

Expand Down Expand Up @@ -68,12 +73,12 @@ actor Camera {
}
}

final private class OutputSampleBufferDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
var continuation: AsyncStream<CIImage>.Continuation?
private final class OutputSampleBufferDelegate: NSObject, Sendable, AVCaptureVideoDataOutputSampleBufferDelegate {
let continuation: LockIsolated<AsyncStream<CIImage>.Continuation?> = .init(nil)

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = sampleBuffer.imageBuffer else { return }

continuation?.yield(CIImage(cvPixelBuffer: pixelBuffer))
continuation.value?.yield(CIImage(cvPixelBuffer: pixelBuffer))
}
}
18 changes: 8 additions & 10 deletions Example/MRZScannerExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ struct ContentView: View {
.alert(isPresented: .init(get: { viewModel.result != nil }, set: { _ in viewModel.result = nil })) {
Alert(
title: Text(createAlertTitle(result: viewModel.result!)),
message: Text(createAlertMessage(result: viewModel.result!))
message: Text(createAlertMessage(result: viewModel.result!)),
dismissButton: .default(Text("Restart scanning")) {
Task {
guard let cameraRect, let mrzRect else { return }

await viewModel.startMRZScanning(cameraRect: cameraRect, mrzRect: mrzRect)
}
}
)
}
.task {
Expand Down Expand Up @@ -118,15 +125,6 @@ struct ContentView: View {
}
}

extension CGRect: @retroactive Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(origin.x)
hasher.combine(origin.y)
hasher.combine(size.width)
hasher.combine(size.height)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down
8 changes: 3 additions & 5 deletions Example/MRZScannerExample/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Created by Roman Mazeev on 01/01/2023.
//

@preconcurrency import AVFoundation
@preconcurrency import SwiftUI
import AVFoundation
import CoreImage
import MRZScanner
import Vision

Expand All @@ -29,10 +29,8 @@ final class ViewModel: ObservableObject {
}

func startMRZScanning(cameraRect: CGRect, mrzRect: CGRect) async {
guard let imageStream = await camera.imageStream else { return }

do {
try await scanImageStream(imageStream, cameraRect: cameraRect, mrzRect: mrzRect)
try await scanImageStream(camera.imageStream, cameraRect: cameraRect, mrzRect: mrzRect)
} catch {
result = .failure(error)
}
Expand Down

0 comments on commit 4de797a

Please sign in to comment.