barcode_detection_impl_mac.mm 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/shape_detection/barcode_detection_impl_mac.h"
  5. #include "base/mac/mac_util.h"
  6. #include "base/mac/scoped_cftyperef.h"
  7. #include "base/strings/sys_string_conversions.h"
  8. #include "services/shape_detection/detection_utils_mac.h"
  9. namespace shape_detection {
  10. BarcodeDetectionImplMac::BarcodeDetectionImplMac() {
  11. NSDictionary* const options = @{CIDetectorAccuracy : CIDetectorAccuracyHigh};
  12. detector_.reset([[CIDetector detectorOfType:CIDetectorTypeQRCode
  13. context:nil
  14. options:options] retain]);
  15. }
  16. BarcodeDetectionImplMac::~BarcodeDetectionImplMac() {}
  17. void BarcodeDetectionImplMac::Detect(const SkBitmap& bitmap,
  18. DetectCallback callback) {
  19. base::scoped_nsobject<CIImage> ci_image = CreateCIImageFromSkBitmap(bitmap);
  20. if (!ci_image) {
  21. std::move(callback).Run({});
  22. return;
  23. }
  24. NSArray* const features = [detector_ featuresInImage:ci_image];
  25. std::vector<mojom::BarcodeDetectionResultPtr> results;
  26. const int height = bitmap.height();
  27. for (CIQRCodeFeature* const f in features) {
  28. shape_detection::mojom::BarcodeDetectionResultPtr result =
  29. shape_detection::mojom::BarcodeDetectionResult::New();
  30. result->bounding_box = ConvertCGToGfxCoordinates(f.bounds, height);
  31. // Enumerate corner points starting from top-left in clockwise fashion:
  32. // https://wicg.github.io/shape-detection-api/#dom-detectedbarcode-cornerpoints
  33. result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y);
  34. result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y);
  35. result->corner_points.emplace_back(f.bottomRight.x,
  36. height - f.bottomRight.y);
  37. result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y);
  38. result->raw_value = base::SysNSStringToUTF8(f.messageString);
  39. result->format = mojom::BarcodeFormat::QR_CODE;
  40. results.push_back(std::move(result));
  41. }
  42. std::move(callback).Run(std::move(results));
  43. }
  44. // static
  45. std::vector<shape_detection::mojom::BarcodeFormat>
  46. BarcodeDetectionImplMac::GetSupportedSymbologies() {
  47. return {mojom::BarcodeFormat::QR_CODE};
  48. }
  49. } // namespace shape_detection