Chromium Code Reviews| 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/strings/sys_string_conversions.h" | |
| 10 #include "media/capture/video/scoped_result_callback.h" | |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 12 #include "services/shape_detection/barcode_detection_impl.h" | |
| 13 #include "services/shape_detection/detection_utils_mac.h" | |
| 14 | |
| 15 namespace shape_detection { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 void RunCallbackWithBarcodes( | |
| 20 const shape_detection::mojom::BarcodeDetection::DetectCallback& callback, | |
| 21 std::vector<shape_detection::mojom::BarcodeDetectionResultPtr> results) { | |
| 22 callback.Run(std::move(results)); | |
| 23 } | |
| 24 | |
| 25 void RunCallbackWithNoBarcodes( | |
| 26 const shape_detection::mojom::BarcodeDetection::DetectCallback& callback) { | |
| 27 callback.Run( | |
| 28 std::vector<shape_detection::mojom::BarcodeDetectionResultPtr>()); | |
| 29 } | |
| 30 | |
| 31 } // anonymous namespace | |
| 32 | |
| 33 //static | |
|
Robert Sesek
2017/02/01 19:34:11
nit: space after /
mcasas
2017/02/01 20:40:20
Done.
| |
| 34 void BarcodeDetectionImpl::Create( | |
| 35 shape_detection::mojom::BarcodeDetectionRequest request) { | |
| 36 // Barcode detection needs at least MAC OS X 10.10. | |
| 37 if (!base::mac::IsAtLeastOS10_10()) | |
| 38 return; | |
|
Robert Sesek
2017/02/01 19:34:11
Does this not need to error out?
mcasas
2017/02/01 20:40:20
No; mojo requests should be dropped so that the cl
| |
| 39 mojo::MakeStrongBinding(base::MakeUnique<BarcodeDetectionImplMac>(), | |
| 40 std::move(request)); | |
| 41 } | |
| 42 | |
| 43 BarcodeDetectionImplMac::BarcodeDetectionImplMac() { | |
| 44 context_.reset([[CIContext alloc] init]); | |
|
Robert Sesek
2017/02/01 19:34:11
Is it worth finding a way to share the CIContext b
mcasas
2017/02/01 20:40:20
Not really, I think the idea was to have a CIConte
| |
| 45 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; | |
| 46 | |
| 47 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10 | |
|
Robert Sesek
2017/02/01 19:34:11
The general approach to doing this is to a) do it
mcasas
2017/02/01 20:40:21
Cool. Done.
| |
| 48 // CIDetectorTypeQRCode is available in MAC OS X 10.10, so should be available | |
| 49 // in runtime because of the base::mac::IsAtLeastOS10_10() in Create(), but we | |
| 50 // need to define it at compile time because the SDK version is 10.9. | |
| 51 NSString* kCIDetectorTypeQRCode = @"CIDetectorTypeQRCode"; | |
| 52 #else | |
| 53 NSString* kCIDetectorTypeQRCode = CIDetectorTypeQRCode; | |
| 54 DCHECK([kCIDetectorTypeQRCode isEqual:@"CIDetectorTypeQRCode"]); | |
| 55 #endif | |
| 56 detector_.reset([[CIDetector detectorOfType:kCIDetectorTypeQRCode | |
| 57 context:context_ | |
| 58 options:opts] retain]); | |
| 59 } | |
| 60 | |
| 61 BarcodeDetectionImplMac::~BarcodeDetectionImplMac() {} | |
| 62 | |
| 63 void BarcodeDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, | |
| 64 uint32_t width, | |
| 65 uint32_t height, | |
| 66 const DetectCallback& callback) { | |
| 67 media::ScopedResultCallback<DetectCallback> scoped_callback( | |
| 68 base::Bind(&RunCallbackWithBarcodes, callback), | |
| 69 base::Bind(&RunCallbackWithNoBarcodes)); | |
| 70 | |
| 71 base::scoped_nsobject<CIImage> ci_image = | |
| 72 CreateCIImageFromSharedMemory(std::move(frame_data), width, height); | |
| 73 if (!ci_image) | |
| 74 return; | |
| 75 | |
| 76 NSArray* const features = [detector_ featuresInImage:ci_image]; | |
| 77 | |
| 78 std::vector<mojom::BarcodeDetectionResultPtr> results; | |
| 79 for (CIQRCodeFeature* const f in features) { | |
| 80 shape_detection::mojom::BarcodeDetectionResultPtr result = | |
| 81 shape_detection::mojom::BarcodeDetectionResult::New(); | |
| 82 // In the default Core Graphics coordinate space, the origin is located | |
| 83 // in the lower-left corner, and thus |ci_image| is flipped vertically. | |
| 84 // We need to adjust |y| coordinate of bounding box before sending it. | |
| 85 gfx::RectF boundingbox(f.bounds.origin.x, | |
| 86 height - f.bounds.origin.y - f.bounds.size.height, | |
| 87 f.bounds.size.width, f.bounds.size.height); | |
| 88 result->bounding_box = std::move(boundingbox); | |
| 89 | |
| 90 // Enumerate corner points starting from top-left in clockwise fashion: | |
| 91 // https://wicg.github.io/shape-detection-api/#dom-detectedbarcode-cornerpoi nts | |
| 92 result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y); | |
| 93 result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y); | |
| 94 result->corner_points.emplace_back(f.bottomRight.x, | |
| 95 height - f.bottomRight.y); | |
| 96 result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y); | |
| 97 | |
| 98 result->raw_value = base::SysNSStringToUTF8(f.messageString); | |
| 99 results.push_back(std::move(result)); | |
| 100 } | |
| 101 scoped_callback.Run(std::move(results)); | |
| 102 } | |
| 103 | |
| 104 } // namespace shape_detection | |
| OLD | NEW |