function varargout = ean13(varargin)
% EAN13 MATLAB code for ean13.fig
% EAN13, by itself, creates a new EAN13 or raises the existing
% singleton*.
%
% H = EAN13 returns the handle to a new EAN13 or the handle to
% the existing singleton*.
%
% EAN13('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EAN13.M with the given input arguments.
%
% EAN13('Property','Value',...) creates a new EAN13 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ean13_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ean13_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 ean13
% Last Modified by GUIDE v2.5 04-Apr-2018 00:26:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ean13_OpeningFcn, ...
'gui_OutputFcn', @ean13_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 ean13 is made visible.
function ean13_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 ean13 (see VARARGIN)
% Choose default command line output for ean13
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ean13 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ean13_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 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)
%输入:X: 1*13行向量。所有位0~9
%输出:EAN13条形码,并存图片
%
%
%【EAN13条形码生成器】
%
% EAN13编码规则(1)——数字含义
%
% EAN13编码中13个数字的意义
% N1 N2 N3 N4 N5 N6 N7 N8 N9 N10 N11 N12 C
% 6 9 3 2 7 2 2 3 3 5 8 5 3
% | | | | | | | | | | | | |
%导入值 左侧数据 右侧数据 校正符
%校正符自检公式:Ctemp=(N1+N3+N5+N7+N9+N11)+(N2+N4+N6+N8+N10+N12)*3
% C=10-rem(Ctemp,10);
% 如果C=10,C取0
%
%注:导入值不参与编码,导入值觉得左侧数据的编码方式
%
% EAN13编码规则(2)——符号编码
% 0为空白,1为线条
% 第一部分 第二部分 第三部分 第四部分 第五部分 第六部分 第七部分 第八部分
% 左侧空白 左侧护线 左侧数据 中间护线 右侧数据 校正符 右侧护线 右侧空白
% 11bit 3bit 42bit 5bit 35bit 7bit 3bit 7bit
%
%左侧空白编码:1 1 1 1 1 1 1 1 1 1 1
%左侧护线编码:1 0 1
%左侧数据编码:ABBBAA(依据编码规则而定)
%中间护线编码:0 1 0 1 0
%右侧数据编码:CCCCC
%校正符编码:C
%右侧护线编码:1 0 1
%右侧空白编码:1 1 1 1 1 1 1
%
%编码方式
% 0 AAAAAA
% 1 AABABB
% 2 AABBAB
% 3 AABBBA
% 4 ABAABB
% 5 ABBAAB
% 6 ABBBAA
% 7 ABABAB
% 8 ABABBA
% 9 ABBABA
%
% 0空白 1线条
%数字 A类编码 B类编码 C类编码
% 0 0001101 0100111 1110010
% 1 0011001 0110011 1100110
% 2 0010011 0011011 1101100
% 3 0111101 0100001 1000010
% 4 0100011 0011101 1011100
% 5 0110001 0111001 1001110
% 6 0101111 0000101 1010000
% 7 0111011 0010001 1000100
% 8 0110111 0001001 1001000
% 9 0001011 0010111 1110100
%% 输入
str1 = get(handles.edit1,'String'); %取出左侧框中字符串存入str1
len = length(str1);
X = double(str1);
for i = 1:len
X(i) = str2double(char(str1(i)));
end
if numel(X)~=13 %当输入时,必须是1*13的矩阵,之后检查校验码是否正确
s = '!!!输入变量必须为1*13矩阵';
set (handles.edit2,'String',s);
end
C=X(13);
CX=10-rem(sum(X(1:2:12)+X(2:2:12)*3),10); %rem 取余数
if CX==10
CX=0;
end
if C~=CX
s = sprintf('校验码错误!!!应为:%d',CX);
set (handles.edit2,'String',s);
X(13) = CX;
end
%% 字符集
Cleft_A=['0001101'; '0011001'; '0010011'; '0111101'; '0100011'; '0110001'; '0101111'; '0111011'; '0110111'; '0001011']; %A类编码
Cleft_B=['0100111'; '0110011'; '0011011'; '0100001'; '0011101'; '0111001'; '0000101'; '0010001'; '0001001'; '0010111']; %B类编码
Cright=['1110010'; '1100110'; '1101100'; '1000010'; '1011100'; '1001110'; '1010000'; '1000100'; '1001000'; '1110100']; %C类编码
%% 左侧空白
A1=zeros(1,11);
%% 起始符
A2=[1 0 1];
%% 左侧数据 根据编码规则选择 编码顺序 (X(1)是编码符)
switch(X(1))
case 0 % 0 AAAAAA
temp=[Cleft_A(X(2)+1,:),Cleft_A(X(3)+1,:),Cleft_A(X(4)+1,:),... % 从0开始编码,所以要+1
Cleft_A(X(5)+1,:),Cleft_A(X(6)+1,:),Cleft_A(X(7)+1,:)];
case 1 % 1 AABABB
temp=[Cleft_A(X(2)+1,:),Cleft_A(X(3)+1,:),Cleft_B(X(4)+1,:),...
Cleft_A(X(5)+1,:),Cleft_B(X(6)+1,:),Cleft_B(X(7)+1,:)];
case 2 % 2 AABBAB
temp=[Cleft_A(X(2)+1,:),Cleft_A(X(3)+1,:),Cleft_B(X(4)+1,:),...
Cleft_B(X(5)+1,:),Cleft_A(X(6)+1,:),Cleft_B(X(7)+1,:)];
case 3 % 3 AABBBA
temp=[Cleft_A(X(2)+1,:),Cleft_A(X(3)+1,:),Cleft_B(X(4)+1,:),...
Cleft_B(X(5)+1,:),Cleft_B(X(6)+1,:),Cleft_A(X(7)+1,:)];
case 4 % 4 ABAABB
temp=[Cleft_A(X(2)+1,:),Cleft_B(X(3)+1,:),Cleft_A(X(4)+1,:),...
Cleft_A(X(5)+1,:),Cleft_B(X(6)+1,:),Cleft_B(X(7)+1,:)];
case 5 % 5 ABBAAB
temp=[Cleft_A(X(2)+1,:),Cleft_B(X(3)+1,:),Cleft_B(X(4)+1,:),...
Cleft_A(X(5)+1,:),Cleft_A(X(6)+1,:),Cleft_B(X(7)+1,:)];
case 6 % 6 ABBBAA
temp=[Cleft_A(X(2)+1,:),Cleft_B(X(3)+1,:),Cleft_B(X(4)+1,:),...
Cleft_B(X(5)+1,:),Cleft_A(X(6)+1,:),Cleft_A(X(7)+1,:)];
case 7 % 7 ABABAB
temp=[Cleft_A(X(2)+1,:),Cleft_B(X(3)+1,