// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; using System.Drawing; namespace Spludlow.Drawing { /// /// Wrapper for ZXing.net /// https://github.com/micjahn/ZXing.Net /// to allow the Spludlow Drawing system to use for barcodes /// public class ZXings : Spludlow.Drawing.IBarCode { private static List FlatternList; static ZXings() { string[] flatFormats = new string[] { "CODABAR", "CODE_39", "CODE_93", "CODE_128", "EAN_8", "EAN_13", "ITF", "UPC_A", "UPC_E", "MSI", "PLESSEY", }; FlatternList = new List(); foreach (string format in flatFormats) FlatternList.Add(ParseBarcodeFormat(format)); } private ZXing.BarcodeFormat Format; public ZXings(string format) { this.Format = ParseBarcodeFormat(format); } /// /// This is the only method required /// public bool[][] Encode(string data) { ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter(); writer.Format = this.Format; ZXing.Common.BitMatrix matrix = writer.Encode(data); int height = matrix.Height; if (FlatternList.Contains(this.Format) == true) height = 1; bool[][] result = new bool[height][]; for (int y = 0; y < height; ++y) { result[y] = new bool[matrix.Width]; ZXing.Common.BitArray rowArray = matrix.getRow(y, null); for (int x = 0; x < matrix.Width; ++x) result[y][x] = rowArray[x]; } return result; } public static string[] BarcodeFormats() { return Enum.GetNames(typeof(ZXing.BarcodeFormat)); } public static ZXing.BarcodeFormat ParseBarcodeFormat(string format) { return (ZXing.BarcodeFormat)Enum.Parse(typeof(ZXing.BarcodeFormat), format); } public static string Decode(string filename) { ZXing.BarcodeReader reader = new ZXing.BarcodeReader(); reader.AutoRotate = true; //ZXing.Common.DecodingOptions options = new ZXing.Common.DecodingOptions(); //options.PossibleFormats = new List(new ZXing.BarcodeFormat[] { ZXing.BarcodeFormat.CODABAR }); make faster if known ? using (Bitmap bitmp = new Bitmap(filename)) { ZXing.Result result = reader.Decode(bitmp); if (result == null) return null; return result.Text; } } public static void DecodeTest(string directoryOfBitmaps) { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Filename Result Error", "String String String", }); foreach (string filename in Directory.GetFiles(directoryOfBitmaps)) { DataRow row = table.Rows.Add(filename, "", ""); try { row["Result"] = Decode(filename); } catch (Exception ee) { row["Error"] = ee.Message; } } Spludlow.Log.Report("Barcode Decode", table); } } }