Exemplo n.º 1
0
 /// <summary>
 /// Shows image on monitor if its open
 /// </summary>
 /// <param name="name"></param>
 public static void ShowImage(string name)
 {
     if (ms_This != IntPtr.Zero)
     {
         WinMsg.SendText(ms_This, name);
     }
 }
Exemplo n.º 2
0
 public static void Stop()
 {
     if (ms_This != IntPtr.Zero)
     {
         WinMsg.SendText(ms_This, Strings.VScan_DesktopUnlock);
     }
 }
Exemplo n.º 3
0
 public static void Stop()
 {
     if (ms_This != IntPtr.Zero)
     {
         WinMsg.SendText(ms_This, Strings.VScan_SinglentonFormClose);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Used to print over the voucher image
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreatePrintOnImageDelegate()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                var printLocation = SettingsTable.Get <Point>(Strings.VScan_PrintAreaLocation, Point.Empty);
                if (printLocation == Point.Empty)
                {
                    var ex = new ApplicationException("Cannot find 'print on image' configuration.");
                    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;
                }

                var aliases           = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
                aliases["[DATE]"]     = new Func <string>(() => DateTime.Now.ToString());
                aliases["[OPERATOR]"] = new Func <string>(() => SettingsTable.Get <UserAuth>(Strings.Transferring_AuthObject, UserAuth.Default).Name);
                aliases["[PCNAME]"]   = new Func <string>(() => PTFUtils.GetMachine());
                aliases["[SITEID]"]   = new Func <string>(() => data.SiteCode);
                aliases["[NUMBER]"]   = new Func <string>(() => Convert.ToString(SettingsTable.Get <int>(Strings.VScan_ScanCount, 0)));

                string voucherTemplate = SettingsTable.Get <string>(Strings.VScan_ImPrinterTemplate, Strings.VScan_ImPrinterTemplateDefault);
                string voucherText     = voucherTemplate.format(aliases);

                using (var g = Graphics.FromImage(data.VoucherImage))
                    g.DrawString(voucherText, SystemFonts.CaptionFont, Brushes.Red, printLocation);
            });

            return(method);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Used to find and cover the card code by a mark on the voucher image
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateHideCardDetailsDelegateByMark()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                var markArea = SettingsTable.Get <MarkAreaCfg>(Strings.VScan_MarkAreaConfiguration, MarkAreaCfg.Empty);

                if (markArea.Equals(MarkAreaCfg.Empty))
                {
                    var ex = new ApplicationException("Cannot find 'hide area' configuration.");
                    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;
                }

                Rectangle hiddenArea;

                using (var bmp2 = data.VoucherImage.ToBlackWhite(markArea.BlackWhiteThreshold))
                    hiddenArea = bmp2.FindRectangle(Point.Empty, markArea.BackColor, markArea.MinAreaSize, markArea.MaxAreaSize, markArea.Threshold);

                var hiddenAreaDraw = SettingsTable.Get <HiddenAreaDrawingCfg>(Strings.VScan_DistanceFromBarcodeBottomLeftToHiddenArea, HiddenAreaDrawingCfg.Default);

                using (var g = Graphics.FromImage(data.VoucherImage))
                    g.FillRectangleHatch(hiddenArea, hiddenAreaDraw.ForeColor, hiddenAreaDraw.BackColor, hiddenAreaDraw.Style);
            });

            return(method);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Used to find and cover the cardcode by using the barcode location
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateHideCardDetailsDelegateByBarcode()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                var distanceToHiddenArea = SettingsTable.Get <Point>(Strings.VScan_DistanceFromBarcodeBottomLeftToHiddenArea, Point.Empty);
                if (distanceToHiddenArea == Point.Empty)
                {
                    var ex = new ApplicationException("Cannot find chunk configuration.");
                    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;
                }

                Size hideAreaSize        = SettingsTable.Get <Size>(Strings.VScan_HiddenAreaSize, Size.Empty);
                Point hiddenAreaLocation = new Point(data.BarCodeArea.Left, data.BarCodeArea.Bottom);

                hiddenAreaLocation.Offset(distanceToHiddenArea);
                Rectangle hiddenArea = new Rectangle(hiddenAreaLocation, hideAreaSize);

                var hiddenAreaDraw = SettingsTable.Get <HiddenAreaDrawingCfg>(Strings.VScan_HiddenAreaDrawingCfg, HiddenAreaDrawingCfg.Default);

                using (var g = Graphics.FromImage(data.VoucherImage))
                    g.FillRectangleHatch(hiddenArea, hiddenAreaDraw.ForeColor, hiddenAreaDraw.BackColor, hiddenAreaDraw.Style);
            });

            return(method);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Used to insert voucher data to the database
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateInsertVoucherDelegate()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                Debug.Assert(state.Dib != IntPtr.Zero);
                Debug.Assert(state.Main != IntPtr.Zero);
                Debug.Assert(state.Scan != IntPtr.Zero);
                Debug.Assert(data != null);

                try
                {
                    data.Validate();

                    ClientDataAccess.UpdateFileAsync((DbClientVoucher)data,
                                                     new EventHandler((o, e) =>
                    {
                        string fileName = Path.ChangeExtension(
                            string.Format("{0}{1}", PTFUtils.GetMachine(), DateTime.Now).ReplaceAll(new char[] { '.', '/', '\\', '-' }, '_'), ".xml");

                        //Data has been saved successfully. Show message
                        string id = string.Concat(Strings.VScan_ItemSaved, fileName);
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Main, id);
                    }),
                                                     new ThreadExceptionEventHandler((o, e) =>
                    {
                        //Data has failed to save.
                        var ex = new ApplicationException("Can't insert: ".concat(e.Exception.Message), e.Exception);
                        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;
                    }));
                }
                catch (Exception e)
                {
                    var ex = new ApplicationException("Err: ".concat(e.Message), e);
                    ex.AddNext(new MethodInvoker(() =>
                    {
                        string id    = Strings.VScan_EditItem.Uniqueue();
                        data.Message = ex.Message;
                        DataSlot.Set(id, data);
                        WinMsg.SendText(state.Main, id);
                    }));
                    throw ex;
                }
            });

            return(method);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Send data to UI editor directly
        /// </summary>
        /// <returns></returns>
        public static VoucherProcessDelegate CreateSendToUIDelagete()
        {
            var method = new VoucherProcessDelegate((Voucher data, StateObj state) =>
            {
                string id    = Strings.VScan_EditItem.Uniqueue();
                data.Message = "Insert data to db is switched off";
                DataSlot.Set(id, data);
                WinMsg.SendText(state.Scan, state.Main, id);
            });

            return(method);
        }
