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/text_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/detection_utils_mac.h" | |
14 #include "services/shape_detection/text_detection_impl.h" | |
15 | |
16 namespace shape_detection { | |
17 | |
18 namespace { | |
19 | |
20 void RunCallbackWithResults( | |
21 const mojom::TextDetection::DetectCallback& callback, | |
22 std::vector<mojom::TextDetectionResultPtr> results) { | |
23 callback.Run(std::move(results)); | |
24 } | |
25 | |
26 void RunCallbackWithNoResults( | |
27 const mojom::TextDetection::DetectCallback& callback) { | |
28 callback.Run(std::vector<mojom::TextDetectionResultPtr>()); | |
29 } | |
30 | |
31 } // anonymous namespace | |
32 | |
33 // static | |
34 void TextDetectionImpl::Create(mojom::TextDetectionRequest request) { | |
35 // Text detection needs at least MAC OS X 10.11. | |
36 if (!base::mac::IsAtLeastOS10_11()) | |
37 return; | |
38 mojo::MakeStrongBinding(base::MakeUnique<TextDetectionImplMac>(), | |
39 std::move(request)); | |
40 } | |
41 | |
42 TextDetectionImplMac::TextDetectionImplMac() { | |
43 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; | |
44 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeText | |
45 context:nil | |
46 options:opts] retain]); | |
47 } | |
48 | |
49 TextDetectionImplMac::~TextDetectionImplMac() {} | |
50 | |
51 void TextDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, | |
52 uint32_t width, | |
53 uint32_t height, | |
54 const DetectCallback& callback) { | |
55 DCHECK(base::mac::IsAtLeastOS10_11()); | |
56 media::ScopedResultCallback<DetectCallback> scoped_callback( | |
57 base::Bind(&RunCallbackWithResults, callback), | |
58 base::Bind(&RunCallbackWithNoResults)); | |
59 | |
60 base::scoped_nsobject<CIImage> ci_image = | |
61 CreateCIImageFromSharedMemory(std::move(frame_data), width, height); | |
62 if (!ci_image) | |
63 return; | |
64 | |
65 NSArray* const features = [detector_ featuresInImage:ci_image]; | |
66 | |
67 std::vector<mojom::TextDetectionResultPtr> results; | |
68 for (CIRectangleFeature* const f in features) { | |
69 // CIRectangleFeature only has bounding box information. | |
70 auto result = mojom::TextDetectionResult::New(); | |
71 // In the default Core Graphics coordinate space, the origin is located | |
72 // in the lower-left corner, and thus |ci_image| is flipped vertically. | |
73 // We need to adjust |y| coordinate of bounding box before sending it. | |
74 gfx::RectF boundingbox(f.bounds.origin.x, | |
75 height - f.bounds.origin.y - f.bounds.size.height, | |
76 f.bounds.size.width, f.bounds.size.height); | |
77 result->bounding_box = std::move(boundingbox); | |
78 results.push_back(std::move(result)); | |
79 } | |
80 scoped_callback.Run(std::move(results)); | |
81 } | |
82 | |
83 } // namespace shape_detection | |
OLD | NEW |