Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/wrappers/dotnet/ZXingCpp/ZXingCpp.cs @ 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 2024 Axel Waggershauser | |
| 3 */ | |
| 4 // SPDX-License-Identifier: Apache-2.0 | |
| 5 | |
| 6 namespace ZXingCpp { | |
| 7 | |
| 8 using System; | |
| 9 #if NETSTANDARD | |
| 10 using System.Text; | |
| 11 #endif | |
| 12 using System.Runtime.InteropServices; | |
| 13 using System.Collections.Generic; | |
| 14 | |
| 15 using static Dll; | |
| 16 using BarcodeFormat = BarcodeFormats; | |
| 17 | |
| 18 internal class Dll | |
| 19 { | |
| 20 private const string DllName = "ZXing"; | |
| 21 | |
| 22 [DllImport(DllName)] public static extern IntPtr ZXing_ReaderOptions_new(); | |
| 23 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_delete(IntPtr opts); | |
| 24 | |
| 25 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setTryHarder(IntPtr opts, bool tryHarder); | |
| 26 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getTryHarder(IntPtr opts); | |
| 27 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setTryRotate(IntPtr opts, bool tryRotate); | |
| 28 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getTryRotate(IntPtr opts); | |
| 29 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setTryInvert(IntPtr opts, bool tryInvert); | |
| 30 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getTryInvert(IntPtr opts); | |
| 31 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setTryDownscale(IntPtr opts, bool tryDownscale); | |
| 32 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getTryDownscale(IntPtr opts); | |
| 33 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getIsPure(IntPtr opts); | |
| 34 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setIsPure(IntPtr opts, bool isPure); | |
| 35 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setReturnErrors(IntPtr opts, bool returnErrors); | |
| 36 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_ReaderOptions_getReturnErrors(IntPtr opts); | |
| 37 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setFormats(IntPtr opts, BarcodeFormats formats); | |
| 38 [DllImport(DllName)] public static extern BarcodeFormats ZXing_ReaderOptions_getFormats(IntPtr opts); | |
| 39 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setBinarizer(IntPtr opts, Binarizer binarizer); | |
| 40 [DllImport(DllName)] public static extern Binarizer ZXing_ReaderOptions_getBinarizer(IntPtr opts); | |
| 41 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setEanAddOnSymbol(IntPtr opts, EanAddOnSymbol eanAddOnSymbol); | |
| 42 [DllImport(DllName)] public static extern EanAddOnSymbol ZXing_ReaderOptions_getEanAddOnSymbol(IntPtr opts); | |
| 43 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setTextMode(IntPtr opts, TextMode textMode); | |
| 44 [DllImport(DllName)] public static extern TextMode ZXing_ReaderOptions_getTextMode(IntPtr opts); | |
| 45 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setMinLineCount(IntPtr opts, int n); | |
| 46 [DllImport(DllName)] public static extern int ZXing_ReaderOptions_getMinLineCount(IntPtr opts); | |
| 47 [DllImport(DllName)] public static extern void ZXing_ReaderOptions_setMaxNumberOfSymbols(IntPtr opts, int n); | |
| 48 [DllImport(DllName)] public static extern int ZXing_ReaderOptions_getMaxNumberOfSymbols(IntPtr opts); | |
| 49 | |
| 50 [DllImport(DllName)] public static extern IntPtr ZXing_PositionToString(Position position); | |
| 51 [DllImport(DllName)] public static extern BarcodeFormat ZXing_BarcodeFormatFromString(string str); | |
| 52 [DllImport(DllName)] public static extern BarcodeFormats ZXing_BarcodeFormatsFromString(string str); | |
| 53 | |
| 54 [DllImport(DllName)] public static extern IntPtr ZXing_ImageView_new(IntPtr data, int width, int height, ImageFormat format, int rowStride, int pixStride); | |
| 55 [DllImport(DllName)] public static extern IntPtr ZXing_ImageView_new_checked(byte[] data, int size, int width, int height, ImageFormat format, int rowStride, int pixStride); | |
| 56 [DllImport(DllName)] public static extern void ZXing_ImageView_delete(IntPtr iv); | |
| 57 | |
| 58 [DllImport(DllName)] public static extern void ZXing_Image_delete(IntPtr img); | |
| 59 [DllImport(DllName)] public static extern IntPtr ZXing_Image_data(IntPtr img); | |
| 60 [DllImport(DllName)] public static extern int ZXing_Image_width(IntPtr img); | |
| 61 [DllImport(DllName)] public static extern int ZXing_Image_height(IntPtr img); | |
| 62 [DllImport(DllName)] public static extern ImageFormat ZXing_Image_format(IntPtr img); | |
| 63 | |
| 64 [DllImport(DllName)] public static extern IntPtr ZXing_ReadBarcodes(IntPtr iv, IntPtr opts); | |
| 65 [DllImport(DllName)] public static extern void ZXing_Barcode_delete(IntPtr barcode); | |
| 66 [DllImport(DllName)] public static extern void ZXing_Barcodes_delete(IntPtr barcodes); | |
| 67 [DllImport(DllName)] public static extern int ZXing_Barcodes_size(IntPtr barcodes); | |
| 68 [DllImport(DllName)] public static extern IntPtr ZXing_Barcodes_move(IntPtr barcodes, int i); | |
| 69 | |
| 70 [DllImport(DllName)] public static extern IntPtr ZXing_CreatorOptions_new(BarcodeFormat format); | |
| 71 [DllImport(DllName)] public static extern void ZXing_CreatorOptions_delete(IntPtr opts); | |
| 72 [DllImport(DllName)] public static extern void ZXing_CreatorOptions_setFormat(IntPtr opts, BarcodeFormat format); | |
| 73 [DllImport(DllName)] public static extern BarcodeFormat ZXing_CreatorOptions_getFormat(IntPtr opts); | |
| 74 [DllImport(DllName)] public static extern void ZXing_CreatorOptions_setReaderInit(IntPtr opts, bool readerInit); | |
| 75 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_CreatorOptions_getReaderInit(IntPtr opts); | |
| 76 [DllImport(DllName)] public static extern void ZXing_CreatorOptions_setForceSquareDataMatrix(IntPtr opts, bool forceSquareDataMatrix); | |
| 77 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_CreatorOptions_getForceSquareDataMatrix(IntPtr opts); | |
| 78 [DllImport(DllName)] public static extern void ZXing_CreatorOptions_setEcLevel(IntPtr opts, string ecLevel); | |
| 79 [DllImport(DllName)] public static extern IntPtr ZXing_CreatorOptions_getEcLevel(IntPtr opts); | |
| 80 | |
| 81 [DllImport(DllName)] public static extern IntPtr ZXing_WriterOptions_new(); | |
| 82 [DllImport(DllName)] public static extern void ZXing_WriterOptions_delete(IntPtr opts); | |
| 83 [DllImport(DllName)] public static extern void ZXing_WriterOptions_setScale(IntPtr opts, int scale); | |
| 84 [DllImport(DllName)] public static extern int ZXing_WriterOptions_getScale(IntPtr opts); | |
| 85 [DllImport(DllName)] public static extern void ZXing_WriterOptions_setSizeHint(IntPtr opts, int sizeHint); | |
| 86 [DllImport(DllName)] public static extern int ZXing_WriterOptions_getSizeHint(IntPtr opts); | |
| 87 [DllImport(DllName)] public static extern void ZXing_WriterOptions_setRotate(IntPtr opts, int rotate); | |
| 88 [DllImport(DllName)] public static extern int ZXing_WriterOptions_getRotate(IntPtr opts); | |
| 89 [DllImport(DllName)] public static extern void ZXing_WriterOptions_setWithHRT(IntPtr opts, bool withHRT); | |
| 90 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_WriterOptions_getWithHRT(IntPtr opts); | |
| 91 [DllImport(DllName)] public static extern void ZXing_WriterOptions_setWithQuietZones(IntPtr opts, bool withQuietZones); | |
| 92 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_WriterOptions_getWithQuietZones(IntPtr opts); | |
| 93 | |
| 94 [DllImport(DllName)] public static extern IntPtr ZXing_CreateBarcodeFromText(string data, int size, IntPtr opts); | |
| 95 [DllImport(DllName)] public static extern IntPtr ZXing_CreateBarcodeFromBytes(byte[] data, int size, IntPtr opts); | |
| 96 | |
| 97 [DllImport(DllName)] public static extern IntPtr ZXing_WriteBarcodeToSVG(IntPtr barcode, IntPtr opts); | |
| 98 [DllImport(DllName)] public static extern IntPtr ZXing_WriteBarcodeToImage(IntPtr barcode, IntPtr opts); | |
| 99 | |
| 100 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_Barcode_isValid(IntPtr barcode); | |
| 101 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_errorMsg(IntPtr barcode); | |
| 102 [DllImport(DllName)] public static extern ErrorType ZXing_Barcode_errorType(IntPtr barcode); | |
| 103 [DllImport(DllName)] public static extern BarcodeFormat ZXing_Barcode_format(IntPtr barcode); | |
| 104 [DllImport(DllName)] public static extern ContentType ZXing_Barcode_contentType(IntPtr barcode); | |
| 105 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_bytes(IntPtr barcode, out int len); | |
| 106 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_bytesECI(IntPtr barcode, out int len); | |
| 107 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_text(IntPtr barcode); | |
| 108 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_ecLevel(IntPtr barcode); | |
| 109 [DllImport(DllName)] public static extern IntPtr ZXing_Barcode_symbologyIdentifier(IntPtr barcode); | |
| 110 [DllImport(DllName)] public static extern Position ZXing_Barcode_position(IntPtr barcode); | |
| 111 [DllImport(DllName)] public static extern int ZXing_Barcode_orientation(IntPtr barcode); | |
| 112 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_Barcode_hasECI(IntPtr barcode); | |
| 113 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_Barcode_isInverted(IntPtr barcode); | |
| 114 [DllImport(DllName)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool ZXing_Barcode_isMirrored(IntPtr barcode); | |
| 115 [DllImport(DllName)] public static extern int ZXing_Barcode_lineCount(IntPtr barcode); | |
| 116 | |
| 117 [DllImport(DllName)] public static extern void ZXing_free(IntPtr opts); | |
| 118 [DllImport(DllName)] public static extern IntPtr ZXing_LastErrorMsg(); | |
| 119 | |
| 120 | |
| 121 public static IntPtr CheckError(IntPtr ptr, string? msg = null) | |
| 122 { | |
| 123 if (ptr == IntPtr.Zero) | |
| 124 throw new Exception(msg ?? MarshalAsString(ZXing_LastErrorMsg())); | |
| 125 return ptr; | |
| 126 } | |
| 127 | |
| 128 public static string MarshalAsString(IntPtr ptr) | |
| 129 { | |
| 130 ptr = CheckError(ptr, "ZXing C-API returned a NULL char*."); | |
| 131 #if NET | |
| 132 string res = Marshal.PtrToStringUTF8(ptr) ?? ""; | |
| 133 #else | |
| 134 string res; | |
| 135 unsafe | |
| 136 { | |
| 137 int length = 0; | |
| 138 for (byte* i = (byte*)ptr; *i != 0; i++, length++); | |
| 139 res = Encoding.UTF8.GetString((byte*)ptr, length); | |
| 140 } | |
| 141 #endif | |
| 142 ZXing_free(ptr); | |
| 143 return res; | |
| 144 } | |
| 145 | |
| 146 public delegate IntPtr RetBytesFunc(IntPtr ptr, out int len); | |
| 147 | |
| 148 public static byte[] MarshalAsBytes(RetBytesFunc func, IntPtr d) | |
| 149 { | |
| 150 IntPtr ptr = CheckError(func(d, out int len), "ZXing C-API returned a NULL byte*."); | |
| 151 byte[] res = new byte[len]; | |
| 152 Marshal.Copy(ptr, res, 0, len); | |
| 153 ZXing_free(ptr); | |
| 154 return res; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 [Flags] | |
| 159 public enum BarcodeFormats | |
| 160 { | |
| 161 None = 0, ///< Used as a return value if no valid barcode has been detected | |
| 162 Aztec = (1 << 0), ///< Aztec | |
| 163 Codabar = (1 << 1), ///< Codabar | |
| 164 Code39 = (1 << 2), ///< Code39 | |
| 165 Code93 = (1 << 3), ///< Code93 | |
| 166 Code128 = (1 << 4), ///< Code128 | |
| 167 DataBar = (1 << 5), ///< GS1 DataBar, formerly known as RSS 14 | |
| 168 DataBarExpanded = (1 << 6), ///< GS1 DataBar Expanded, formerly known as RSS EXPANDED | |
| 169 DataMatrix = (1 << 7), ///< DataMatrix | |
| 170 EAN8 = (1 << 8), ///< EAN-8 | |
| 171 EAN13 = (1 << 9), ///< EAN-13 | |
| 172 ITF = (1 << 10), ///< ITF (Interleaved Two of Five) | |
| 173 MaxiCode = (1 << 11), ///< MaxiCode | |
| 174 PDF417 = (1 << 12), ///< PDF417 | |
| 175 QRCode = (1 << 13), ///< QR Code | |
| 176 UPCA = (1 << 14), ///< UPC-A | |
| 177 UPCE = (1 << 15), ///< UPC-E | |
| 178 MicroQRCode = (1 << 16), ///< Micro QR Code | |
| 179 RMQRCode = (1 << 17), ///< Rectangular Micro QR Code | |
| 180 DXFilmEdge = (1 << 18), ///< DX Film Edge Barcode | |
| 181 DataBarLimited = (1 << 19), ///< GS1 DataBar Limited | |
| 182 | |
| 183 LinearCodes = Codabar | Code39 | Code93 | Code128 | EAN8 | EAN13 | ITF | DataBar | DataBarExpanded | DataBarLimited | DXFilmEdge | UPCA | UPCE, | |
| 184 MatrixCodes = Aztec | DataMatrix | MaxiCode | PDF417 | QRCode | MicroQRCode | RMQRCode, | |
| 185 Any = LinearCodes | MatrixCodes, | |
| 186 }; | |
| 187 | |
| 188 | |
| 189 public enum Binarizer | |
| 190 { | |
| 191 LocalAverage, ///< T = average of neighboring pixels for matrix and GlobalHistogram for linear (HybridBinarizer) | |
| 192 GlobalHistogram, ///< T = valley between the 2 largest peaks in the histogram (per line in linear case) | |
| 193 FixedThreshold, ///< T = 127 | |
| 194 BoolCast, ///< T = 0, fastest possible | |
| 195 }; | |
| 196 | |
| 197 public enum EanAddOnSymbol | |
| 198 { | |
| 199 Ignore, ///< Ignore any Add-On symbol during read/scan | |
| 200 Read, ///< Read EAN-2/EAN-5 Add-On symbol if found | |
| 201 Require, ///< Require EAN-2/EAN-5 Add-On symbol to be present | |
| 202 }; | |
| 203 | |
| 204 public enum TextMode | |
| 205 { | |
| 206 Plain, ///< bytes() transcoded to unicode based on ECI info or guessed charset (the default mode prior to 2.0) | |
| 207 ECI, ///< standard content following the ECI protocol with every character set ECI segment transcoded to unicode | |
| 208 HRI, ///< Human Readable Interpretation (dependent on the ContentType) | |
| 209 Hex, ///< bytes() transcoded to ASCII string of HEX values | |
| 210 Escaped, ///< Use the EscapeNonGraphical() function (e.g. ASCII 29 will be transcoded to "<GS>") | |
| 211 }; | |
| 212 | |
| 213 public enum ContentType { Text, Binary, Mixed, GS1, ISO15434, UnknownECI }; | |
| 214 | |
| 215 public enum ErrorType { None, Format, Checksum, Unsupported }; | |
| 216 | |
| 217 public enum ImageFormat { | |
| 218 None = 0, | |
| 219 Lum = 0x01000000, | |
| 220 LumA = 0x02000000, | |
| 221 RGB = 0x03000102, | |
| 222 BGR = 0x03020100, | |
| 223 RGBA = 0x04000102, | |
| 224 ARGB = 0x04010203, | |
| 225 BGRA = 0x04020100, | |
| 226 ABGR = 0x04030201, | |
| 227 }; | |
| 228 | |
| 229 public struct PointI | |
| 230 { | |
| 231 public int X, Y; | |
| 232 }; | |
| 233 | |
| 234 public struct Position | |
| 235 { | |
| 236 public PointI TopLeft, TopRight, BottomRight, BottomLeft; | |
| 237 | |
| 238 public override string ToString() => MarshalAsString(ZXing_PositionToString(this)); | |
| 239 }; | |
| 240 | |
| 241 public class ImageView | |
| 242 { | |
| 243 internal IntPtr _d; | |
| 244 | |
| 245 public ImageView(byte[] data, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0) | |
| 246 => _d = CheckError(ZXing_ImageView_new_checked(data, data.Length, width, height, format, rowStride, pixStride)); | |
| 247 | |
| 248 public ImageView(IntPtr data, int width, int height, ImageFormat format, int rowStride = 0, int pixStride = 0) | |
| 249 => _d = CheckError(ZXing_ImageView_new(data, width, height, format, rowStride, pixStride)); | |
| 250 | |
| 251 ~ImageView() => ZXing_ImageView_delete(_d); | |
| 252 } | |
| 253 | |
| 254 public class Image : IDisposable | |
| 255 { | |
| 256 internal IntPtr _d; | |
| 257 | |
| 258 internal Image(IntPtr d) => _d = d; | |
| 259 | |
| 260 ~Image() => Dispose(); | |
| 261 | |
| 262 public void Dispose() | |
| 263 { | |
| 264 ZXing_Image_delete(_d); | |
| 265 _d = IntPtr.Zero; | |
| 266 GC.SuppressFinalize(this); | |
| 267 } | |
| 268 | |
| 269 public IntPtr Data => ZXing_Image_data(_d); | |
| 270 public int Width => ZXing_Image_width(_d); | |
| 271 public int Height => ZXing_Image_height(_d); | |
| 272 public ImageFormat Format => ZXing_Image_format(_d); | |
| 273 | |
| 274 public byte[] ToArray() | |
| 275 { | |
| 276 IntPtr ptr = ZXing_Image_data(_d); | |
| 277 if (ptr == IntPtr.Zero) | |
| 278 return new byte[0]; | |
| 279 | |
| 280 int len = Width * Height; | |
| 281 byte[] res = new byte[len]; | |
| 282 Marshal.Copy(ptr, res, 0, len); | |
| 283 return res; | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 public class ReaderOptions | |
| 288 { | |
| 289 internal IntPtr _d; | |
| 290 | |
| 291 public ReaderOptions() => _d = CheckError(ZXing_ReaderOptions_new(), "Failed to create ReaderOptions."); | |
| 292 | |
| 293 ~ReaderOptions() => ZXing_ReaderOptions_delete(_d); | |
| 294 | |
| 295 public bool TryHarder | |
| 296 { | |
| 297 get => ZXing_ReaderOptions_getTryHarder(_d); | |
| 298 set => ZXing_ReaderOptions_setTryHarder(_d, value); | |
| 299 } | |
| 300 | |
| 301 public bool TryRotate | |
| 302 { | |
| 303 get => ZXing_ReaderOptions_getTryRotate(_d); | |
| 304 set => ZXing_ReaderOptions_setTryRotate(_d, value); | |
| 305 } | |
| 306 | |
| 307 public bool TryInvert | |
| 308 { | |
| 309 get => ZXing_ReaderOptions_getTryInvert(_d); | |
| 310 set => ZXing_ReaderOptions_setTryInvert(_d, value); | |
| 311 } | |
| 312 | |
| 313 public bool TryDownscale | |
| 314 { | |
| 315 get => ZXing_ReaderOptions_getTryDownscale(_d); | |
| 316 set => ZXing_ReaderOptions_setTryDownscale(_d, value); | |
| 317 } | |
| 318 | |
| 319 public bool IsPure | |
| 320 { | |
| 321 get => ZXing_ReaderOptions_getIsPure(_d); | |
| 322 set => ZXing_ReaderOptions_setIsPure(_d, value); | |
| 323 } | |
| 324 | |
| 325 public bool ReturnErrors | |
| 326 { | |
| 327 get => ZXing_ReaderOptions_getReturnErrors(_d); | |
| 328 set => ZXing_ReaderOptions_setReturnErrors(_d, value); | |
| 329 } | |
| 330 | |
| 331 public BarcodeFormats Formats | |
| 332 { | |
| 333 get => ZXing_ReaderOptions_getFormats(_d); | |
| 334 set => ZXing_ReaderOptions_setFormats(_d, value); | |
| 335 } | |
| 336 | |
| 337 public Binarizer Binarizer | |
| 338 { | |
| 339 get => ZXing_ReaderOptions_getBinarizer(_d); | |
| 340 set => ZXing_ReaderOptions_setBinarizer(_d, value); | |
| 341 } | |
| 342 | |
| 343 public EanAddOnSymbol EanAddOnSymbol | |
| 344 { | |
| 345 get => ZXing_ReaderOptions_getEanAddOnSymbol(_d); | |
| 346 set => ZXing_ReaderOptions_setEanAddOnSymbol(_d, value); | |
| 347 } | |
| 348 | |
| 349 public TextMode TextMode | |
| 350 { | |
| 351 get => ZXing_ReaderOptions_getTextMode(_d); | |
| 352 set => ZXing_ReaderOptions_setTextMode(_d, value); | |
| 353 } | |
| 354 | |
| 355 public int MinLineCount | |
| 356 { | |
| 357 get => ZXing_ReaderOptions_getMinLineCount(_d); | |
| 358 set => ZXing_ReaderOptions_setMinLineCount(_d, value); | |
| 359 } | |
| 360 | |
| 361 public int MaxNumberOfSymbols | |
| 362 { | |
| 363 get => ZXing_ReaderOptions_getMaxNumberOfSymbols(_d); | |
| 364 set => ZXing_ReaderOptions_setMaxNumberOfSymbols(_d, value); | |
| 365 } | |
| 366 | |
| 367 } | |
| 368 | |
| 369 public class CreatorOptions | |
| 370 { | |
| 371 internal IntPtr _d; | |
| 372 | |
| 373 public CreatorOptions(BarcodeFormat format) | |
| 374 => _d = CheckError(ZXing_CreatorOptions_new(format), "Failed to create CreatorOptions."); | |
| 375 | |
| 376 public static implicit operator CreatorOptions(BarcodeFormat f) => new CreatorOptions(f); | |
| 377 | |
| 378 ~CreatorOptions() => ZXing_CreatorOptions_delete(_d); | |
| 379 | |
| 380 public bool ReaderInit | |
| 381 { | |
| 382 get => ZXing_CreatorOptions_getReaderInit(_d); | |
| 383 set => ZXing_CreatorOptions_setReaderInit(_d, value); | |
| 384 } | |
| 385 | |
| 386 public bool ForceSquareDataMatrix | |
| 387 { | |
| 388 get => ZXing_CreatorOptions_getForceSquareDataMatrix(_d); | |
| 389 set => ZXing_CreatorOptions_setForceSquareDataMatrix(_d, value); | |
| 390 } | |
| 391 | |
| 392 public String ECLevel | |
| 393 { | |
| 394 get => MarshalAsString(ZXing_CreatorOptions_getEcLevel(_d)); | |
| 395 set => ZXing_CreatorOptions_setEcLevel(_d, value); | |
| 396 } | |
| 397 | |
| 398 } | |
| 399 | |
| 400 public class WriterOptions | |
| 401 { | |
| 402 internal IntPtr _d; | |
| 403 | |
| 404 public WriterOptions() => _d = CheckError(ZXing_WriterOptions_new(), "Failed to create WriterOptions."); | |
| 405 | |
| 406 ~WriterOptions() => ZXing_WriterOptions_delete(_d); | |
| 407 | |
| 408 public int Scale | |
| 409 { | |
| 410 get => ZXing_WriterOptions_getScale(_d); | |
| 411 set => ZXing_WriterOptions_setScale(_d, value); | |
| 412 } | |
| 413 | |
| 414 public int SizeHint | |
| 415 { | |
| 416 get => ZXing_WriterOptions_getSizeHint(_d); | |
| 417 set => ZXing_WriterOptions_setSizeHint(_d, value); | |
| 418 } | |
| 419 | |
| 420 public int Rotate | |
| 421 { | |
| 422 get => ZXing_WriterOptions_getRotate(_d); | |
| 423 set => ZXing_WriterOptions_setRotate(_d, value); | |
| 424 } | |
| 425 | |
| 426 public bool WithHRT | |
| 427 { | |
| 428 get => ZXing_WriterOptions_getWithHRT(_d); | |
| 429 set => ZXing_WriterOptions_setWithHRT(_d, value); | |
| 430 } | |
| 431 | |
| 432 public bool WithQuietZones | |
| 433 { | |
| 434 get => ZXing_WriterOptions_getWithQuietZones(_d); | |
| 435 set => ZXing_WriterOptions_setWithQuietZones(_d, value); | |
| 436 } | |
| 437 } | |
| 438 | |
| 439 public class Barcode | |
| 440 { | |
| 441 internal IntPtr _d; | |
| 442 | |
| 443 internal Barcode(IntPtr d) => _d = d; | |
| 444 | |
| 445 ~Barcode() => ZXing_Barcode_delete(_d); | |
| 446 | |
| 447 public Barcode(string data, CreatorOptions opts) | |
| 448 => _d = CheckError(ZXing_CreateBarcodeFromText(data, data.Length, opts._d)); | |
| 449 | |
| 450 public Barcode(byte[] data, CreatorOptions opts) | |
| 451 => _d = CheckError(ZXing_CreateBarcodeFromBytes(data, data.Length, opts._d)); | |
| 452 | |
| 453 public bool IsValid => ZXing_Barcode_isValid(_d); | |
| 454 public BarcodeFormat Format => ZXing_Barcode_format(_d); | |
| 455 public ContentType ContentType => ZXing_Barcode_contentType(_d); | |
| 456 public string Text => MarshalAsString(ZXing_Barcode_text(_d)); | |
| 457 public byte[] Bytes => MarshalAsBytes(ZXing_Barcode_bytes, _d); | |
| 458 public byte[] BytesECI => MarshalAsBytes(ZXing_Barcode_bytesECI, _d); | |
| 459 public string ECLevel => MarshalAsString(ZXing_Barcode_ecLevel(_d)); | |
| 460 public string SymbologyIdentifier => MarshalAsString(ZXing_Barcode_symbologyIdentifier(_d)); | |
| 461 public string ErrorMsg => MarshalAsString(ZXing_Barcode_errorMsg(_d)); | |
| 462 public ErrorType ErrorType => ZXing_Barcode_errorType(_d); | |
| 463 public Position Position => ZXing_Barcode_position(_d); | |
| 464 public int Orientation => ZXing_Barcode_orientation(_d); | |
| 465 public bool HasECI => ZXing_Barcode_hasECI(_d); | |
| 466 public bool IsInverted => ZXing_Barcode_isInverted(_d); | |
| 467 public bool IsMirrored => ZXing_Barcode_isMirrored(_d); | |
| 468 public int LineCount => ZXing_Barcode_lineCount(_d); | |
| 469 | |
| 470 public string ToSVG(WriterOptions? opts = null) | |
| 471 => MarshalAsString(CheckError(ZXing_WriteBarcodeToSVG(_d, opts?._d ?? IntPtr.Zero))); | |
| 472 | |
| 473 public Image ToImage(WriterOptions? opts = null) | |
| 474 => new Image(CheckError(ZXing_WriteBarcodeToImage(_d, opts?._d ?? IntPtr.Zero))); | |
| 475 | |
| 476 public static BarcodeFormat FormatFromString(string str) | |
| 477 { | |
| 478 var res = ZXing_BarcodeFormatFromString(str); | |
| 479 if ((int)res == -1) // see ZXing_BarcodeFormat_Invalid | |
| 480 throw new Exception(MarshalAsString(ZXing_LastErrorMsg())); | |
| 481 return res; | |
| 482 } | |
| 483 | |
| 484 public static BarcodeFormats FormatsFromString(string str) | |
| 485 { | |
| 486 var res = ZXing_BarcodeFormatsFromString(str); | |
| 487 if ((int)res == -1) // see ZXing_BarcodeFormat_Invalid | |
| 488 throw new Exception(MarshalAsString(ZXing_LastErrorMsg())); | |
| 489 return res; | |
| 490 } | |
| 491 } | |
| 492 | |
| 493 public class BarcodeReader : ReaderOptions | |
| 494 { | |
| 495 public static Barcode[] Read(ImageView iv, ReaderOptions? opts = null) | |
| 496 { | |
| 497 var ptr = CheckError(ZXing_ReadBarcodes(iv._d, opts?._d ?? IntPtr.Zero)); | |
| 498 | |
| 499 // return static empty array if no Barcodes are found, avoiding any managed heap allocation | |
| 500 var res = Array.Empty<Barcode>(); | |
| 501 var size = ZXing_Barcodes_size(ptr); | |
| 502 if (size > 0) { | |
| 503 res = new Barcode[size]; | |
| 504 for (int i = 0; i < size; ++i) | |
| 505 res[i] = new Barcode(ZXing_Barcodes_move(ptr, i)); | |
| 506 } | |
| 507 | |
| 508 ZXing_Barcodes_delete(ptr); | |
| 509 | |
| 510 return res; | |
| 511 } | |
| 512 | |
| 513 public Barcode[] From(ImageView iv) => Read(iv, this); | |
| 514 } | |
| 515 | |
| 516 public class BarcodeCreator : CreatorOptions | |
| 517 { | |
| 518 public BarcodeCreator(BarcodeFormat format) : base(format) {} | |
| 519 | |
| 520 public Barcode From(string data) => new Barcode(data, this); | |
| 521 public Barcode From(byte[] data) => new Barcode(data, this); | |
| 522 } | |
| 523 | |
| 524 } |