Exemplo n.º 9
0
        public bool PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = m_twain.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.TransferReady:
            {
                bool status = m_twain.TransferPictures(VoucherData);
                if (status)
                {
                    AcquireVoucher(true);
                }
                else
                {
                    m_twain.CloseDS();
                    m_twain.CloseDSM();
                    Close();
                }
                break;
            }

            case TwainCommand.CloseRequest:
            {
                m_twain.CloseDS();
                m_twain.CloseDSM();
                Close();
                break;
            }

            case TwainCommand.CloseOk:
            {
                WinMsg.SendText(m_MainFormHandler, Strings.VScan_ScanIsDoneEvent);
                Close();
                break;
            }

            case TwainCommand.DeviceEvent:
                break;

            default:
                break;
            }

            return(true);
        }
Exemplo n.º 10
0
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
            case user32.WM_COPYDATA:
            {
                //The scaning Form sends ready status
                //Get image from appslot
                IntPtr sender;
                string text = WinMsg.ReceiveText(m.LParam, out sender);
                if (!string.IsNullOrEmpty(text))
                {
                    if (text.StartsWith(Strings.VScan_EditItem))
                    {
                        new MethodInvoker(() =>
                            {
                                const int MAX_OPENED_FORMS = 5;
                                int openedForms            = ScanAppContext.Default.OpenFormsOf <VoucherForm>().Count();
                                int settings = SettingsTable.Get <int>(Strings.VScan_MaximumOpenedScanForms, MAX_OPENED_FORMS);
                                if (openedForms > settings)
                                {
                                    WinMsg.SendText(sender, Strings.VScan_StopTheScanner);
                                    throw new AppExclamationException("Too many opened windows in editor.\nStop scanning.");
                                }
                            }).FireAndForget();

                        var data = DataSlot.Get <Voucher>(text);
                        Debug.Assert(data != null && data.VoucherImage != null && !string.IsNullOrEmpty(data.Message),
                                     "MainForm.WndProc");
                        try
                        {
                            VoucherForm form = AddNewChild();
                            form.Canvas.Update(data);
                        }
                        finally
                        {
                            if (VoucherMonitorForm.IsStarted)
                            {
                                VoucherMonitorForm.ShowImage(text);
                            }
                            DataSlot.Free(text);
                        }
                    }
                    else if (text.StartsWith(Strings.VScan_ItemSaved))
                    {
                        string[] msgs = text.Split('|');
                        if (msgs.Length > 1)
                        {
                            tssLabel1.Text = msgs[0];
                            tssLabel2.Text = msgs[1];
                        }

                        if (VoucherMonitorForm.IsStarted)
                        {
                            VoucherMonitorForm.ShowImage(text);
                        }
                        DataSlot.Free(text);
                    }
                    else if (text.CompareNoCase(Strings.VScan_ScanIsDoneEvent))
                    {
                        WaitForm.Stop(this);
                    }
                    else if (string.Compare(text, Strings.Transferring_RemoteLock) == 0)
                    {
                        Lock();
                    }
                }
            }
            break;

            default:
                base.WndProc(ref m);
                break;
            }
        }
