VB 识别条形码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB 识别条形码相关的知识,希望对你有一定的参考价值。

谁能教我, 用VB 生成一个 条形码,和识别 条形码的程序?

请教请教~~~

一、条形码的读取

用过键盘口式的扫条码工具的朋友就知道,它就如同在键盘上按下数字键一样,基本不需任何编程和处理。但如果你使用的是其它接口的话,可能你就要为该设备编写通讯代码了。以下有一段简单的25针串口的条码读取器通讯代码。

Option Explicit
Dim sData As String
Private Sub Form_Load()
With MSComm1
.CommPort = 3 \'设为COM3,试运行的系统而定,你可提供一个Combox让用户选择。
.PortOpen = True \'打开通讯端口
End With
End Sub

Private Sub MSComm1_OnComm()

Dim EndPos As Integer
Select Case MSComm1.CommEvent
Case comEvReceive \'当有数据传送过来时
sData = sData & Trim(MSComm1.Input)
\'检索回车,通常读卡机每组数据结尾都返回一个回车作为结束符
EndPos = InStr(1, sData, Chr(13))
If EndPos = 0 Then \'如果未结束就继续努力
Else \'读完一组。
lblBarCode.Caption = sData \'显示一组条形码
With lstBarCode
.AddItem Mid(sData, 1, EndPos - 1) \'添加一组条形码到列表
End With
sData = "" \'清空
End If
End Select
End Sub

Private Sub cmdEnd_Click()
MSComm1.PortOpen = False \'关闭端口
End
End Sub

二:条形码的生成
在VB上编程本来就不难。以下关于条形码生成的代码也是很容易理解,只需使用一个OFFICE的附带的 BarCode控件就可以轻松打印出11种不同标准的条形码,足以满足我们的要求。想起我书架上的一本书中的一篇用Turbo C编写条形码打印程序文章,长篇大论,那时不知看了n天,打了n小时字结果也不尽人意,现在真是幸福多了:)。废话说完,得回归正题。且看条形码生成的代码及有关说明。
源代码主要由两个窗体(frmMain主窗体和frmOption条码设置窗体)和两个模块组成(modGetScreen.bas、SysDLG32.bas)。考虑到篇幅,这里只列出部分较为关键的代码。

新建一个标准工程,添加一个名为(Microsoft Access BarCode
Control9)的条形码部件,并添加一个条码控件到窗口,并将窗口改名为frmMain,如图所示。由于控件比较多,这里不便细说,详细内容请看源代码。
模块modGetScreen.bas代码如下:
Option Explicit
声明BitBlt、GetDesktopWindow、GetWindowDC、ReleaseDC这几个API函数略
Public RegUser As Boolean

Sub GetObjImage1(Obj As Object, OwnerForm As PictureBox, Picture1
As PictureBox)
\'hDC
Dim hWndDesk As Long
Dim hDCDesk As Long
\'区域表达变量
Dim x As Long
Dim y As Long
Dim w As Long
Dim h As Long

x = Obj.Left Screen.TwipsPerPixelX
y = Obj.Top Screen.TwipsPerPixelY
w = Obj.Width Screen.TwipsPerPixelX
h = Obj.Height Screen.TwipsPerPixelY
hDCDesk = OwnerForm.hdc
\'取出图像
Call BitBlt(Picture1.hdc, 0, 0, w, h, hDCDesk, x, y,
vbSrcCopy)
Call ReleaseDC(hWndDesk, hDCDesk)

End Sub

主窗体frmMain.frm部分代码如下:
Private Sub cmdPrint_Click()
\'生成条形码图像
Dim r As Long, i As Integer, t As String,cfile As
String \'临时变量
t = BarCode
For i = 0 To Val(Times) - 1

BarCode1.Value = BarCode + i
DoEvents
Picture1.Refresh

GetObjImage1 BarCode1, Conel, Picture1

If RegUser = False Then \'如果未注册添加MASK标记
Picture1.PaintPicture Picture2.Picture, 300, 300
End If

If Dir(SavePath, vbDirectory) = "" Then MkDir SavePath

SavePath = SavePath & IIf(Right(SavePath, 1) <> "", "",
"")

cfile = SavePath & BarCode1.Value & ".bmp"

SavePicture Picture1.Image, cfile \'将条形码保存为图像文件以便打印
Next
BarCode = t

