需要帮助=使用带条形码扫描的课程

时间:2011-04-10 12:49:06

标签: c# windows-ce

我有来自摩托罗拉的代码,可以扫描Windows-CE中的条形码

它的效果非常好,但如何使我的所有表单都可以使用

这个扫描仪?

扫描仪类:

using System;
using System.Data;
using System.IO;
using System.Windows.Forms;


namespace MyScann
{
    /// <summary>
    /// Summary description for Scanner.
    /// </summary>
    public class Scanner
    {

        public static Symbol.Barcode.Reader SymbolReader = null;
        public static Symbol.Barcode.ReaderData SymbolReaderData = null;
        public static System.EventHandler SymbolEventHandler = null;
        public static DataSet CodeDataSet;


        //public static Utils.Sound ReadErrorSound;

        public static bool ScannerEnabled = false;
        public static void ActivateScanner()
        {

            // If we have both a reader and a reader-data Object
            if ( Scanner.SymbolReader != null &&
                Scanner.SymbolReaderData != null &&
                ! Scanner.SymbolReaderData.IsPending )
            {
                // Submit 'Read'
                try
                {
                    Scanner.SymbolReader.Actions.Read(Scanner.SymbolReaderData);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    if ( err.InnerException != null )
                        MessageBox.Show(err.InnerException.Message);
                }
            }

        }

        public static void DeactivateScanner()
        {

            // If we have both a reader and a reader-data Object
            if ( Scanner.SymbolReader != null &&
                Scanner.SymbolReaderData != null &&
                Scanner.SymbolReaderData.IsPending )
            {
                // Submit 'CancelRead'
                try
                {
                    Scanner.SymbolReader.Actions.CancelRead(Scanner.SymbolReaderData);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    if ( err.InnerException != null )
                        MessageBox.Show(err.InnerException.Message);
                }
            }

        }

        /// <summary>
        /// Initialize the reader.
        /// </summary>
        /// <returns>False if an error accurs</returns>
        public static bool InitSymbolReader()
        {

            // If reader is already present then fail initialize
            if ( SymbolReader != null )
            {
                return false;

             }

            // Create new reader, first available reader will be used.
            SymbolReader = new Symbol.Barcode.Reader();
            // Create reader data
            SymbolReaderData = new Symbol.Barcode.ReaderData(
                Symbol.Barcode.ReaderDataTypes.Text,
                Symbol.Barcode.ReaderDataLengths.DefaultText);


            // set scanner read error sound
            //ReadErrorSound = new Utils.Sound(ErrorSound);

            return true;
        }

        /// <summary>
        /// Stop reading and disable/close reader
        /// </summary>
        public static void DeinitSymbolReader()
        {
            try
            {
                // If we have a reader
                if ( SymbolReader != null )
                {
                    // Disable the reader
                    SymbolReader.Actions.Disable();

                    // Free it up
                    SymbolReader.Dispose();

                    // Indicate we no longer have one
                    SymbolReader = null;
                }

                // If we have a reader data
                if ( SymbolReaderData != null )
                {
                    // Free it up
                    SymbolReaderData.Dispose();

                    // Indicate we no longer have one
                    SymbolReaderData = null;
                }
            }
            catch {}
        }

        /// <summary>
        /// Enable most barcode type with no limitation on length
        /// </summary>
        public static void OpenAllBarcodes()
        {
            SymbolReader.Decoders.CODABAR.Enabled = true;
            SymbolReader.Decoders.CODABAR.MinimumLength = 0;
            SymbolReader.Decoders.CODABAR.MaximumLength = 0;

            SymbolReader.Decoders.CODE11.Enabled = true;
            SymbolReader.Decoders.CODE11.MinimumLength = 0;
            SymbolReader.Decoders.CODE11.MaximumLength = 0;

            SymbolReader.Decoders.CODE39.Enabled = true;
            SymbolReader.Decoders.CODE39.MinimumLength = 0;
            SymbolReader.Decoders.CODE39.MaximumLength = 0;

            SymbolReader.Decoders.CODE93.Enabled = true;
            SymbolReader.Decoders.CODE93.MinimumLength = 0;
            SymbolReader.Decoders.CODE93.MaximumLength = 0;

            SymbolReader.Decoders.CODE128.Enabled = true;
            SymbolReader.Decoders.CODE128.MinimumLength = 0;
            SymbolReader.Decoders.CODE128.MaximumLength = 0;

            SymbolReader.Decoders.DATAMATRIX.Enabled = true;
            SymbolReader.Decoders.DATAMATRIX.MinimumLength = 0;
            SymbolReader.Decoders.DATAMATRIX.MaximumLength = 0;

            SymbolReader.Decoders.D2OF5.Enabled = true;
            SymbolReader.Decoders.D2OF5.MinimumLength = 0;
            SymbolReader.Decoders.D2OF5.MaximumLength = 0;

            SymbolReader.Decoders.I2OF5.Enabled = true;
            SymbolReader.Decoders.I2OF5.MinimumLength = 0;
            SymbolReader.Decoders.I2OF5.MaximumLength = 0;

            SymbolReader.Decoders.EAN13.Enabled = true;
            SymbolReader.Decoders.EAN13.MinimumLength = 0;
            SymbolReader.Decoders.EAN13.MaximumLength = 0;

            SymbolReader.Decoders.EAN8.Enabled = true;
            SymbolReader.Decoders.EAN8.MinimumLength = 0;
            SymbolReader.Decoders.EAN8.MaximumLength = 0;

            SymbolReader.Decoders.PDF417.Enabled = true;
            SymbolReader.Decoders.PDF417.MinimumLength = 0;
            SymbolReader.Decoders.PDF417.MaximumLength = 0;

            SymbolReader.Decoders.UPCA.Enabled = true;
            SymbolReader.Decoders.UPCA.MinimumLength = 0;
            SymbolReader.Decoders.UPCA.MaximumLength = 0;

            SymbolReader.Decoders.UPCE1.Enabled = true;
            SymbolReader.Decoders.UPCE1.MinimumLength = 0;
            SymbolReader.Decoders.UPCE1.MaximumLength =0;

            SymbolReader.Decoders.UPCE0.Enabled = true;
            SymbolReader.Decoders.UPCE0.MinimumLength = 0;
            SymbolReader.Decoders.UPCE0.MaximumLength = 0;





            SymbolReader.Parameters.CodeIdType = Symbol.Barcode.CodeIdTypes.None;
            SymbolReader.Parameters.ScanType = Symbol.Barcode.ScanTypes.Background;
            SymbolReader.Parameters.LocalFeedback = Symbol.Barcode.DisabledEnabled.Enabled;

            Scanner.SymbolReader.Parameters.Feedback.Success.BeepTime = 0;
        }







    }
}

