Esempio n. 1
0
        /// <summary>
        /// Used to find and exctract the barcode
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateExtractBarCodeDelegate2()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                Debug.Assert(state.Main != IntPtr.Zero);
                Debug.Assert(state.Scan != IntPtr.Zero);
                Debug.Assert(data != null);

                if (data.VoucherImage == null)
                {
                    throw new AppWarningException("No image found");
                }

                Guid g     = Guid.NewGuid();
                var buffer = data.VoucherImage.ToArray();
                ClientDataAccess.InsertBarcodeInfo(g, buffer);

                string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "ReaderProc.exe");
                var p       = Process.Start(path, g.ToString());

                if (!p.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds))
                {
                    throw new TimeoutException("Reader timeout");
                }

                var buffer2 = ClientDataAccess.SelectBarcodeInfoData(g);
                ClientDataAccess.DeleteBarcodeInfo(g);

                ObjectSerializer ser      = new ObjectSerializer(true);
                BarcodeInfoArray barArray = ser.Deserialize <BarcodeInfoArray>(buffer2);
                if (barArray.Count == 0)
                {
                    throw new IndexOutOfRangeException("No barcode found");
                }

                if (barArray == null || barArray.Count == 0)
                {
                    var ex = new ApplicationException("No bar code found");
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                    }));
                    throw ex;
                }
                else if (barArray.Count > 1)
                {
                    var ex = new ApplicationException("More than one bar code on image.");
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                    }));
                    throw ex;
                }

                try
                {
                    data.Parse(barArray[0].String);
                }
                catch (Exception e)
                {
                    var ex = new ApplicationException("Can't parse barcode string.", e);
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                        VoucherMonitorForm.ShowImage(id);
                    }));
                    throw ex;
                }
            });

            return(method);
        }
Esempio n. 2
0
        /// <summary>
        /// Used to find and exctract the barcode
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateExtractBarCodeDelegate()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                Debug.Assert(state.Main != IntPtr.Zero);
                Debug.Assert(state.Scan != IntPtr.Zero);
                Debug.Assert(data != null);

                if (data.VoucherImage == null)
                {
                    throw new AppWarningException("No image found");
                }

                Barcode[] readBarCodes = null;

                lock (typeof(BarcodeReader))
                {
                    BarcodeReader reader = new BarcodeReader(Strings.VScan_BarcodeReaderSDKDeveloperLicenseKey);
                    reader.LicenseManager.AddLicenseKey(Strings.VScan_BarcodeReaderSDKUnlimitedRuntimeLicenseKey);
                    reader.BarcodesToRead = 1;
                    reader.BarcodeTypes   = BarcodeTypeEnum.BT_Inter2of5;
                    var bmp           = data.VoucherImage;
                    readBarCodes      = reader.ReadFromBitmapRotateAll(ref bmp, data);
                    data.VoucherImage = bmp;
                }

                if (readBarCodes == null || readBarCodes.Length == 0)
                {
                    var ex = new ApplicationException("No bar code found");
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                    }));
                    throw ex;
                }
                else if (readBarCodes.Length > 1)
                {
                    var ex = new ApplicationException("More than one bar code on image.");
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                    }));
                    throw ex;
                }

                try
                {
                    data.Parse(readBarCodes[0].BarcodeString);
                }
                catch (Exception e)
                {
                    var ex = new ApplicationException("Can't process barcode string.", e);
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Scan, state.Main, id);
                        VoucherMonitorForm.ShowImage(id);
                    }));
                    throw ex;
                }
            });

            return(method);
        }