End Sub

条形码设置窗体frmOption.frm代码如下:
Option Explicit
\'条形码设置模块

Private Sub cboBig_Click()
BarCode1.Style = cboBig.ListIndex \'改变标准
End Sub

Private Sub cboDirection_Click()
BarCode1.Direction = cboDirection.ListIndex \'改变方向
End Sub

Private Sub cboLine_Click()
BarCode1.LineWeight = cboLine.ListIndex \'改变线宽
End Sub

Private Sub cboSmall_Click()
BarCode1.SubStyle = cboSmall.ListIndex \'改变样式
End Sub

Private Sub Check1_Click()
BarCode1.ShowData = Check1.Value \'是否显示数据
End Sub

Private Sub cmdChange_Click()
\'设置长、宽大小
BarWidth = BarCode1.Height
BarHeight = BarCode1.Width
cmdRefresh_Click
End Sub

Private Sub cmdOK_Click()
\'传送条形码设定到主界面
With frmMain.BarCode1
.LineWeight = BarCode1.LineWeight
.Style = BarCode1.Style
.SubStyle = BarCode1.SubStyle
.Direction = BarCode1.Direction
.Width = BarCode1.Width
.Height = BarCode1.Height
.ShowData = BarCode1.ShowData
Me.Hide
End With
With frmMain
.Picture1.Width = .BarCode1.Width
.Picture1.Height = .BarCode1.Height
.Conel.Width = .BarCode1.Width
.Conel.Height = .BarCode1.Height
End With
End Sub

Private Sub cmdRefresh_Click()
BarCode1.Width = BarWidth
BarCode1.Height = BarHeight
End Sub

Private Sub Form_Load()
LoadBarInfo
BarWidth = BarCode1.Width
BarHeight = BarCode1.Height
End Sub

Sub LoadBarInfo() \'初始化选项
LoadBigClass cboBig
LoadSmallClass cboSmall
LoadLineSize cboLine
LoadDirection cboDirection
End Sub
Sub LoadBigClass(cbo As ComboBox) \'条码标准
With cbo
.AddItem "UPC-A"
.AddItem "UPC-E"
.AddItem "EAN-13"
.AddItem "EAN-8"
.AddItem "Case Code"
.AddItem "Codabar (NW-T)"
.AddItem "Code-39"
.AddItem "Code-128"
.AddItem "U.S. Postnet"
.AddItem "U.S. Postal FIM"
.AddItem "JP Post"
.ListIndex = 2
End With
End Sub
Sub LoadSmallClass(cbo As ComboBox) \'条码样式
With cbo
.AddItem "Standard"
.AddItem "2-Digit Supplement"
.AddItem "5-Digit Supplement"
.AddItem "POS Case Code"
.ListIndex = 0
End With
End Sub

许多人在编写数据库应用程序时,都想要加上条形码功能加强工作效率,尤其是销售管理,图书馆管理这类流量大的应用软件,但由于条形码技术难以掌握、标谁又多以及过去的技术种种原因,使得许多人望而却步。本文介绍的一套简单实用的条形码解决方法,希望能帮助各位完善软件系统的功能。追问

其实 我就是想 把一副 条形码的图片,用VB 判断,是否原来的条形码

其实你们上面的是什么的?

条形码判断 是不是,靠图片的像素判断? 两个只要相等的话,就判断为true

追答

你的问题已经超出了条形码识别的范围了。
靠像素来判断两张图片是否一样比较难,给你个方法,把图片根据文件的byte[]生成一个md5码,存入数据库,通过对比数据库中的MD5码来判断两张图片是否一样的,这种方法比较快捷。

参考资料:www.51saomiao.com

参考技术A 识别条形码,用专业的条形码设备,从几十元到几千的都有,红外,无线等

VB 中生成条形码,一般用组件来生成条形码PICTURE 或者文件,可以用来打印输出,
如果只是界面上显示用,可以找原来OFFICE中一个组件,微软的条形码控件。
找我,41722866 , 我有开发条形码组件,
参考技术B 买设备的时候应该带开发文档

图像识别基于二维条形码识别matlab 源码含GUI

一、简介

基于matlab GUI二维条形码的识别

二、源代码

function varargout = untitled(varargin)
% UNTITLED M-file for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
 
% Edit the above text to modify the response to help untitled
 
