Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/wrappers/dotnet/ZXingCpp.DemoReader/Program.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 using System.Collections.Generic; | |
| 7 using ImageMagick; | |
| 8 using SkiaSharp; | |
| 9 using ZXingCpp; | |
| 10 | |
| 11 public static class MagickImageBarcodeReader | |
| 12 { | |
| 13 public static Barcode[] Read(MagickImage img, ReaderOptions? opts = null) | |
| 14 { | |
| 15 if (img.DetermineBitDepth() < 8) | |
| 16 img.SetBitDepth(8); | |
| 17 var bytes = img.ToByteArray(MagickFormat.Gray); | |
| 18 var iv = new ImageView(bytes, img.Width, img.Height, ImageFormat.Lum); | |
| 19 return BarcodeReader.Read(iv, opts); | |
| 20 } | |
| 21 | |
| 22 public static Barcode[] From(this BarcodeReader reader, MagickImage img) => Read(img, reader); | |
| 23 } | |
| 24 | |
| 25 public static class SkBitmapBarcodeReader | |
| 26 { | |
| 27 public static Barcode[] Read(SKBitmap img, ReaderOptions? opts = null) | |
| 28 { | |
| 29 var format = img.Info.ColorType switch | |
| 30 { | |
| 31 SKColorType.Gray8 => ImageFormat.Lum, | |
| 32 SKColorType.Rgba8888 => ImageFormat.RGBA, | |
| 33 SKColorType.Bgra8888 => ImageFormat.BGRA, | |
| 34 _ => ImageFormat.None, | |
| 35 }; | |
| 36 if (format == ImageFormat.None) | |
| 37 { | |
| 38 if (!img.CanCopyTo(SKColorType.Gray8)) | |
| 39 throw new Exception("Incompatible SKColorType"); | |
| 40 img = img.Copy(SKColorType.Gray8); | |
| 41 format = ImageFormat.Lum; | |
| 42 } | |
| 43 var iv = new ImageView(img.GetPixels(), img.Info.Width, img.Info.Height, format); | |
| 44 return BarcodeReader.Read(iv, opts); | |
| 45 } | |
| 46 | |
| 47 public static Barcode[] From(this BarcodeReader reader, SKBitmap img) => Read(img, reader); | |
| 48 } | |
| 49 | |
| 50 public class Program | |
| 51 { | |
| 52 public static void Main(string[] args) | |
| 53 { | |
| 54 #if false | |
| 55 var img = new MagickImage(args[0]); | |
| 56 #else | |
| 57 var img = SKBitmap.Decode(args[0]); | |
| 58 #endif | |
| 59 Console.WriteLine(img); | |
| 60 | |
| 61 var readBarcodes = new BarcodeReader() { | |
| 62 TryInvert = false, | |
| 63 ReturnErrors = true, | |
| 64 }; | |
| 65 | |
| 66 if (args.Length >= 2) | |
| 67 readBarcodes.Formats = Barcode.FormatsFromString(args[1]); | |
| 68 | |
| 69 foreach (var b in readBarcodes.From(img)) | |
| 70 Console.WriteLine($"{b.Format} ({b.ContentType}): {b.Text} / [{string.Join(", ", b.Bytes)}] {b.ErrorMsg}"); | |
| 71 } | |
| 72 } |
