comparison mupdf-source/thirdparty/zxing-cpp/wrappers/winrt/BarcodeReader.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Copyright 2016 Nu-book Inc.
3 */
4 // SPDX-License-Identifier: Apache-2.0
5
6 #if (_MSC_VER >= 1915)
7 #define no_init_all deprecated
8 #pragma warning(disable : 4996)
9 #endif
10
11 #include "BarcodeReader.h"
12
13 #include "BarcodeFormat.h"
14 #include "ReaderOptions.h"
15 #include "ReadBarcode.h"
16 #include "ReadResult.h"
17 #include "Utf.h"
18
19 #include <algorithm>
20 #include <MemoryBuffer.h>
21 #include <stdexcept>
22 #include <wrl.h>
23
24 using namespace Microsoft::WRL;
25 using namespace Windows::Foundation;
26 using namespace Windows::Graphics::Imaging;
27
28 namespace ZXing {
29
30 BarcodeReader::BarcodeReader(bool tryHarder, bool tryRotate, const Platform::Array<BarcodeType>^ types)
31 {
32 init(tryHarder, tryRotate, types);
33 }
34
35 BarcodeReader::BarcodeReader(bool tryHarder, bool tryRotate)
36 {
37 init(tryHarder, tryRotate, nullptr);
38 }
39
40 BarcodeReader::BarcodeReader(bool tryHarder)
41 {
42 init(tryHarder, tryHarder, nullptr);
43 }
44
45 void
46 BarcodeReader::init(bool tryHarder, bool tryRotate, const Platform::Array<BarcodeType>^ types)
47 {
48 m_opts.reset(new ReaderOptions());
49 m_opts->setTryHarder(tryHarder);
50 m_opts->setTryRotate(tryRotate);
51 m_opts->setTryInvert(tryHarder);
52
53 if (types != nullptr && types->Length > 0) {
54 BarcodeFormats barcodeFormats;
55 for (BarcodeType type : types) {
56 barcodeFormats |= BarcodeReader::ConvertRuntimeToNative(type);
57 }
58 m_opts->setFormats(barcodeFormats);
59 }
60 }
61
62 BarcodeReader::~BarcodeReader()
63 {
64 }
65
66 BarcodeFormat BarcodeReader::ConvertRuntimeToNative(BarcodeType type)
67 {
68 switch (type) {
69 case BarcodeType::AZTEC:
70 return BarcodeFormat::Aztec;
71 case BarcodeType::CODABAR:
72 return BarcodeFormat::Codabar;
73 case BarcodeType::CODE_128:
74 return BarcodeFormat::Code128;
75 case BarcodeType::CODE_39:
76 return BarcodeFormat::Code39;
77 case BarcodeType::CODE_93:
78 return BarcodeFormat::Code93;
79 case BarcodeType::DATA_MATRIX:
80 return BarcodeFormat::DataMatrix;
81 case BarcodeType::EAN_13:
82 return BarcodeFormat::EAN13;
83 case BarcodeType::EAN_8:
84 return BarcodeFormat::EAN8;
85 case BarcodeType::ITF:
86 return BarcodeFormat::ITF;
87 case BarcodeType::MAXICODE:
88 return BarcodeFormat::MaxiCode;
89 case BarcodeType::PDF_417:
90 return BarcodeFormat::PDF417;
91 case BarcodeType::QR_CODE:
92 return BarcodeFormat::QRCode;
93 case BarcodeType::MICRO_QR_CODE:
94 return BarcodeFormat::MicroQRCode;
95 case BarcodeType::RMQR_CODE:
96 return BarcodeFormat::RMQRCode;
97 case BarcodeType::RSS_14:
98 return BarcodeFormat::DataBar;
99 case BarcodeType::RSS_EXPANDED:
100 return BarcodeFormat::DataBarExpanded;
101 case BarcodeType::DATA_BAR_LIMITED:
102 return BarcodeFormat::DataBarLimited;
103 case BarcodeType::DX_FILM_EDGE:
104 return BarcodeFormat::DXFilmEdge;
105 case BarcodeType::UPC_A:
106 return BarcodeFormat::UPCA;
107 case BarcodeType::UPC_E:
108 return BarcodeFormat::UPCE;
109 default:
110 std::wstring typeAsString = type.ToString()->Begin();
111 throw std::invalid_argument("Unknown Barcode Type: " + ToUtf8(typeAsString));
112 }
113 }
114
115 BarcodeType BarcodeReader::ConvertNativeToRuntime(BarcodeFormat format)
116 {
117 switch (format) {
118 case BarcodeFormat::Aztec:
119 return BarcodeType::AZTEC;
120 case BarcodeFormat::Codabar:
121 return BarcodeType::CODABAR;
122 case BarcodeFormat::Code128:
123 return BarcodeType::CODE_128;
124 case BarcodeFormat::Code39:
125 return BarcodeType::CODE_39;
126 case BarcodeFormat::Code93:
127 return BarcodeType::CODE_93;
128 case BarcodeFormat::DataMatrix:
129 return BarcodeType::DATA_MATRIX;
130 case BarcodeFormat::EAN13:
131 return BarcodeType::EAN_13;
132 case BarcodeFormat::EAN8:
133 return BarcodeType::EAN_8;
134 case BarcodeFormat::ITF:
135 return BarcodeType::ITF;
136 case BarcodeFormat::MaxiCode:
137 return BarcodeType::MAXICODE;
138 case BarcodeFormat::PDF417:
139 return BarcodeType::PDF_417;
140 case BarcodeFormat::QRCode:
141 return BarcodeType::QR_CODE;
142 case BarcodeFormat::MicroQRCode:
143 return BarcodeType::MICRO_QR_CODE;
144 case BarcodeFormat::RMQRCode:
145 return BarcodeType::RMQR_CODE;
146 case BarcodeFormat::DataBar:
147 return BarcodeType::RSS_14;
148 case BarcodeFormat::DataBarExpanded:
149 return BarcodeType::RSS_EXPANDED;
150 case BarcodeFormat::DataBarLimited:
151 return BarcodeType::DATA_BAR_LIMITED;
152 case BarcodeFormat::DXFilmEdge:
153 return BarcodeType::DX_FILM_EDGE;
154 case BarcodeFormat::UPCA:
155 return BarcodeType::UPC_A;
156 case BarcodeFormat::UPCE:
157 return BarcodeType::UPC_E;
158 default:
159 throw std::invalid_argument("Unknown Barcode Format ");
160 }
161 }
162
163 static Platform::String^ ToPlatformString(const std::string& str)
164 {
165 std::wstring wstr = FromUtf8(str);
166 return ref new Platform::String(wstr.c_str(), (unsigned)wstr.length());
167 }
168
169 ReadResult^
170 BarcodeReader::Read(SoftwareBitmap^ bitmap, int cropWidth, int cropHeight)
171 {
172 try {
173 cropWidth = cropWidth <= 0 ? bitmap->PixelWidth : std::min(bitmap->PixelWidth, cropWidth);
174 cropHeight = cropHeight <= 0 ? bitmap->PixelHeight : std::min(bitmap->PixelHeight, cropHeight);
175 int cropLeft = (bitmap->PixelWidth - cropWidth) / 2;
176 int cropTop = (bitmap->PixelHeight - cropHeight) / 2;
177
178 auto inBuffer = bitmap->LockBuffer(BitmapBufferAccessMode::Read);
179 auto inMemRef = inBuffer->CreateReference();
180 ComPtr<IMemoryBufferByteAccess> inBufferAccess;
181
182 if (SUCCEEDED(ComPtr<IUnknown>(reinterpret_cast<IUnknown*>(inMemRef)).As(&inBufferAccess))) {
183 BYTE* inBytes = nullptr;
184 UINT32 inCapacity = 0;
185 inBufferAccess->GetBuffer(&inBytes, &inCapacity);
186
187 ImageFormat fmt = ImageFormat::None;
188 switch (bitmap->BitmapPixelFormat)
189 {
190 case BitmapPixelFormat::Gray8: fmt = ImageFormat::Lum; break;
191 case BitmapPixelFormat::Bgra8: fmt = ImageFormat::BGRA; break;
192 case BitmapPixelFormat::Rgba8: fmt = ImageFormat::RGBA; break;
193 default:
194 throw std::runtime_error("Unsupported BitmapPixelFormat");
195 }
196
197 auto img = ImageView(inBytes, bitmap->PixelWidth, bitmap->PixelHeight, fmt, inBuffer->GetPlaneDescription(0).Stride)
198 .cropped(cropLeft, cropTop, cropWidth, cropHeight);
199
200 auto barcode = ReadBarcode(img, *m_opts);
201 if (barcode.isValid()) {
202 return ref new ReadResult(ToPlatformString(ZXing::ToString(barcode.format())), ToPlatformString(barcode.text()), ConvertNativeToRuntime(barcode.format()));
203 }
204 } else {
205 throw std::runtime_error("Failed to read bitmap's data");
206 }
207 }
208 catch (const std::exception& e) {
209 OutputDebugStringA(e.what());
210 }
211 catch (...) {
212 }
213 return nullptr;
214 }
215
216 } // ZXing