教你使用Matlab制作圖形驗(yàn)證碼生成器(app designer)
突然發(fā)現(xiàn)cla函數(shù)也可以應(yīng)用到app designer控件上,因而對(duì)部分內(nèi)容做出更改,將繪制隱藏像素刷新的方式改為用cla
原
hold(acAxes,'off');
image(acAxes,[-1,0],[-1,0],ones(1,1,3),'visible','off');
hold(acAxes,'on');
delete(findobj('tag','ax'));新
cla(acAxes) cla(ax)
0效果


1字符圖片生成
如果我們單純的用text繪制圖形,就無(wú)法做到效果中符號(hào)和符號(hào)邊緣兩個(gè)顏色,也無(wú)法做到更大程度的變形,因此我們需要將字符轉(zhuǎn)換為矩陣形式。
想要實(shí)現(xiàn)也非常簡(jiǎn)單,我們只需要?jiǎng)?chuàng)建一個(gè)不可視的fig,在其上繪制字符,保存fig為png格式圖片,再通過(guò)imread讀取圖片,就能獲得字符矩陣:
第一次運(yùn)行程序因?yàn)橐勺址麍D片因而會(huì)比較慢,再次運(yùn)行就可以讀取之前已經(jīng)生成過(guò)的圖片啦:
% 字符圖片矩陣構(gòu)造 ========================================================
% 以下為字符圖片創(chuàng)建過(guò)程
% 原理為構(gòu)造隱藏的figure和axes
% 在其上用text繪制字符并保存figure為圖片
% 導(dǎo)入圖片
if ~exist('Materials','dir')
mkdir('Materials');
end
fig=figure('units','pixels',...
'position',[20 80 200 200],...
'Numbertitle','off',...
'Color',[1 1 1],...
'resize','off',...
'visible','off',...
'menubar','none');
ax=axes('Units','pixels',...
'parent',fig,...
'Color',[1 1 1],...
'Position',[0 0 200 200],...
'XLim',[0 200],...
'YLim',[0 200],...
'XColor',[1 1 1],...
'YColor',[1 1 1]);
strPic{length(strElement)}=[];
for i=1:length(strElement)
% 若是不存在該字符圖片則生成,否則直接導(dǎo)入
if ~exist(['.\Materials\',strElement(i),'.png'],'file')
delete(findobj('tag','textStr'));
text(ax,100,100,strElement(i),'HorizontalAlignment',...
'center','FontSize',140,'tag','textStr','FontWeigh','bold')
saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存圖片
end
tempPic=imread(['.\Materials\',strElement(i),'.png']); % 讀取圖片
strPic{i}=imresize(tempPic,[150,150]); % 重新調(diào)整圖片大小
end

2刷新按鈕生成
大家可以看到這個(gè)按鈕的樣式與大部分按鈕不同:

實(shí)際上這是一個(gè)HTML控件,輸入html文件的位置就可以形成類(lèi)似嵌入頁(yè)面的效果:
acHTML=uihtml(acFigure); acHTML.HTMLSource='.\Materials\textbtn.html'; acHTML.DataChangedFcn=@refresh; acHTML.Position=[300 50 88 26];
如代碼所示,我們導(dǎo)入的是Materials文件夾內(nèi)的textbtn.html文件
textbtn.html長(zhǎng)這樣:
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<script type="text/javascript">
function setup(htmlComponent) {
document.getElementById("btnonclink").addEventListener("click", function(event) {
htmlComponent.Data="test";
});
}
</script>
</head>
<body>
<a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a>
</body>
</html>當(dāng)然為了防止大家不會(huì)創(chuàng)建,我在m文件中寫(xiě)了一段能夠自動(dòng)創(chuàng)建html文件的代碼,原理就是將字符串信息寫(xiě)入txt,再將txt文件后綴改為html:
% .html文件自動(dòng)生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
htmlContent={'<!DOCTYPE html><html><head><meta charset=UTF-8>';
'<script type="text/javascript">';
'function setup(htmlComponent){';
'document.getElementById("btnonclink").addEventListener("click",function(event){';
'htmlComponent.Data="test";});}</script></head>';
'<body><a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a></body></html>'};
if ~exist('.\Materials\textbtn.html','file')
fid=fopen('.\Materials\textbtn.txt','w');
for i=1:length(htmlContent)
fprintf(fid,'%s\r\n',htmlContent{i});
end
fclose(fid);
copyfile('.\Materials\textbtn.txt','.\Materials\textbtn.html');
delete('.\Materials\textbtn.txt')
end
3圖片處理
3.1圖像任意方向拉伸
這部分原理就是將圖像旋轉(zhuǎn)一定角度后,在豎直方向進(jìn)行拉伸后再旋轉(zhuǎn)回去

3.2字符邊緣
這部分原理將字符均值濾波后,把不完全是黑色的部分設(shè)置為灰色,后期再設(shè)置為其他顏色

3.3圖像處理部分代碼
randColor=@()randi([0,200],[1,3]); % 生成隨機(jī)顏色的匿名函數(shù)
% 從圖像集合中提取圖像
tPic=strPic{randiNums(ii)};
tPic=tPic(:,:,1);
% 將圖像旋轉(zhuǎn)-拉伸-旋轉(zhuǎn)
randiTheta1=randi([0,90]);
randiTheta2=randi([-30,30]);
randiLenth=randi([0,70]);
tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
tPic=imresize(tPic,[150+randiLenth,150]);
tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
% 將圖像邊緣進(jìn)行模糊,并將模糊的部分?jǐn)?shù)值設(shè)置為150
tPic=255-imfilter(tPic,I_5);
tPic(tPic~=0&tPic~=255)=150;
% 為符號(hào)和符號(hào)邊緣賦予不同顏色
tempColor1=randColor();tempColor2=randColor();
tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
tempPic_3=uint8(zeros([size(tPic),3]));
tempPic_3(:,:,1)=tempPicR;
tempPic_3(:,:,2)=tempPicG;
tempPic_3(:,:,3)=tempPicB;
4線(xiàn)條和散點(diǎn)生成
散點(diǎn)就是生成一堆隨機(jī)位置點(diǎn)和一些隨機(jī)顏色后用scatter函數(shù)繪制,線(xiàn)條是生成散點(diǎn)后使用’spline’插值方法插值成線(xiàn)后再繪制:
randColor=@()randi([0,200],[1,3]); % 生成隨機(jī)顏色的匿名函數(shù)
randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n個(gè)隨機(jī)顏色的匿名函數(shù)
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n個(gè)隨機(jī)點(diǎn)的匿名函數(shù)
% 繪制散點(diǎn)
pPonintsNum=randi([6,10]);
pPoints=randPoint_n(pPonintsNum);
pPointsColor=randColor_n(pPonintsNum);
scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
'CData',pPointsColor,'AlphaData',0.6)
% 繪制線(xiàn)
lPonintsNum=randi([5,7]);
lPoints=randPoint_n(lPonintsNum);
lPointsColor=[randColor()./255,0.6];
x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
5關(guān)于圖像存儲(chǔ)
由于目前版本uifigure還不支持存儲(chǔ)為圖像,因此我們繪制圖像是在figure和uifigure分別繪制一遍,其中figure依舊是不可見(jiàn)狀態(tài),主要用于將圖片驗(yàn)證碼保存為png格式,可以在完整代碼中看出這一點(diǎn)。
同時(shí),本程序的設(shè)置為,每次刷新圖形驗(yàn)證碼,都會(huì)刷新當(dāng)前文件夾下authCode.png為最新的驗(yàn)證碼,如需要保存請(qǐng)及時(shí)將其改名或復(fù)制另存:

6關(guān)于驗(yàn)證碼對(duì)比
首先就是需要提取框內(nèi)驗(yàn)證碼:
codeInPut=acEditField.Value;
因?yàn)槲覀兊尿?yàn)證碼字符都是大寫(xiě)的,將輸入的文本用upper函數(shù)變?yōu)榇髮?xiě):
codeInPut=upper(codeInPut);
同時(shí)我們因?yàn)?和O長(zhǎng)的太像,所以不對(duì)其進(jìn)行區(qū)分,直接將輸入的驗(yàn)證碼中的0改為O:
codeInPut(codeInPut=='0')='O';
之后就能夠用strcmp函數(shù)將當(dāng)前驗(yàn)證碼和輸入的驗(yàn)證碼進(jìn)行對(duì)比:
if strcmp(codeInPut,authCode)
msgbox('驗(yàn)證碼正確')
else
msgbox('驗(yàn)證碼錯(cuò)誤')
end
7完整代碼
function authCode
strElement=char([49:57,65:90]); % 1-9,A-Z的字符
randColor=@()randi([0,200],[1,3]); % 生成隨機(jī)顏色的匿名函數(shù)
randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n個(gè)隨機(jī)顏色的匿名函數(shù)
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n個(gè)隨機(jī)點(diǎn)的匿名函數(shù)
global authCode; % 全局變量:驗(yàn)證碼
% 字符圖片矩陣構(gòu)造 ========================================================
% 以下為字符圖片創(chuàng)建過(guò)程
% 原理為構(gòu)造隱藏的figure和axes
% 在其上用text繪制字符并保存figure為圖片
% 導(dǎo)入圖片
if ~exist('Materials','dir')
mkdir('Materials');
end
fig=figure('units','pixels',...
'position',[20 80 200 200],...
'Numbertitle','off',...
'Color',[1 1 1],...
'resize','off',...
'visible','off',...
'menubar','none');
ax=axes('Units','pixels',...
'parent',fig,...
'Color',[1 1 1],...
'Position',[0 0 200 200],...
'XLim',[0 200],...
'YLim',[0 200],...
'XColor',[1 1 1],...
'YColor',[1 1 1]);
strPic{length(strElement)}=[];
for i=1:length(strElement)
% 若是不存在該字符圖片則生成,否則直接導(dǎo)入
if ~exist(['.\Materials\',strElement(i),'.png'],'file')
delete(findobj('tag','textStr'));
text(ax,100,100,strElement(i),'HorizontalAlignment',...
'center','FontSize',140,'tag','textStr','FontWeigh','bold')
saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存圖片
end
tempPic=imread(['.\Materials\',strElement(i),'.png']); % 讀取圖片
strPic{i}=imresize(tempPic,[150,150]); % 重新調(diào)整圖片大小
end
% 更改fig ax樣式,為方便后期驗(yàn)證碼存儲(chǔ)
fig.Position=[100 100 200 70];
ax.Position=[1 1 199.5 70];
ax.XTick=[];
ax.YTick=[];
ax.XLim=[0,200];
ax.YLim=[0,70];
ax.XColor=[0.7 0.7 0.7];
ax.YColor=[0.7 0.7 0.7];
ax.Box='on';
ax.YDir='reverse';
hold(ax,'on');
% APP designer窗口構(gòu)建 ====================================================
acFigure=uifigure();
acFigure.Position=[100 100 370 90];
acFigure.Name='authCode';
acFigure.Resize='off';
acAxes=uiaxes(acFigure);
acAxes.Position=[10 10 200 70];
acAxes.XTick=[];
acAxes.YTick=[];
acAxes.XLim=[0,200];
acAxes.YLim=[0,70];
acAxes.XColor=[0.7 0.7 0.7];
acAxes.YColor=[0.7 0.7 0.7];
acAxes.Box='on';
acAxes.YDir='reverse';
hold(acAxes,'on');
acEditField=uieditfield(acFigure,'text');
acEditField.Position=[220 52 70 23];
acEditField.FontSize=16;
acEditField.FontWeight='bold';
acEditField.FontColor=[0.3,0.3,0.3];
% .html文件自動(dòng)生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
htmlContent={'<!DOCTYPE html><html><head><meta charset=UTF-8>';
'<script type="text/javascript">';
'function setup(htmlComponent){';
'document.getElementById("btnonclink").addEventListener("click",function(event){';
'htmlComponent.Data="test";});}</script></head>';
'<body><a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a></body></html>'};
if ~exist('.\Materials\textbtn.html','file')
fid=fopen('.\Materials\textbtn.txt','w');
for i=1:length(htmlContent)
fprintf(fid,'%s\r\n',htmlContent{i});
end
fclose(fid);
copyfile('.\Materials\textbtn.txt','.\Materials\textbtn.html');
delete('.\Materials\textbtn.txt')
end
acHTML=uihtml(acFigure);
acHTML.HTMLSource='.\Materials\textbtn.html';
acHTML.DataChangedFcn=@refresh;
acHTML.Position=[300 50 88 26];
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
acButton=uibutton(acFigure);
acButton.Position=[220 15 140 30];
acButton.Text='確 認(rèn) 驗(yàn) 證 碼';
acButton.BackgroundColor=[0.31 0.58 0.80];
acButton.FontColor=[1 1 1];
acButton.FontWeight='bold';
acButton.FontSize=14;
acButton.ButtonPushedFcn=@verify;
% 回調(diào)函數(shù) ================================================================
function refresh(~,~)
cla(acAxes)
cla(ax)
I_5=fspecial('average',[5,5]); % 5*5均值濾波模板
randiNums=randi([1,length(strElement)],[1,4]);
authCode=strElement(randiNums); % 驗(yàn)證碼
disp(authCode)
for ii=1:4
tPic=strPic{randiNums(ii)};
tPic=tPic(:,:,1);
%tempPic(tempPic<250)=150;
% 將圖像旋轉(zhuǎn)-拉伸-旋轉(zhuǎn)
randiTheta1=randi([0,90]);
randiTheta2=randi([-30,30]);
randiLenth=randi([0,70]);
tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
tPic=imresize(tPic,[150+randiLenth,150]);
tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
% 將圖像邊緣進(jìn)行模糊,并將模糊的部分?jǐn)?shù)值設(shè)置為150
tPic=255-imfilter(tPic,I_5);
tPic(tPic~=0&tPic~=255)=150;
% 為符號(hào)和符號(hào)邊緣賦予不同顏色
tempColor1=randColor();tempColor2=randColor();
tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
tempPic_3=uint8(zeros([size(tPic),3]));
tempPic_3(:,:,1)=tempPicR;
tempPic_3(:,:,2)=tempPicG;
tempPic_3(:,:,3)=tempPicB;
% 顯示圖片
image(acAxes,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
[-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
image(ax,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
[-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
end
% 繪制散點(diǎn)
pPonintsNum=randi([6,10]);
pPoints=randPoint_n(pPonintsNum);
pPointsColor=randColor_n(pPonintsNum);
scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
'CData',pPointsColor,'AlphaData',0.6)
scatter(ax,pPoints(:,1),pPoints(:,2),6,'filled',...
'CData',pPointsColor,'AlphaData',0.6)
% 繪制線(xiàn)
lPonintsNum=randi([5,7]);
lPoints=randPoint_n(lPonintsNum);
lPointsColor=[randColor()./255,0.6];
x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
plot(ax,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
saveas(fig,'.\authCode.png');
end
refresh()
function verify(~,~)
codeInPut=acEditField.Value;
codeInPut=upper(codeInPut);
codeInPut(codeInPut=='0')='O';
if strcmp(codeInPut,authCode)
msgbox('驗(yàn)證碼正確')
else
msgbox('驗(yàn)證碼錯(cuò)誤')
end
end
end
注:程序第一次運(yùn)行由于有html文件及png文件需要生成,因而會(huì)比較慢,之后的運(yùn)行速度會(huì)快很多。
對(duì)于以前版本沒(méi)有uihtml控件可以先嘗試如下代碼:

這里用正常按鈕替換了uihtml控件
function authCode2
strElement=char([49:57,65:90]); % 1-9,A-Z的字符
randColor=@()randi([0,200],[1,3]); % 生成隨機(jī)顏色的匿名函數(shù)
randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n個(gè)隨機(jī)顏色的匿名函數(shù)
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n個(gè)隨機(jī)點(diǎn)的匿名函數(shù)
global authCode; % 全局變量:驗(yàn)證碼
% 字符圖片矩陣構(gòu)造 ========================================================
% 以下為字符圖片創(chuàng)建過(guò)程
% 原理為構(gòu)造隱藏的figure和axes
% 在其上用text繪制字符并保存figure為圖片
% 導(dǎo)入圖片
if ~exist('Materials','dir')
mkdir('Materials');
end
fig=figure('units','pixels',...
'position',[20 80 200 200],...
'Numbertitle','off',...
'Color',[1 1 1],...
'resize','off',...
'visible','off',...
'menubar','none');
ax=axes('Units','pixels',...
'parent',fig,...
'Color',[1 1 1],...
'Position',[0 0 200 200],...
'XLim',[0 200],...
'YLim',[0 200],...
'XColor',[1 1 1],...
'YColor',[1 1 1]);
strPic{length(strElement)}=[];
for i=1:length(strElement)
% 若是不存在該字符圖片則生成,否則直接導(dǎo)入
if ~exist(['.\Materials\',strElement(i),'.png'],'file')
delete(findobj('tag','textStr'));
text(ax,100,100,strElement(i),'HorizontalAlignment',...
'center','FontSize',140,'tag','textStr','FontWeigh','bold')
saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存圖片
end
tempPic=imread(['.\Materials\',strElement(i),'.png']); % 讀取圖片
strPic{i}=imresize(tempPic,[150,150]); % 重新調(diào)整圖片大小
end
% 更改fig ax樣式,為方便后期驗(yàn)證碼存儲(chǔ)
fig.Position=[100 100 200 70];
ax.Position=[1 1 199.5 70];
ax.XTick=[];
ax.YTick=[];
ax.XLim=[0,200];
ax.YLim=[0,70];
ax.XColor=[0.7 0.7 0.7];
ax.YColor=[0.7 0.7 0.7];
ax.Box='on';
ax.YDir='reverse';
hold(ax,'on');
% APP designer窗口構(gòu)建 ====================================================
acFigure=uifigure();
acFigure.Position=[100 100 370 90];
acFigure.Name='authCode';
acFigure.Resize='off';
acAxes=uiaxes(acFigure);
acAxes.Position=[10 10 200 70];
acAxes.XTick=[];
acAxes.YTick=[];
acAxes.XLim=[0,200];
acAxes.YLim=[0,70];
acAxes.XColor=[0.7 0.7 0.7];
acAxes.YColor=[0.7 0.7 0.7];
acAxes.Box='on';
acAxes.YDir='reverse';
hold(acAxes,'on');
acEditField=uieditfield(acFigure,'text');
acEditField.Position=[220 52 70 23];
acEditField.FontSize=16;
acEditField.FontWeight='bold';
acEditField.FontColor=[0.3,0.3,0.3];
acfreshBtn=uibutton(acFigure);
acfreshBtn.Text='看不清?';
acfreshBtn.ButtonPushedFcn=@refresh;
acfreshBtn.Position=[300 50 60 27];
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
acButton=uibutton(acFigure);
acButton.Position=[220 15 140 30];
acButton.Text='確 認(rèn) 驗(yàn) 證 碼';
acButton.BackgroundColor=[0.31 0.58 0.80];
acButton.FontColor=[1 1 1];
acButton.FontWeight='bold';
acButton.FontSize=14;
acButton.ButtonPushedFcn=@verify;
% 回調(diào)函數(shù) ================================================================
function refresh(~,~)
cla(acAxes)
cla(ax)
% hold(acAxes,'off');
% image(acAxes,[-1,0],[-1,0],ones(1,1,3),'visible','off');
% hold(acAxes,'on');
% delete(findobj('tag','ax'));
I_5=fspecial('average',[5,5]); % 5*5均值濾波模板
randiNums=randi([1,length(strElement)],[1,4]);
authCode=strElement(randiNums); % 驗(yàn)證碼
disp(authCode)
for ii=1:4
tPic=strPic{randiNums(ii)};
tPic=tPic(:,:,1);
%tempPic(tempPic<250)=150;
% 將圖像旋轉(zhuǎn)-拉伸-旋轉(zhuǎn)
randiTheta1=randi([0,90]);
randiTheta2=randi([-30,30]);
randiLenth=randi([0,70]);
tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
tPic=imresize(tPic,[150+randiLenth,150]);
tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
% 將圖像邊緣進(jìn)行模糊,并將模糊的部分?jǐn)?shù)值設(shè)置為150
tPic=255-imfilter(tPic,I_5);
tPic(tPic~=0&tPic~=255)=150;
% 為符號(hào)和符號(hào)邊緣賦予不同顏色
tempColor1=randColor();tempColor2=randColor();
tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
tempPic_3=uint8(zeros([size(tPic),3]));
tempPic_3(:,:,1)=tempPicR;
tempPic_3(:,:,2)=tempPicG;
tempPic_3(:,:,3)=tempPicB;
% 顯示圖片
image(acAxes,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
[-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
image(ax,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
[-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
end
% 繪制散點(diǎn)
pPonintsNum=randi([6,10]);
pPoints=randPoint_n(pPonintsNum);
pPointsColor=randColor_n(pPonintsNum);
scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
'CData',pPointsColor,'AlphaData',0.6)
scatter(ax,pPoints(:,1),pPoints(:,2),6,'filled',...
'CData',pPointsColor,'AlphaData',0.6)
% 繪制線(xiàn)
lPonintsNum=randi([5,7]);
lPoints=randPoint_n(lPonintsNum);
lPointsColor=[randColor()./255,0.6];
x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
plot(ax,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
saveas(fig,'.\authCode.png');
end
refresh()
function verify(~,~)
codeInPut=acEditField.Value;
codeInPut=upper(codeInPut);
codeInPut(codeInPut=='0')='O';
if strcmp(codeInPut,authCode)
msgbox('驗(yàn)證碼正確')
else
msgbox('驗(yàn)證碼錯(cuò)誤')
end
end
end
以上就是教你使用Matlab制作圖形驗(yàn)證碼生成器(app designer)的詳細(xì)內(nèi)容,更多關(guān)于Matlab圖形驗(yàn)證碼生成器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)的猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)的猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C語(yǔ)言指針入門(mén)的簡(jiǎn)單實(shí)例教程
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言指針入門(mén)的簡(jiǎn)單實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Qt如何實(shí)現(xiàn)輸入框@聯(lián)系人的@檢測(cè)的示例
本文主要介紹了Qt如何實(shí)現(xiàn)輸入框@聯(lián)系人的@檢測(cè)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
C++利用靜態(tài)成員或類(lèi)模板構(gòu)建鏈表的方法講解
這篇文章主要介紹了C++利用靜態(tài)成員或類(lèi)模板構(gòu)建鏈表的方法講解,鏈表是基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),而在C++中構(gòu)件單鏈表還是稍顯復(fù)雜,需要的朋友可以參考下2016-04-04
C語(yǔ)言輸入一個(gè)數(shù)判斷是否為素?cái)?shù)的多種方法
素?cái)?shù)是只能被1和它自己本身整除,不能被其他自然數(shù)整除的大于1的正整數(shù),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言輸入一個(gè)數(shù)判斷是否為素?cái)?shù)的多種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
VC中使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序簡(jiǎn)明教程
這篇文章主要介紹了VC中使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序的方法,結(jié)合實(shí)例形式詳細(xì)講述了ADO的原理及VC使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
C++基礎(chǔ)入門(mén)篇之強(qiáng)制轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于C++基礎(chǔ)入門(mén)篇之強(qiáng)制轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C++中字符串全排列算法及next_permutation原理詳解
這篇文章主要為大家詳細(xì)介紹了C++中字符串全排列(遞歸法)和(迭代法)以及next_permutation底層原理,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-02-02

