Navaron Bracke

add jpeg data extension & toMap extension

@@ -494,6 +494,45 @@ class MapArgumentReader { @@ -494,6 +494,45 @@ class MapArgumentReader {
494 494
495 } 495 }
496 496
  497 +extension CGImage {
  498 + public func jpegData(compressionQuality: CGFloat) -> Data? {
  499 + let mutableData = CFDataCreateMutable(nil, 0)
  500 +
  501 + let formatHint: CFString
  502 +
  503 + if #available(macOS 11.0, *) {
  504 + formatHint = UTType.jpeg.identifier as CFString
  505 + } else {
  506 + formatHint = kUTTypeJPEG
  507 + }
  508 +
  509 + guard let destination = CGImageDestinationCreateWithData(mutableData!, formatHint, 1, nil) else {
  510 + return nil
  511 + }
  512 +
  513 + let options: NSDictionary = [
  514 + kCGImageDestinationLossyCompressionQuality: compressionQuality,
  515 + ]
  516 +
  517 + CGImageDestinationAddImage(destination, self, options)
  518 +
  519 + if !CGImageDestinationFinalize(destination) {
  520 + return nil
  521 + }
  522 +
  523 + return mutableData as Data?
  524 + }
  525 +}
  526 +
  527 +extension VNBarcodeObservation {
  528 + public func toMap() -> [String: Any?] {
  529 + return [
  530 + "rawValue": self.payloadStringValue ?? "",
  531 + "format": self.symbology.toInt ?? -1,
  532 + ]
  533 + }
  534 +}
  535 +
497 extension VNBarcodeSymbology { 536 extension VNBarcodeSymbology {
498 static func fromInt(_ mapValue:Int) -> VNBarcodeSymbology? { 537 static func fromInt(_ mapValue:Int) -> VNBarcodeSymbology? {
499 if #available(macOS 12.0, *) { 538 if #available(macOS 12.0, *) {