关于条形码识别,Java实现

Posted

tags:

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

项目有这样的需求:条形码已经用扫描仪扫进电脑并存为jpg或其它格式的图片文件,要求程序可以对此条形码图片进行识别,程序的输出就是该条形码对应的串,用java实现,求高手指点……
简单的说,就是把条形码解释成它下面的那一串数字……
注:不是用条形码扫描枪,只是普通扫描仪扫成的jpg或是其它格式的图片文件!!!

条形码是按条的宽度和间隔来表示信息,分析图片的象素,通过象素的长度来判断条的宽度和间隔,根据一定的标准来转换成字符串……但是,识别的错误率会相当高了,因为条形码图片是通过扫描仪扫出来的,本身就会有误差! 参考技术A 不知道你要生成的串是要把条形码解析成字符串 还是 数字...
条形码是按条的宽度和间隔来表示信息,读图片相素吧 通过相素的长度来判断条的宽度和间隔.

matab基于形态学实现二值化条形码识别

一、简介
1 图像的二值化的基本原理
图像的二值化处理就是讲图像上的点的灰度置为0或255,也就是讲整个图像呈现出明显的黑白效果。即将256个亮度等级的灰度图像通过适当的阀值选取而获得仍然可以反映图像整体和局部特征的二值化图像。在数字图像处理中,二值图像占有非常重要的地位,特别是在实用的图像处理中,以二值图像处理实现而构成的系统是很多的,要进行二值图像的处理与分析,首先要把灰度图像二值化,得到二值化图像,这样子有利于再对图像做进一步处理时,图像的集合性质只与像素值为0或255的点的位置有关,不再涉及像素的多级值,使处理变得简单,而且数据的处理和压缩量小。为了得到理想的二值图像,一般采用封闭、连通的边界定义不交叠的区域。所有灰度大于或等于阀值的像素被判定为属于特定物体,其灰度值为255表示,否则这些像素点被排除在物体区域以外,灰度值为0,表示背景或者例外的物体区域。如果某特定物体在内部有均匀一致的灰度值,并且其处在一个具有其他等级灰度值的均匀背景下,使用阀值法就可以得到比较的分割效果。如果物体同背景的差别表现不在灰度值上(比如纹理不同),可以将这个差别特征转换为灰度的差别,然后利用阀值选取技术来分割该图像。动态调节阀值实现图像的二值化可动态观察其分割图像的具体结果。

二值化是图像分割的一种方法。在二值化图象的时候把大于某个临界灰度值的像素灰度设为灰度極大值,把小于这个值的像素灰度设为灰度極小值,从而实现二值化。
根据阈值选取的不同,二值化的算法分为固定阈值和自适应阈值。 比较常用的二值化方法则有:双峰法、P参数法、迭代法和OTSU法等。

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('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


    check_code = 10 - check_code;
end

if check_code==str2num(num_bar(13)) %判断校验码是否正确
    code = num_bar
else
  %  fprintf(1,'Please Turn It Around!\\n');
  str='Please Turn It Around!';
    set(handles.text4,'string',str);
    return
end
%c=num2str(code);
set(handles.text1,'string',code);
set(handles.text4,'string','');
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text1,'string','');
[filename,pathname]=...
    uigetfile({'*.jpg';'*.tif';'*.bmp';'*.gif';'*.*'},'选择图片');
if pathname == 0
    return;
end
str=[pathname filename];
global im;
im=imread(str);
axes(handles.axes1);
imshow(im);


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1


wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

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

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

java识别条形码api都有哪些

用java程序如何读取图书条形码

条形码java代码问题 菜鸟级问题

怎么样用java生产ITF条形码。。

java 实现图片的文字识别