/// <summary> /// Decodes a bitmap returning all symbols found in the image. /// </summary> /// <param name="b">The bitmap to decode.</param> /// <param name="options">The options used for decoding.</param> /// <returns>An array of decoded symbols, one for each symbol found.</returns> /// <example> /// This example shows a basic decoding. /// <code> /// Bitmap bm = (Bitmap)Bitmap.FromFile("barcode.bmp"); /// DecodeOptions decodeOptions = new DecodeOptions(); /// DmtxDecoded[] decodeResults = Dmtx.Decode(bm, decodeOptions); /// for (int i = 0; i < decodeResults.Length; i++) { /// string str = Encoding.ASCII.GetString(decodeResults[i].Data).TrimEnd('\0'); /// Console.WriteLine("Decode " + i + ": \"" + str + "\""); /// } /// </code> /// </example> public static DmtxDecoded[] Decode(Bitmap b, DecodeOptions options) { List <DmtxDecoded> results = new List <DmtxDecoded>(); Decode(b, options, delegate(DmtxDecoded d) { results.Add(d); }); return(results.ToArray()); }
DmtxDecode( [In] byte[] image, [In] UInt32 width, [In] UInt32 height, [In] UInt32 bitmapStride, [In] DecodeOptions options, [In] DmtxDiagnosticImageCallback diagnosticImageCallback, [In] DiagnosticImageStyles diagnosticImageStyle, [In] DmtxDecodeCallback decodeCallback);
public void TestDecode() { Bitmap bm = GetBitmapFromResource("Libdmtx.TestImages.Test001.png"); DecodeOptions opt = new DecodeOptions(); DmtxDecoded[] decodeResults = Dmtx.Decode(bm, opt); Assert.AreEqual(1, decodeResults.Length); string data = Encoding.ASCII.GetString(decodeResults[0].Data).TrimEnd('\0'); Assert.AreEqual("Test", data); }
private void button1_Click(object sender, EventArgs e) { //decoding // C# Example Libdmtx.DecodeOptions o = new Libdmtx.DecodeOptions(); Bitmap b = new Bitmap(@"bitmap.png"); Libdmtx.DmtxDecoded[] res = Dmtx.Decode(b, o); for (uint i = 0; i < res.Length; i++) { string str = Encoding.ASCII.GetString(res[i].Data).TrimEnd('\0'); Console.WriteLine("Code " + i + ": " + str); } }
public void TestDecodeWithCallback() { List <DmtxDecoded> decodeResults = new List <DmtxDecoded>(); Bitmap bm = GetBitmapFromResource("Libdmtx.TestImages.Test002.png"); DecodeOptions opt = new DecodeOptions(); Dmtx.Decode(bm, opt, d => decodeResults.Add(d)); Assert.AreEqual(2, decodeResults.Count); string data1 = Encoding.ASCII.GetString(decodeResults[0].Data).TrimEnd('\0'); Assert.AreEqual("Test1", data1); string data2 = Encoding.ASCII.GetString(decodeResults[1].Data).TrimEnd('\0'); Assert.AreEqual("Test2", data2); }
public void TestDecodeWithCallbackThrowException() { Bitmap bm = GetBitmapFromResource("Libdmtx.TestImages.Test002.png"); DecodeOptions opt = new DecodeOptions(); int callCount = 0; try { Dmtx.Decode(bm, opt, d => { callCount++; throw new Exception("Test Exception"); }); Assert.Fail("Should have gotten an exception."); } catch (Exception ex) { Assert.AreEqual(1, callCount); Assert.AreEqual("Test Exception", ex.Message); } }
public static DmtxDecoded[] Decode( Bitmap b, DecodeOptions options, DiagnosticImageStyles diagnosticImageStyle, out Bitmap diagnosticImage) { List <DmtxDecoded> results = new List <DmtxDecoded>(); Bitmap diagnosticImageTemp = null; Decode( b, options, delegate(DmtxDecoded d) { results.Add(d); }, diagnosticImageStyle, delegate(Bitmap di) { diagnosticImageTemp = di; }); diagnosticImage = diagnosticImageTemp; return(results.ToArray()); }
public void DecodeSingle(Bitmap bitmap, DecodeOptions options, Barcode barcode, Context context, AppDomain domain) { var pxl = BitmapToByteArray(bitmap, out int bitmapStride); DmtxDecode( pxl, (uint)bitmap.Width, (uint)bitmap.Height, (uint)bitmapStride, options, null, 0, async delegate(DecodedInternal dmtxDecodeResult) { var result = new DmtxDecoded { Data = new byte[dmtxDecodeResult.DataSize] }; var codeIsOk = false; try { for (int dataIdx = 0; dataIdx < dmtxDecodeResult.DataSize; dataIdx++) { result.Data[dataIdx] = Marshal.ReadByte(dmtxDecodeResult.Data, dataIdx); } codeIsOk = true; } catch { //TODO: Логирование ошибки нечитабельного кода на картинке } if (codeIsOk) { await SaveCode(barcode, context, result); } AppDomain.Unload(domain); }); }
public void TestStrideAndPadding() { EncodeOptions encodeOptions = new EncodeOptions { MarginSize = 2, ModuleSize = 2 }; DmtxEncoded encoded = Dmtx.Encode(Encoding.ASCII.GetBytes("t"), encodeOptions); Bitmap bm = encoded.Bitmap; // make sure we have an image who's stride is not divisable by 3 int stride; ExecuteBitmapToByteArray(bm, out stride); if (stride % 3 == 0) { bm = BitmapIncreaseCanvas(bm, bm.Width + 1, bm.Height, Color.White); ExecuteBitmapToByteArray(bm, out stride); } Assert.AreNotEqual(0, stride % 3, "Stride was divisable by 3 which doesn't make a very good test"); DecodeOptions opt = new DecodeOptions(); Bitmap diagnoseImage; DmtxDecoded[] decodedImages = Dmtx.Decode(bm, opt, DiagnosticImageStyles.Default, out diagnoseImage); //diagnoseImage.Save("c:/temp/diagnose.bmp", ImageFormat.Bmp); Assert.AreEqual(24.0, diagnoseImage.Width, 1.0); Assert.AreEqual(24.0, diagnoseImage.Height, 1.0); Assert.AreEqual(1, decodedImages.Length, "Didn't find barcode"); // make sure the left line is straight up and down (not skewed) for (int y = 4; y < diagnoseImage.Height - 4; y++) { Color clrLeft = diagnoseImage.GetPixel(1, y); Color clrRight = diagnoseImage.GetPixel(3, y); Assert.AreEqual(1.0, clrLeft.GetBrightness(), 0.01, "at location [1, " + y + "]"); Assert.AreEqual(0.698, clrRight.GetBrightness(), 0.01, "at location [3, " + y + "]"); } }
public static DmtxDecoded[] Decode( Bitmap b, DecodeOptions options, DiagnosticImageStyles diagnosticImageStyle, out Bitmap diagnosticImage) { List<DmtxDecoded> results = new List<DmtxDecoded>(); Bitmap diagnosticImageTemp = null; Decode( b, options, delegate(DmtxDecoded d) { results.Add(d); }, diagnosticImageStyle, delegate(Bitmap di) { diagnosticImageTemp = di; }); diagnosticImage = diagnosticImageTemp; return results.ToArray(); }
public static void Decode(Bitmap b, DecodeOptions options, DecodeCallback Callback) { Decode(b, options, Callback, 0, null); }
public static void Decode( Bitmap b, DecodeOptions options, DecodeCallback Callback, DiagnosticImageStyles diagnosticImageStyle, DecodeDiagnosticImageCallback DiagnosticImageCallback) { Exception decodeException = null; byte status; try { int bitmapStride; byte[] pxl = BitmapToByteArray(b, out bitmapStride); DmtxDiagnosticImageCallback diagnosticImageCallbackParam = null; if (DiagnosticImageCallback != null) { diagnosticImageCallbackParam = delegate(IntPtr data, uint totalBytes, uint headerBytes) { try { byte[] pnmData = new byte[totalBytes]; Marshal.Copy(data, pnmData, 0, pnmData.Length); using (MemoryStream pnmInputStream = new MemoryStream(pnmData)) { Bitmap bm = PnmToBitmap(pnmInputStream); DiagnosticImageCallback(bm); } } catch (Exception ex) { decodeException = ex; } }; } status = DmtxDecode( pxl, (UInt32)b.Width, (UInt32)b.Height, (UInt32)bitmapStride, options, diagnosticImageCallbackParam, diagnosticImageStyle, delegate(DecodedInternal dmtxDecodeResult) { DmtxDecoded result; try { result = new DmtxDecoded(); result.Corners = dmtxDecodeResult.Corners; result.SymbolInfo = dmtxDecodeResult.SymbolInfo; result.Data = new byte[dmtxDecodeResult.DataSize]; for (int dataIdx = 0; dataIdx < dmtxDecodeResult.DataSize; dataIdx++) { result.Data[dataIdx] = Marshal.ReadByte(dmtxDecodeResult.Data, dataIdx); } Callback(result); return true; } catch (Exception ex) { decodeException = ex; return false; } }); } catch (Exception ex) { throw new DmtxException("Error calling native function.", ex); } if (decodeException != null) { throw decodeException; } if (status == RETURN_NO_MEMORY) { throw new DmtxOutOfMemoryException("Not enough memory."); } else if (status == RETURN_INVALID_ARGUMENT) { throw new DmtxInvalidArgumentException("Invalid options configuration."); } else if (status > 0) { throw new DmtxException("Unknown error."); } }
public void TestDecodeWithCallback() { List<DmtxDecoded> decodeResults = new List<DmtxDecoded>(); Bitmap bm = GetBitmapFromResource("Libdmtx.TestImages.Test002.png"); DecodeOptions opt = new DecodeOptions(); Dmtx.Decode(bm, opt, d => decodeResults.Add(d)); Assert.AreEqual(2, decodeResults.Count); string data1 = Encoding.ASCII.GetString(decodeResults[0].Data).TrimEnd('\0'); Assert.AreEqual("Test1", data1); string data2 = Encoding.ASCII.GetString(decodeResults[1].Data).TrimEnd('\0'); Assert.AreEqual("Test2", data2); }
public static void Decode( Bitmap b, DecodeOptions options, DecodeCallback Callback, DiagnosticImageStyles diagnosticImageStyle, DecodeDiagnosticImageCallback DiagnosticImageCallback) { Exception decodeException = null; byte status; try { int bitmapStride; byte[] pxl = BitmapToByteArray(b, out bitmapStride); DmtxDiagnosticImageCallback diagnosticImageCallbackParam = null; if (DiagnosticImageCallback != null) { diagnosticImageCallbackParam = delegate(IntPtr data, uint totalBytes, uint headerBytes) { try { byte[] pnmData = new byte[totalBytes]; Marshal.Copy(data, pnmData, 0, pnmData.Length); using (MemoryStream pnmInputStream = new MemoryStream(pnmData)) { Bitmap bm = PnmToBitmap(pnmInputStream); DiagnosticImageCallback(bm); } } catch (Exception ex) { decodeException = ex; } }; } status = DmtxDecode( pxl, (UInt32)b.Width, (UInt32)b.Height, (UInt32)bitmapStride, options, diagnosticImageCallbackParam, diagnosticImageStyle, delegate(DecodedInternal dmtxDecodeResult) { DmtxDecoded result; try { result = new DmtxDecoded(); result.Corners = dmtxDecodeResult.Corners; result.SymbolInfo = dmtxDecodeResult.SymbolInfo; result.Data = new byte[dmtxDecodeResult.DataSize]; for (int dataIdx = 0; dataIdx < dmtxDecodeResult.DataSize; dataIdx++) { result.Data[dataIdx] = Marshal.ReadByte(dmtxDecodeResult.Data, dataIdx); } Callback(result); return(true); } catch (Exception ex) { decodeException = ex; return(false); } }); } catch (Exception ex) { throw new DmtxException("Error calling native function.", ex); } if (decodeException != null) { throw decodeException; } if (status == RETURN_NO_MEMORY) { throw new DmtxOutOfMemoryException("Not enough memory."); } else if (status == RETURN_INVALID_ARGUMENT) { throw new DmtxInvalidArgumentException("Invalid options configuration."); } else if (status > 0) { throw new DmtxException("Unknown error."); } }
/// <summary> /// Decodes a bitmap returning all symbols found in the image. /// </summary> /// <param name="b">The bitmap to decode.</param> /// <param name="options">The options used for decoding.</param> /// <returns>An array of decoded symbols, one for each symbol found.</returns> /// <example> /// This example shows a basic decoding. /// <code> /// Bitmap bm = (Bitmap)Bitmap.FromFile("barcode.bmp"); /// DecodeOptions decodeOptions = new DecodeOptions(); /// DmtxDecoded[] decodeResults = Dmtx.Decode(bm, decodeOptions); /// for (int i = 0; i < decodeResults.Length; i++) { /// string str = Encoding.ASCII.GetString(decodeResults[i].Data).TrimEnd('\0'); /// Console.WriteLine("Decode " + i + ": \"" + str + "\""); /// } /// </code> /// </example> public static DmtxDecoded[] Decode(Bitmap b, DecodeOptions options) { List<DmtxDecoded> results = new List<DmtxDecoded>(); Decode(b, options, delegate(DmtxDecoded d) { results.Add(d); }); return results.ToArray(); }