OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/shape_detection/barcode_detection_impl_mac.h" | 5 #include "services/shape_detection/barcode_detection_impl_mac.h" |
6 | 6 |
7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
8 #include "base/mac/scoped_cftyperef.h" | 8 #include "base/mac/scoped_cftyperef.h" |
9 #include "base/mac/sdk_forward_declarations.h" | 9 #include "base/mac/sdk_forward_declarations.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 BarcodeDetectionImplMac::BarcodeDetectionImplMac() { | 45 BarcodeDetectionImplMac::BarcodeDetectionImplMac() { |
46 NSDictionary* const options = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; | 46 NSDictionary* const options = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; |
47 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeQRCode | 47 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeQRCode |
48 context:nil | 48 context:nil |
49 options:options] retain]); | 49 options:options] retain]); |
50 } | 50 } |
51 | 51 |
52 BarcodeDetectionImplMac::~BarcodeDetectionImplMac() {} | 52 BarcodeDetectionImplMac::~BarcodeDetectionImplMac() {} |
53 | 53 |
54 void BarcodeDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, | 54 void BarcodeDetectionImplMac::Detect(const SkBitmap& bitmap, |
55 uint32_t width, | |
56 uint32_t height, | |
57 const DetectCallback& callback) { | 55 const DetectCallback& callback) { |
58 media::ScopedResultCallback<DetectCallback> scoped_callback( | 56 media::ScopedResultCallback<DetectCallback> scoped_callback( |
59 base::Bind(&RunCallbackWithBarcodes, callback), | 57 base::Bind(&RunCallbackWithBarcodes, callback), |
60 base::Bind(&RunCallbackWithNoBarcodes)); | 58 base::Bind(&RunCallbackWithNoBarcodes)); |
61 | 59 |
62 base::scoped_nsobject<CIImage> ci_image = | 60 base::scoped_nsobject<CIImage> ci_image = CreateCIImageFromSkBitmap(bitmap); |
63 CreateCIImageFromSharedMemory(std::move(frame_data), width, height); | |
64 if (!ci_image) | 61 if (!ci_image) |
65 return; | 62 return; |
66 | 63 |
67 NSArray* const features = [detector_ featuresInImage:ci_image]; | 64 NSArray* const features = [detector_ featuresInImage:ci_image]; |
68 | 65 |
69 std::vector<mojom::BarcodeDetectionResultPtr> results; | 66 std::vector<mojom::BarcodeDetectionResultPtr> results; |
| 67 const int height = bitmap.height(); |
70 for (CIQRCodeFeature* const f in features) { | 68 for (CIQRCodeFeature* const f in features) { |
71 shape_detection::mojom::BarcodeDetectionResultPtr result = | 69 shape_detection::mojom::BarcodeDetectionResultPtr result = |
72 shape_detection::mojom::BarcodeDetectionResult::New(); | 70 shape_detection::mojom::BarcodeDetectionResult::New(); |
73 // In the default Core Graphics coordinate space, the origin is located | 71 // In the default Core Graphics coordinate space, the origin is located |
74 // in the lower-left corner, and thus |ci_image| is flipped vertically. | 72 // in the lower-left corner, and thus |ci_image| is flipped vertically. |
75 // We need to adjust |y| coordinate of bounding box before sending it. | 73 // We need to adjust |y| coordinate of bounding box before sending it. |
76 gfx::RectF boundingbox(f.bounds.origin.x, | 74 gfx::RectF boundingbox(f.bounds.origin.x, |
77 height - f.bounds.origin.y - f.bounds.size.height, | 75 height - f.bounds.origin.y - f.bounds.size.height, |
78 f.bounds.size.width, f.bounds.size.height); | 76 f.bounds.size.width, f.bounds.size.height); |
79 result->bounding_box = std::move(boundingbox); | 77 result->bounding_box = std::move(boundingbox); |
80 | 78 |
81 // Enumerate corner points starting from top-left in clockwise fashion: | 79 // Enumerate corner points starting from top-left in clockwise fashion: |
82 // https://wicg.github.io/shape-detection-api/#dom-detectedbarcode-cornerpoi
nts | 80 // https://wicg.github.io/shape-detection-api/#dom-detectedbarcode-cornerpoi
nts |
83 result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y); | 81 result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y); |
84 result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y); | 82 result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y); |
85 result->corner_points.emplace_back(f.bottomRight.x, | 83 result->corner_points.emplace_back(f.bottomRight.x, |
86 height - f.bottomRight.y); | 84 height - f.bottomRight.y); |
87 result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y); | 85 result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y); |
88 | 86 |
89 result->raw_value = base::SysNSStringToUTF8(f.messageString); | 87 result->raw_value = base::SysNSStringToUTF8(f.messageString); |
90 results.push_back(std::move(result)); | 88 results.push_back(std::move(result)); |
91 } | 89 } |
92 scoped_callback.Run(std::move(results)); | 90 scoped_callback.Run(std::move(results)); |
93 } | 91 } |
94 | 92 |
95 } // namespace shape_detection | 93 } // namespace shape_detection |
OLD | NEW |