% Last Modified by GUIDE v2.5 03-Nov-2011 13:59:22
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
 
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
 
 
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)
 
% Choose default command line output for untitled
handles.output = hObject;
image1=imread('C:\\Users\\lenovo\\Desktop\\61602302barcodehcc\\barcodehcc\\1.bmp');
axes(handles.axes1);
imshow(image1);
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Get default command line output from handles structure
varargout{1} = handles.output;
 
% --- Executes on mouse press over axes background.
 
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
global im;
%提取条形码区域构成的矩形的左上角坐标
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
image=im;
l1=rgb2gray(image);
level=graythresh(l1);
l2=im2bw(l1,level);
l3=~l2;
l4=bwareaopen(l3,50);
l5=~l4;
l6=edge(l1,'canny');
l7=imclose(l6,strel('rectangle',[2,19]));
l8=imopen(l7,strel('rectangle',[2,19]));
l9=imopen(l8,strel('rectangle',[2,19]));
[L,num]=bwlabel(l9,8);
STATS=regionprops(L,'all');
a=length(STATS);
%figure,imshow(L);
%hold on;
b=0;
for i=1:a
    temp=STATS(i).BoundingBox;
    s=temp(3)*temp(4);
    if s>b
        b=s;
        k=i;%记录面积最大的标记区域的索引值
    end
end
%rectangle('position',STATS(k).BoundingBox,'edgecolor','r');%绘制最大标记区域的外接矩形
temp=STATS(k).BoundingBox;
Rx=round(temp(1));Ry=round(temp(2));%提取条形码区域左上角的坐标
Rwidth=round(temp(3));Rlength=round(temp(4));
 
 
%初始化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
check_left = [13,25,19,61,35,49,47,59,55,11;...	%左边数据编码,奇
              39,51,27,33,29,57, 5,17, 9,23];	%左边数据编码,偶
check_right = [114,102,108,66,92,78,80,68,72,116];	%右边数据编码
first_num = [31,20,18,17,12,6,3,10,9,5];    %第一位数据编码
bar = im;  %读输入条形码图片
bar_Gray = rgb2gray(bar);   %将RGB图片转换灰度图
 
[a_hist x] = imhist(bar_Gray);   %绘制灰度直方图,返回直方图数据向量a_hist,和相应的色彩值向量x
 
%寻找进行二值化处理的阈值,存放在T中
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
hist_max = [];
if a_hist(1)>a_hist(2)
    hist_max = [hist_max 1];
end
x = max(x);
for i=2:x
    if a_hist(i)>a_hist(i-1) && a_hist(i)>a_hist(i+1)
        hist_max = [hist_max i];
    end
end
if a_hist(x)<a_hist(x+1)
    hist_max = [hist_max x+1];
end
[m,n] = size(hist_max);
k = 0;
max_1 = 0;
max_2 = 0;
for i=1:n
    if k<a_hist(hist_max(i))
        k = a_hist(hist_max(i));
        max_1 = hist_max(i);
    end 
end
temp = a_hist(max_1);
a_hist(max_1) = 0;
k = 0;
for i=1:n
    if k<a_hist(hist_max(i))
        k = a_hist(hist_max(i));
        max_2 = hist_max(i);
    end
end
a_hist(max_1) = temp;
if max_1>max_2
    k = max_1;
    max_1 = max_2;
    max_2 = k;
end
T = max_1;
k = a_hist(max_1);
for i=max_1:max_2
    if k>a_hist(i)
        k = a_hist(i);
        T = i;
    end
end
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 
[m,n] = size(bar_Gray); %求灰度图的大小
for i=1:m        %对图像进行二值化处理
    for j=1:n
        if bar_Gray(i,j)>T    %选择适当的阈值进行二值化处理
            bar_10(i,j) = 1;
        else
            bar_10(i,j) = 0;
        end
    end
end
 

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

完整代码添加QQ1575304183

以上是关于VB 识别条形码的主要内容,如果未能解决你的问题,请参考以下文章

VB.NET如何操作条码扫描枪,如何设置,如何进行条形码的设置及打印

如何识别条形码?

公司有一个vb 软件是通过扫描枪扫描条形码获得信息,但用扫描枪扫描后Text中的条形码一闪就没了。

关于条形码识别,Java实现

如何识别条形码

如何用Java实现条形码识别技术