Exemplo n.º 11
0
        public void ExportAndCloseAsync()
        {
            if (this.Parent != null)
            {
                IntPtr main = this.Parent.Handle;

                Voucher bData = Canvas.Data;
                bData.VoucherImage = (Bitmap)Canvas.BackgroundImage;

                var cnts = Canvas.Controls.Find(ManualInsertDataControl.MANUALINSERT_DATACONTROL_NAME, true);
                if (cnts != null && cnts.Length != 0)
                {
                    ManualInsertDataControl cnt = (ManualInsertDataControl)cnts[0];
                    if (!string.IsNullOrEmpty(cnt.BarCodeString))
                    {
                        bData.Parse(cnt.BarCodeString);
                    }
                    else
                    {
                        string[] str = cnt.BarCodeNumberGroups;
                        bData.CountryID = int.Parse(str[0]);
                        //bData.Business = int.Parse(str[1]);
                        bData.RetailerID    = int.Parse(str[2]);
                        bData.VoucherID     = str[3];
                        bData.BarCodeString = string.Format("{0:000}{1:00}{2:000000}{3:000000000}",
                                                            bData.CountryID, "na", bData.RetailerID, bData.VoucherID);
                    }
                }

                new Action <Voucher>((data) =>
                {
                    Debug.Assert(data != null);
                    Debug.Assert(data.VoucherImage != null);

                    lock (data.VoucherImage)
                    {
                        try
                        {
                            DelegateUtils.CreateAuditIdReloadDelegate().FireAndForget();

                            data.SiteCode = AuditIDSTable.SelectRemoveFirstOrEmpty().ThrowIfDefault <string, AppExclamationException>();

                            data.Validate();

                            ClientDataAccess.UpdateFileAsync((DbClientVoucher)data,
                                                             new EventHandler((o, s) =>
                            {
                                string fileName = Path.ChangeExtension(
                                    string.Format("{0}{1}", PTFUtils.GetMachine(),
                                                  DateTime.Now).ReplaceAll(new char[] { '.', '/', '\\', '-' }, '_'), ".xml");

                                //Data has been saved successfully.
                                string text = string.Concat(Strings.VScan_ItemSaved, fileName);
                                WinMsg.SendText(main, text);
                                this.InvokeSf(() =>
                                {
                                    //Set DialogResult OK
                                    //Or else the form will resign to close
                                    this.DialogResult = DialogResult.OK;
                                    this.Close();
                                });
                            }),
                                                             //No error handling.
                                                             //General processing handler.
                                                             null);
                        }
                        catch (Exception ee)
                        {
                            ee.AddNext(new MethodInvoker(() =>
                            {
                                Canvas.InvokeSf(() =>
                                {
                                    Canvas.ShowInputControl(ee.Message);
                                });
                            }));
                            throw;
                        }
                    }
                }).FireAndForget(bData);
            }
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }