function varargout = mycode(varargin)
% MYCODE M-file for mycode.fig
% MYCODE, by itself, creates a new MYCODE or raises the existing
% singleton*.
%
% H = MYCODE returns the handle to a new MYCODE or the handle to
% the existing singleton*.
%
% MYCODE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYCODE.M with the given input arguments.
%
% MYCODE('Property','Value',...) creates a new MYCODE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before mycode_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to mycode_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 mycode
% Last Modified by GUIDE v2.5 29-Dec-2008 15:39:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mycode_OpeningFcn, ...
'gui_OutputFcn', @mycode_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 mycode is made visible.
function mycode_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 mycode (see VARARGIN)
% Choose default command line output for mycode
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mycode wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = mycode_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 button press in openimage.
function openimage_Callback(hObject, eventdata, handles)
% hObject handle to openimage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit1,'String',' ');
set(handles.T,'String','120 ');
[FileName,PathName] = uigetfile('*.*','ì');
FileFullName=strcat(PathName,FileName);
handles.savename=strcat(PathName,'new',FileName);
Image=imread(FileFullName);
imshow(Image,[]);
Image = rgb2gray(Image);
handles.Image=Image;
guidata(gcbo,handles);
% --- 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)
stringT=str2num(get(handles.T,'String'));
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_Gray=handles.Image;
[m,n] = size(bar_Gray); %求灰度图的大小
for i=1:m %对图像进行二值化处理
for j=1:n
if bar_Gray(i,j)>stringT %选择适当的阈值进行二值化处理
bar_10(i,j) = 1;
else
bar_10(i,j) = 0;
end
end
end
l = 0; %检测59根条形码
for i=1:m
k = 1;
l = l+1;
for j=1:n-1
if bar_10(i,j)~=bar_10(i,j+1) %比较同一行相邻两点的颜色是否一致
%bar_x(l,k) = i;
bar_y(l,k) = j; %记录转折点的纵坐标
k = k+1; %准备记录下一个数据点
end
if k>61 %点数大于60,该行应该删掉
l = l-1;
break
end
end
if k<61 %点数小于60,该行应该删掉
l = l-1;
end
end
[m,n] = size(bar_y);
if m<=1 %查看条形码是否有效
code = '0';
%fprintf(1,'GameOver!\n');
set(handles.edit1,'String','GameOver!');
return
end
for i=1:m %计算每根条形码的宽度
for j=1:n-1
bar_num(i,j) = bar_y(i,j+1) - bar_y(i,j);
if bar_num(i,j)<0
bar_num(i,j) = 0;
end
end
end
bar_sum = sum(bar_num)/m; %求每根条形码宽度的平均值
k = 0;
for i=1:59 %计算59根条形码的总宽度
k = k + bar_sum(i);
end
k = k/95; %计算单位条形码的宽度
for i=1:59 %计算每根条形码所占位数
bar_int(i) = round(bar_sum(i)/k);
end
k = 1;
for i=1:59 %将条形码转换成二进制数
if rem(i,2)
for j=1:bar_int(i) %黑色条用1表示
bar_01(k) = 1;
k = k+1;
end
else
for j=1:bar_int(i) %白色条用0表示
bar_01(k) = 0;
k = k+1;
end
end
end
if ((bar_01(1)&&~bar_01(2)&&bar_01(3))... %判断起始符是否正确
&&(~bar_01(46)&&bar_01(47)&&~bar_01(48)&&bar_01(49)&&~bar_01(50))... %判断中间分隔符是否正确
&&(bar_01(95)&&~bar_01(94)&&bar_01(93))) %判断终止符是否正确
l = 1;
for i=1:6 %将左侧42位二进制数转换为十进制数
bar_left(l) = 0;
for k=1:7
bar_left(l) = bar_left(l)+bar_01(7*(i-1)+k+3)*(2^(7-k));
end
l = l+1;
end
l = 1;
for i=1:6 %将右侧42位二进制数转换为十进制数
bar_right(l) = 0;
for k=1:7
bar_right(l) = bar_right(l)+bar_01(7*(i+6)+k+1)*(2^(7-k));
k = k-1;
end
l = l+1;
end
else
set(handles.edit1,'String','error!');
return
end
num_bar = '';
num_first = 0;
first = 2;
for i=1:6 %从左边数据编码表中查出条形码编码数字
for j=0:1
for k=0:9
if bar_left(i)==check_left(j+1,k+1)
num_bar = strcat(num_bar , num2str(k));
switch first %记录左边数据的奇偶顺序
case 2
first = j;
break;
case 1
num_first = num_first + j*(2^(6-i));
break;
case 0
num_first = num_first + (~j)*(2^(6-i));
break;
otherwise
break;
end
end
end
end
end
for i=1:6 %从右边数据编码表中查出条形码编码数字
for j=0:9
if bar_right(i)==check_right(j+1)
num_bar = strcat(num_bar , num2str(j));
end
end
end
for i=0:9 %从第一位数据编码表中查出第一位数字
if num_first==first_num(i+1)
num_bar = strcat(num2str(i) , num_bar);
break;
end
end
if numel(num_bar)~=13
set(handles.edit1,'String','Please Turn It Around!');
return
end
check_code = 0;
for i=1:12 %计算校验码