| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #include "services/shape_detection/barcode_detection_impl_mac.h" |
| 6 |
| 7 #include "base/mac/mac_util.h" |
| 8 #include "base/mac/scoped_cftyperef.h" |
| 9 #include "base/mac/sdk_forward_declarations.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "media/capture/video/scoped_result_callback.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "services/shape_detection/barcode_detection_impl.h" |
| 14 #include "services/shape_detection/detection_utils_mac.h" |
| 15 |
| 16 namespace shape_detection { |
| 17 |
| 18 namespace { |
| 19 |
| 20 void RunCallbackWithBarcodes( |
| 21 const shape_detection::mojom::BarcodeDetection::DetectCallback& callback, |
| 22 std::vector<shape_detection::mojom::BarcodeDetectionResultPtr> results) { |
| 23 callback.Run(std::move(results)); |
| 24 } |
| 25 |
| 26 void RunCallbackWithNoBarcodes( |
| 27 const shape_detection::mojom::BarcodeDetection::DetectCallback& callback) { |
| 28 callback.Run( |
| 29 std::vector<shape_detection::mojom::BarcodeDetectionResultPtr>()); |
| 30 } |
| 31 |
| 32 } // anonymous namespace |
| 33 |
| 34 // static |
| 35 void BarcodeDetectionImpl::Create( |
| 36 shape_detection::mojom::BarcodeDetectionRequest request) { |
| 37 // Barcode detection needs at least MAC OS X 10.10. |
| 38 if (!base::mac::IsAtLeastOS10_10()) |
| 39 return; |
| 40 mojo::MakeStrongBinding(base::MakeUnique<BarcodeDetectionImplMac>(), |
| 41 std::move(request)); |
| 42 } |
| 43 |
| 44 BarcodeDetectionImplMac::BarcodeDetectionImplMac() { |
| 45 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; |
| 46 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeQRCode |
| 47 context:nil |
| 48 options:opts] retain]); |
| 49 } |
| 50 |
| 51 BarcodeDetectionImplMac::~BarcodeDetectionImplMac() {} |
| 52 |
| 53 void BarcodeDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, |
| 54 uint32_t width, |
| 55 uint32_t height, |
| 56 const DetectCallback& callback) { |
| 57 media::ScopedResultCallback<DetectCallback> scoped_callback( |
| 58 base::Bind(&RunCallbackWithBarcodes, callback), |
| 59 base::Bind(&RunCallbackWithNoBarcodes)); |
| 60 |
| 61 base::scoped_nsobject<CIImage> ci_image = |
| 62 CreateCIImageFromSharedMemory(std::move(frame_data), width, height); |
| 63 if (!ci_image) |
| 64 return; |
| 65 |
| 66 NSArray* const features = [detector_ featuresInImage:ci_image]; |
| 67 |
| 68 std::vector<mojom::BarcodeDetectionResultPtr> results; |
| 69 for (CIQRCodeFeature* const f in features) { |
| 70 shape_detection::mojom::BarcodeDetectionResultPtr result = |
| 71 shape_detection::mojom::BarcodeDetectionResult::New(); |
| 72 // In the default Core Graphics coordinate space, the origin is located |
| 73 // in the lower-left corner, and thus |ci_image| is flipped vertically. |
| 74 // We need to adjust |y| coordinate of bounding box before sending it. |
| 75 gfx::RectF boundingbox(f.bounds.origin.x, |
| 76 height - f.bounds.origin.y - f.bounds.size.height, |
| 77 f.bounds.size.width, f.bounds.size.height); |
| 78 result->bounding_box = std::move(boundingbox); |
| 79 |
| 80 // Enumerate corner points starting from top-left in clockwise fashion: |
| 81 // https://wicg.github.io/shape-detection-api/#dom-detectedbarcode-cornerpoi
nts |
| 82 result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y); |
| 83 result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y); |
| 84 result->corner_points.emplace_back(f.bottomRight.x, |
| 85 height - f.bottomRight.y); |
| 86 result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y); |
| 87 |
| 88 result->raw_value = base::SysNSStringToUTF8(f.messageString); |
| 89 results.push_back(std::move(result)); |
| 90 } |
| 91 scoped_callback.Run(std::move(results)); |
| 92 } |
| 93 |
| 94 } // namespace shape_detection |
| OLD | NEW |