如何使用条形码扫描仪读取条形码 [英] How to read a barcode using a barcode scanner

查看:175
本文介绍了如何使用条形码扫描仪读取条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,



我是VB.NET的初学者,但我可以设法完成小而简单的编程。



我将在项目中做的是生成条形码并将其分发给人们。它们配有几个条形码扫描仪,每个人都有3个条形码可以扫描完成一个条目。

我可以设法生成条形码,现在我需要的是读取条形码并保存数据我的电脑。我计划将它与服务器连接,但是暂时我想将它本地保存在我的机器上。你能告诉我怎么做吗。



我可以告诉我更多的信息,

如果我买条形码扫描仪,他们是否提供任何软件来收集读取数据,该软件是否可定制。

你能告诉我条码阅读的工作原理吗。



请帮帮我,因为这是我的项目,急需。

非常感谢任何帮助。





提前致谢。

Hello experts,

I am a beginner to VB.NET but I could manage to get things done for small and simple programming.

What I will do in my project is that I will generate barcode and distribute it to the people. They are provided with a couple of barcode scanners and every person will have 3 barcodes to scan to complete one entry.
I could manage to generate barcode, now what I need is read the barcode and save data on my pc. I have plan to connect it with server, however for the time being I want to save it locally on my machine. Can you tell me how to do these.

For my more information can you also tell me,
If I buy a barcode scanner, do they provide any software to collect the read data, is that software customizable.
Could you please tell me how barcode reading works.

Please help me as this is my project and is required urgently.
Any help is highly appreciated.


Thanks in advance.

推荐答案

如果您购买带USB连接器的条形码扫描仪,它们将具有键盘仿真功能。这意味着,他们会向您的应用程序发送按键事件,就像您通过键盘输入内容一样。您可以通过打开任何文字处理应用程序并扫描条形码来验证这一点。它看起来好像是通过键盘输入的。



所以你要做的就是确保一些文本输入控件在尝试之前有输入焦点扫描一些东西,然后像任何文本输入一样处理它。



理论上有一些方法可以直接从条形码扫描仪中读取它,这样你就不必有文本输入控制,但这会更高级,因为你说你是VB.NET的初学者,很可能超出你目前的范围。无论如何你想看一下,我只能用一篇用C#做的文章为你服务:

使用C#的原始输入来处理多个键盘 [ ^ ]



评论后编辑:

我为您构建了一个示例代码。创建一个新的Windows-Forms-Project,通过Designer向表单添加三个TextBox,并将它们命名为BarcodeInput1,BarcodeInput2,BarcodeInput3并添加一个名为DemoLabel的Label。然后使用以下代码替换Form1的代码(源文件Form1.vb)。



如果在TextBox 1中输入(或扫描)某些内容然后按输入(通常条形码扫描仪在扫描条形码后自动执行),输入/扫描的文本将由DemoLabel显示,输入焦点将自动前进到TextBox 2,然后再前进到TextBox 3,再从那里前进到TextBox 1。



请注意,这只是一个样本。我没有清除TextBoxes中输入的文本。 VB.NET代码是C#的自动翻译,可能包含一两个奇怪的东西(但它正在工作)。



VB.NET

If you buy barcode-scanners with an USB-connector, they will have keyboard-emulation. Meaning, they will send key-press-events to your application as if you entered something via keyboard. You can verify that by opening just any word-processing application and scanning a barcode. It should appear as if entered it via keyboard.

So all you would have to do is to ensure that some text-input-control has the input-focus before attempting to scan something and then process it like any text-input.

Theoretically there are ways to directly read it from the barcode-scanner so that you don't have to have that text-input-control but that would be way more advanced and, as you're saying you're a beginner to VB.NET, most probably out of your current scope. If you want to take a look at that anyway, I can serve you only with an article that does this with C#:
Using Raw Input from C# to handle multiple keyboards[^]

Edit after comment:
I built a sample-code for you. Create a new Windows-Forms-Project, add three TextBoxes to the Form via the Designer and name them BarcodeInput1, BarcodeInput2, BarcodeInput3 and add a Label named DemoLabel. Then replace the code of Form1 (source file "Form1.vb") with the following code.

