溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么使用Matlab制作圖形驗(yàn)證碼生成器

發(fā)布時(shí)間:2022-02-28 14:46:34 來(lái)源:億速云 閱讀:146 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了怎么使用Matlab制作圖形驗(yàn)證碼生成器,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

    突然發(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效果

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    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

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    2刷新按鈕生成

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

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    實(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)回去

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    3.2字符邊緣

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

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    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線條和散點(diǎn)生成

    散點(diǎn)就是生成一堆隨機(jī)位置點(diǎn)和一些隨機(jī)顏色后用scatter函數(shù)繪制,線條是生成散點(diǎn)后使用&rsquo;spline&rsquo;插值方法插值成線后再繪制:

    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)
    
    % 繪制線
    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ù)制另存:

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    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)
            
            % 繪制線
            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控件可以先嘗試如下代碼:

    怎么使用Matlab制作圖形驗(yàn)證碼生成器

    這里用正常按鈕替換了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)
            
            % 繪制線
            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

    感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“怎么使用Matlab制作圖形驗(yàn)證碼生成器”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI