Skip to content

Commit

Permalink
Fixed detectQRCode (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldavidw authored Nov 12, 2024
1 parent 2aa2933 commit 688a0e6
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions ChronosTests/Token/QrCodeGenerationAndParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,40 @@ final class QrCodeGenerationAndParsingTests: XCTestCase {
}

func detectQRCode(in image: UIImage, completion: @escaping (String?) -> Void) {
// Ensure the image has a CGImage representation
guard let cgImage = image.cgImage else {
completion(nil)
return
}

// Configure the request to detect barcodes
let request = VNDetectBarcodesRequest { request, error in
guard error == nil else {
completion(nil)
if let error = error {
print("Barcode detection error: \(error.localizedDescription)")
DispatchQueue.main.async { completion(nil) }
return
}

let results = request.results as? [VNBarcodeObservation]
let payload = results?.first?.payloadStringValue
completion(payload)
// Extract the first payload string if available
if let results = request.results as? [VNBarcodeObservation],
let payload = results.first?.payloadStringValue
{
DispatchQueue.main.async { completion(payload) }
} else {
DispatchQueue.main.async { completion(nil) }
}
}

// Create an image request handler for the given CGImage
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])

// Perform the request asynchronously
DispatchQueue.global(qos: .userInitiated).async {
do {
try handler.perform([request])
} catch {
completion(nil)
print("Failed to perform barcode detection: \(error.localizedDescription)")
DispatchQueue.main.async { completion(nil) }
}
}
}

0 comments on commit 688a0e6

Please sign in to comment.