我在表单中使用的内容:

 public void InitScanner(byte BatteryLevel)
        {

            // Create event handler delegate
            if (Scanner.SymbolEventHandler == null)
            {
                Scanner.SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
                // Enable reader, with wait cursor
                Scanner.SymbolReader.Actions.Enable();
                Scanner.OpenAllBarcodes();
                Scanner.ScannerEnabled = true;
            }
            // If we have both a reader and a reader data
            if ((Scanner.SymbolReader != null) &&
                (Scanner.SymbolReaderData != null))
            {
                // Submit a read
                Scanner.SymbolReader.ReadNotify += Scanner.SymbolEventHandler;
                Scanner.SymbolReader.Actions.Read(Scanner.SymbolReaderData);
            }
        }

        public void SymbolReader_ReadNotify(object sender, EventArgs e)
        {
            Symbol.Barcode.ReaderData TheReaderData = Scanner.SymbolReader.GetNextReaderData();
            if (TheReaderData.Result == Symbol.Results.SUCCESS && (txtBarcode.Focused == true))
            {
                if (txtBarcode.Focused == true)
                {
                    txtBarcode.Text = TheReaderData.Text.ToString();
                    SymbolReader_CycleScannerReader();
                    return;
                }
            }
            SymbolReader_CycleScannerReader();
        }

        public void SymbolReader_CycleScannerReader()
        {
            Scanner.SymbolReader.Actions.Read(Scanner.SymbolReaderData);
        }

        public void StartScanner()
        {
            bool flag = false;
            try
            {
                Scanner.SymbolEventHandler = null;
                Scanner.DeinitSymbolReader();
                Scanner.ScannerEnabled = false;
                if (!Scanner.ScannerEnabled)
                {
                    Scanner.SymbolEventHandler = null;
                    Scanner.InitSymbolReader();
                    InitScanner(100);
                    flag = true;
                }
            }
            catch
            {
                Scanner.SymbolEventHandler = null;
                Scanner.InitSymbolReader();
                InitScanner(100);
                flag = true;
            }
            finally
            {
                if (!flag)
                {
                    MessageBox.Show("Scanner Error");
                }
            }
        }

        public void CloseScanner()
        {
            Scanner.SymbolEventHandler = null;
            Scanner.DeinitSymbolReader();
        }
public Main()中的

我使用StartScanner();

当程序结束时,我使用:CloseScanner();

我在表单中的txtBarcode上阅读了条形码

提前致谢

1 个答案:

答案 0 :(得分:2)

如果没有给出我们办公室提供的确切答案,这里是我们所做的伪代码。类似地,手持扫描仪设备具有内置的扫描和识别不同条形码格式的能力。所以,我们所做的是将扫描代码包装到我们的表单中......类似于

public class OurForm : Form
{
    protected MyScannerClass oScanner

    public OurForm()
    {
        // create your class no matter what form uses it (subclass from this class)
        oScanner = new MyScannerClass();

        // Now, force registration of your "scanning" event to pass the incoming 
        // string to THIS form's NewScan event.
        oScanner.OnYourScannerScanEvent += NewScan();

    }

    // Now, this being a VIRTUAL, you can OVERRIDE this on each specific form and
    // parse / interpret the data as needed... based on the specific field user is in,
    // type of code (Code39, PDF417 (2d barcode), etc)
    protected void NewScan( string StringParameterFromScannerHandler, otherParm ofScanType )
    {
        // do whatever cleanup / pre-validation that may be common to ALL forms you use...
        // THEN pass on to secondary method to actually be overridden specific to each form
        FormSpecificHandler( StringParameterFromScannerHandler, ofScanType );
    }

    protected virtual void FormSpecificHandler( 
        string StringParameterFromScannerHandler, 
        otherParm ofScanType )
    {


    }


public class YourSubForm : OurForm 
{
    protected override void FormSpecificHandler( 
        string StringParameterFromScannerHandler, 
        otherParm ofScanType )
    {
        if( YourCurrentField == Something and ofScanType = PDF417 )
        {
            // do whatever with your scan results...
        }
    }
}