If you enter (or scan) something in TextBox 1 and then press Enter (which usually barcode scanners automatically do after scanning a barcode), the entered/scanned Text will be displayed by DemoLabel and the input focus will automatically advance to TextBox 2, then to TextBox 3 and from there again to TextBox 1.

Please note that it's just a sample. I didn't clear the entered text from the TextBoxes. The VB.NET-code is an automatic translation from C# and may contain one or two oddities (but it's working).

VB.NET
Public Class Form1
    Private TextBoxOrder As New Dictionary(Of TextBox, TextBox)()

    Public Sub New()
        InitializeComponent()

        TextBoxOrder.Add(BarcodeInput1, BarcodeInput2)
        TextBoxOrder.Add(BarcodeInput2, BarcodeInput3)
        TextBoxOrder.Add(BarcodeInput3, BarcodeInput1)

        BarcodeInput1.Tag = 1
        BarcodeInput2.Tag = 2
        BarcodeInput3.Tag = 3

        AddHandler BarcodeInput1.KeyDown, AddressOf BarcodeInputKeyDown
        AddHandler BarcodeInput2.KeyDown, AddressOf BarcodeInputKeyDown
        AddHandler BarcodeInput3.KeyDown, AddressOf BarcodeInputKeyDown

        AddHandler BarcodeInput1.Leave, AddressOf BarcodeInputLeave
        AddHandler BarcodeInput2.Leave, AddressOf BarcodeInputLeave
        AddHandler BarcodeInput3.Leave, AddressOf BarcodeInputLeave
    End Sub

    Private Sub BarcodeInputKeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter AndAlso ActiveControl.[GetType]() = GetType(TextBox) Then
            Dim nextTextBox As TextBox
            If TextBoxOrder.TryGetValue(DirectCast(ActiveControl, TextBox), nextTextBox) Then
                e.Handled = True
                e.SuppressKeyPress = True
                nextTextBox.Focus()
            End If
        End If
    End Sub

    Private Sub BarcodeInputLeave(sender As Object, e As EventArgs)
        If sender.[GetType]() = GetType(TextBox) Then
            Dim textBox As TextBox = DirectCast(sender, TextBox)
            If textBox.Tag.[GetType]() = GetType(Integer) Then
                BarcodeScanned(textBox.Text, CInt(textBox.Tag))
            End If
        End If
    End Sub

    Private Sub BarcodeScanned(barcode As String, order As Integer)
        DemoLabel.Text = Convert.ToString(order.ToString() + ": ") & barcode
    End Sub
End Class





C#



C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BarcodeTest
{
    public partial class Form1 : Form
    {
        private Dictionary<TextBox, TextBox> TextBoxOrder = new Dictionary<TextBox, TextBox>();

        public BarcodeInputForm()
        {
            InitializeComponent();

            TextBoxOrder.Add(BarcodeInput1, BarcodeInput2);
            TextBoxOrder.Add(BarcodeInput2, BarcodeInput3);
            TextBoxOrder.Add(BarcodeInput3, BarcodeInput1);

            BarcodeInput1.Tag = 1;
            BarcodeInput2.Tag = 2;
            BarcodeInput3.Tag = 3;

            BarcodeInput1.KeyDown += BarcodeInputKeyDown;
            BarcodeInput2.KeyDown += BarcodeInputKeyDown;
            BarcodeInput3.KeyDown += BarcodeInputKeyDown;

            BarcodeInput1.Leave += BarcodeInputLeave;
            BarcodeInput2.Leave += BarcodeInputLeave;
            BarcodeInput3.Leave += BarcodeInputLeave;
        }

        private void BarcodeInputKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && ActiveControl.GetType() == typeof(TextBox))
            {
                TextBox nextTextBox;
                if(TextBoxOrder.TryGetValue((TextBox)ActiveControl, out nextTextBox))
                {
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                    nextTextBox.Focus();
                }
            }
        }

        private void BarcodeInputLeave(object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(TextBox))
            {
                TextBox textBox = (TextBox)sender;
                if (textBox.Tag.GetType() == typeof(int))
                {
                    BarcodeScanned(textBox.Text, (int)textBox.Tag);
                }
            }
        }

        private void BarcodeScanned(string barcode, int order)
        {
            DemoLabel.Text = order.ToString() + ": " + barcode;
        }
    }
}


这篇关于如何使用条形码扫描仪读取条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