feat: Implement cornerPoints and frame for scanned codes (#2117)

* Android & TypeScript part of scanned code corner points. Scanned frame dimensions also included in callback. #2076

* TS fix. #2076

* Implement iOS parts of code scanner corner points with additional scanned frame data.

* Add example page for code scanning

* Use Point type from Point.ts

* Update package/src/CodeScanner.ts

Add parameters description to CodeScanner callback.

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Update package/src/CodeScanner.ts

More expressive description for CodeScannerFrame.

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Update package/src/CodeScanner.ts

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Update package/src/CodeScanner.ts

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Update package/ios/Core/CameraSession+CodeScanner.swift

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Update package/ios/Core/CameraSession+CodeScanner.swift

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* Remove default values from CodeSCannerFrame

* Linting

* Multiply code corner points in swift

---------

Co-authored-by: stemy <balazs.stemler@metrix.co.hu>
Co-authored-by: Zoli <iamzozo@metrix.co.hu>
Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>
This commit is contained in:
Metrix Hungary Kft
2023-11-09 11:57:05 +01:00
committed by GitHub
parent 24ddca3409
commit e649aba8e1
17 changed files with 317 additions and 20 deletions

View File

@@ -291,12 +291,13 @@ public final class CameraView: UIView, CameraSessionDelegate {
#endif
}
func onCodeScanned(codes: [CameraSession.Code]) {
func onCodeScanned(codes: [CameraSession.Code], scannerFrame: CameraSession.CodeScannerFrame) {
guard let onCodeScanned = onCodeScanned else {
return
}
onCodeScanned([
"codes": codes.map { $0.toJSValue() },
"frame": scannerFrame.toJSValue(),
])
}

View File

@@ -28,8 +28,12 @@ extension CameraSession: AVCaptureMetadataOutputObjectsDelegate {
// Map codes to JS values
let codes = metadataObjects.map { object in
var value: String?
var corners: [CGPoint]?
if let code = object as? AVMetadataMachineReadableCodeObject {
value = code.stringValue
corners = code.corners.map {
CGPoint(x: $0.x * Double(size.width), y: $0.y * Double(size.height))
}
}
let x = object.bounds.origin.x * Double(size.width)
let y = object.bounds.origin.y * Double(size.height)
@@ -37,11 +41,11 @@ extension CameraSession: AVCaptureMetadataOutputObjectsDelegate {
let h = object.bounds.height * Double(size.height)
let frame = CGRect(x: x, y: y, width: w, height: h)
return Code(type: object.type, value: value, frame: frame)
return Code(type: object.type, value: value, frame: frame, corners: corners)
}
// Call delegate (JS) event
onCodeScanned(codes)
onCodeScanned(codes, CodeScannerFrame(width: size.width, height: size.height))
}
/**
@@ -61,6 +65,11 @@ extension CameraSession: AVCaptureMetadataOutputObjectsDelegate {
*/
let frame: CGRect
/**
Location of the code on-screen, relative to the video output layer
*/
let corners: [CGPoint]?
/**
Converts this Code to a JS Object (Dictionary)
*/
@@ -74,6 +83,22 @@ extension CameraSession: AVCaptureMetadataOutputObjectsDelegate {
"width": frame.size.width,
"height": frame.size.height,
],
"corners": corners?.map { [
"x": $0.x,
"y": $0.y,
] } ?? [],
]
}
}
struct CodeScannerFrame {
let width: Int32
let height: Int32
func toJSValue() -> [String: AnyHashable] {
return [
"width": width,
"height": height,
]
}
}

View File

@@ -28,5 +28,5 @@ protocol CameraSessionDelegate: AnyObject {
/**
Called whenever a QR/Barcode has been scanned. Only if the CodeScanner Output is enabled
*/
func onCodeScanned(codes: [CameraSession.Code])
func onCodeScanned(codes: [CameraSession.Code], scannerFrame: CameraSession.CodeScannerFrame)
}