private List <string> DecodeImage(Bitmap image, int maxResultCount, TimeSpan timeOut, bool isMosaic) { List <string> result = new List <string>(); int stride; byte[] rawImg = ImageToByteArray(image, out stride); DmtxImage dmtxImg = new DmtxImage(rawImg, image.Width, image.Height, DmtxPackOrder.DmtxPack24bppRGB); dmtxImg.RowPadBytes = stride % 3; DmtxDecode decode = new DmtxDecode(dmtxImg, 1); TimeSpan timeLeft = new TimeSpan(timeOut.Ticks); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); while (true) { if (stopWatch.Elapsed > timeOut) { break; } DmtxRegion region = decode.RegionFindNext(timeOut); if (region != null) { DmtxMessage msg = isMosaic ? decode.MosaicRegion(region, -1) : decode.MatrixRegion(region, -1); string message = Encoding.ASCII.GetString(msg.Output); message = message.Substring(0, message.IndexOf('\0')); if (!result.Contains(message)) { result.Add(message); if (result.Count >= maxResultCount) { break; } } } else { break; } } return(result); }
internal static void GenReedSolEcc(DmtxMessage message, DmtxSymbolSize sizeIdx) { byte[] g = new byte[69]; byte[] b = new byte[68]; int bIndex = 0;; int symbolDataWords = GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribSymbolDataWords, sizeIdx); int symbolErrorWords = GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribSymbolErrorWords, sizeIdx); int symbolTotalWords = symbolDataWords + symbolErrorWords; int blockErrorWords = GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribBlockErrorWords, sizeIdx); int step = GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribInterleavedBlocks, sizeIdx); if (blockErrorWords != symbolErrorWords / step) { throw new Exception("Error generation reed solomon error correction"); } for (int gI = 0; gI < g.Length; gI++) { g[gI] = 0x01; } // Generate ECC polynomia for (int i = 1; i <= blockErrorWords; i++) { for (int j = i - 1; j >= 0; j--) { g[j] = GfDoublify(g[j], i); // g[j] *= 2**i if (j > 0) { g[j] = GfSum(g[j], g[j - 1]); // g[j] += g[j-1] } } } // Populate error codeword array for (int block = 0; block < step; block++) { for (int bI = 0; bI < b.Length; bI++) { b[bI] = 0; } for (int i = block; i < symbolDataWords; i += step) { int val = GfSum(b[blockErrorWords - 1], message.Code[i]); for (int j = blockErrorWords - 1; j > 0; j--) { b[j] = GfSum(b[j - 1], GfProduct(g[j], val)); } b[0] = GfProduct(g[0], val); } int blockDataWords = GetBlockDataSize(sizeIdx, block); bIndex = blockErrorWords; for (int i = block + (step * blockDataWords); i < symbolTotalWords; i += step) { message.Code[i] = b[--bIndex]; } if (bIndex != 0) { throw new Exception("Error generation error correction code!"); } } }