Skip to content

Commit

Permalink
ShapeDetection: Get bounding box in the line of Text on Windows 10
Browse files Browse the repository at this point in the history
The OcrLine[1] has no BoundingRect properties, so the result of bounding box
need to be calculated by the words in the current line of text.

[1] https://docs.microsoft.com/en-us/uwp/api/windows.media.ocr.ocrline

BUG=866019

Cq-Include-Trybots: luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win10_chromium_x64_rel_ng
Change-Id: I7bcbd527b55b685f03a8c1783ca8c7bf7b090ee2
Reviewed-on: https://chromium-review.googlesource.com/1156054
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
Reviewed-by: Miguel Casas <mcasas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581499}
  • Loading branch information
fujunwei authored and Commit Bot committed Aug 8, 2018
1 parent 968c46d commit ecf2d44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions services/shape_detection/text_detection_impl_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/win/windows_version.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/shape_detection/text_detection_impl.h"
#include "ui/gfx/geometry/rect_f.h"

namespace shape_detection {

Expand All @@ -23,6 +24,8 @@ using ABI::Windows::Globalization::ILanguageFactory;
using ABI::Windows::Media::Ocr::IOcrEngineStatics;
using ABI::Windows::Media::Ocr::IOcrLine;
using ABI::Windows::Media::Ocr::OcrLine;
using ABI::Windows::Media::Ocr::IOcrWord;
using ABI::Windows::Media::Ocr::OcrWord;
using base::win::GetActivationFactory;
using base::win::ScopedHString;

Expand Down Expand Up @@ -200,7 +203,34 @@ TextDetectionImplWin::BuildTextDetectionResult(
if (FAILED(hr))
break;

// Gets bounding box with the words detected in the current line of Text.
Microsoft::WRL::ComPtr<IVectorView<OcrWord*>> ocr_words;
hr = line->get_Words(ocr_words.GetAddressOf());
if (FAILED(hr))
break;

uint32_t words_count;
hr = ocr_words->get_Size(&words_count);
if (FAILED(hr))
break;

auto result = shape_detection::mojom::TextDetectionResult::New();
for (uint32_t i = 0; i < words_count; ++i) {
Microsoft::WRL::ComPtr<IOcrWord> word;
hr = ocr_words->GetAt(i, &word);
if (FAILED(hr))
break;

ABI::Windows::Foundation::Rect bounds;
hr = word->get_BoundingRect(&bounds);
if (FAILED(hr))
break;

result->bounding_box = gfx::UnionRects(
result->bounding_box,
gfx::RectF(bounds.X, bounds.Y, bounds.Width, bounds.Height));
}

result->raw_value = ScopedHString(text).GetAsUTF8();
results.push_back(std::move(result));
}
Expand Down
2 changes: 2 additions & 0 deletions services/shape_detection/text_detection_impl_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ TEST_F(TextDetectionImplWinTest, ScanOnce) {
run_loop.Run();
ASSERT_EQ(2u, results.size());
EXPECT_EQ("The Chromium Project website is:", results[0]->raw_value);
EXPECT_EQ(gfx::RectF(51, 38, 272, 17), results[0]->bounding_box);
EXPECT_EQ("https://www.chromium.org", results[1]->raw_value);
EXPECT_EQ(gfx::RectF(51, 63, 209, 17), results[1]->bounding_box);
}

} // namespace shape_detection

0 comments on commit ecf2d44

Please sign in to comment.