Esempio n. 1
0
        private async Task SaveCode(Barcode barcode, Context context, DmtxDecoded result)
        {
            var code = Encoding.ASCII.GetString(result.Data).TrimEnd('\0');

            barcode.value = code;

            //Console.WriteLine(barcode.value);
            context.Barcodes.Add(barcode);
            PrintBarcode(barcode);
            await context.SaveChangesAsync();

            //lock (_lock)
            //{
            //    context.SaveChanges();
            //}

            //Здесь идет задержка 10-20 секунд
        }
Esempio n. 2
0
        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);
            });
        }
Esempio n. 3
0
        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.");
            }
        }
Esempio n. 4
0
        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.");
            }
        }