教你使用Matlab制作图形验证码生成器(app designer)

目录
  • 1字符图片生成
  • 2刷新按钮生成
  • 3图片处理
    • 3.1图像任意方向拉伸
    • 3.2字符边缘
    • 3.3图像处理部分代码
  • 4线条和散点生成
  • 5关于图像存储
  • 6关于验证码对比
  • 7完整代码

突然发现cla函数也可以应用到app designer控件上,因而对部分内容做出更改,将绘制隐藏像素刷新的方式改为用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绘制图形,就无法做到效果中符号和符号边缘两个颜色,也无法做到更大程度的变形,因此我们需要将字符转换为矩阵形式。

想要实现也非常简单,我们只需要创建一个不可视的fig,在其上绘制字符,保存fig为png格式图片,再通过imread读取图片,就能获得字符矩阵:

第一次运行程序因为要生成字符图片因而会比较慢,再次运行就可以读取之前已经生成过的图片啦:

% 字符图片矩阵构造 ========================================================
% 以下为字符图片创建过程
% 原理为构造隐藏的figure和axes
% 在其上用text绘制字符并保存figure为图片
% 导入图片
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)
    % 若是不存在该字符图片则生成,否则直接导入
    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]);             % 重新调整图片大小
end

2刷新按钮生成

大家可以看到这个按钮的样式与大部分按钮不同:

实际上这是一个HTML控件,输入html文件的位置就可以形成类似嵌入页面的效果:

acHTML=uihtml(acFigure);
acHTML.HTMLSource='.\Materials\textbtn.html';
acHTML.DataChangedFcn=@refresh;
acHTML.Position=[300 50 88 26];

如代码所示,我们导入的是Materials文件夹内的textbtn.html文件

textbtn.html长这样:

<!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>

当然为了防止大家不会创建,我在m文件中写了一段能够自动创建html文件的代码,原理就是将字符串信息写入txt,再将txt文件后缀改为html:

% .html文件自动生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
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图像任意方向拉伸

这部分原理就是将图像旋转一定角度后,在竖直方向进行拉伸后再旋转回去

3.2字符边缘

这部分原理将字符均值滤波后,把不完全是黑色的部分设置为灰色,后期再设置为其他颜色

3.3图像处理部分代码

randColor=@()randi([0,200],[1,3]);   % 生成随机颜色的匿名函数

% 从图像集合中提取图像
tPic=strPic{randiNums(ii)};
tPic=tPic(:,:,1);

% 将图像旋转-拉伸-旋转
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');

% 将图像边缘进行模糊,并将模糊的部分数值设置为150
tPic=255-imfilter(tPic,I_5);
tPic(tPic~=0&tPic~=255)=150;

% 为符号和符号边缘赋予不同颜色
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线条和散点生成

散点就是生成一堆随机位置点和一些随机颜色后用scatter函数绘制,线条是生成散点后使用’spline’插值方法插值成线后再绘制:

randColor=@()randi([0,200],[1,3]);           % 生成随机颜色的匿名函数
randColor_n=@(n)randi([0,200],[n,3])./255;   % 生成n个随机颜色的匿名函数
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成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关于图像存储

由于目前版本uifigure还不支持存储为图像,因此我们绘制图像是在figure和uifigure分别绘制一遍,其中figure依旧是不可见状态,主要用于将图片验证码保存为png格式,可以在完整代码中看出这一点。

同时,本程序的设置为,每次刷新图形验证码,都会刷新当前文件夹下authCode.png为最新的验证码,如需要保存请及时将其改名或复制另存:

6关于验证码对比

首先就是需要提取框内验证码:

codeInPut=acEditField.Value;

因为我们的验证码字符都是大写的,将输入的文本用upper函数变为大写:

codeInPut=upper(codeInPut);

同时我们因为0和O长的太像,所以不对其进行区分,直接将输入的验证码中的0改为O:

codeInPut(codeInPut=='0')='O';

之后就能够用strcmp函数将当前验证码和输入的验证码进行对比:

if strcmp(codeInPut,authCode)
    msgbox('验证码正确')
else
    msgbox('验证码错误')
end

7完整代码

function authCode
strElement=char([49:57,65:90]);              % 1-9,A-Z的字符
randColor=@()randi([0,200],[1,3]);           % 生成随机颜色的匿名函数
randColor_n=@(n)randi([0,200],[n,3])./255;   % 生成n个随机颜色的匿名函数
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n个随机点的匿名函数
global authCode;                             % 全局变量:验证码

% 字符图片矩阵构造 ========================================================
% 以下为字符图片创建过程
% 原理为构造隐藏的figure和axes
% 在其上用text绘制字符并保存figure为图片
% 导入图片
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)
    % 若是不存在该字符图片则生成,否则直接导入
    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]);             % 重新调整图片大小
end

% 更改fig ax样式,为方便后期验证码存储
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窗口构建 ====================================================
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文件自动生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
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='确 认 验 证 码';
acButton.BackgroundColor=[0.31 0.58 0.80];
acButton.FontColor=[1 1 1];
acButton.FontWeight='bold';
acButton.FontSize=14;
acButton.ButtonPushedFcn=@verify;

% 回调函数 ================================================================
    function refresh(~,~)
        cla(acAxes)
        cla(ax)

        I_5=fspecial('average',[5,5]);   % 5*5均值滤波模板
        randiNums=randi([1,length(strElement)],[1,4]);
        authCode=strElement(randiNums);  % 验证码
        disp(authCode)
        for ii=1:4
            tPic=strPic{randiNums(ii)};
            tPic=tPic(:,:,1);
            %tempPic(tempPic<250)=150;

            % 将图像旋转-拉伸-旋转
            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'); 

            % 将图像边缘进行模糊,并将模糊的部分数值设置为150
            tPic=255-imfilter(tPic,I_5);
            tPic(tPic~=0&tPic~=255)=150;

            % 为符号和符号边缘赋予不同颜色
            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

        % 绘制散点
        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('验证码正确')
        else
            msgbox('验证码错误')
        end

    end

end

:程序第一次运行由于有html文件及png文件需要生成,因而会比较慢,之后的运行速度会快很多。

对于以前版本没有uihtml控件可以先尝试如下代码:

这里用正常按钮替换了uihtml控件

function authCode2
strElement=char([49:57,65:90]);              % 1-9,A-Z的字符
randColor=@()randi([0,200],[1,3]);           % 生成随机颜色的匿名函数
randColor_n=@(n)randi([0,200],[n,3])./255;   % 生成n个随机颜色的匿名函数
randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n个随机点的匿名函数
global authCode;                             % 全局变量:验证码

% 字符图片矩阵构造 ========================================================
% 以下为字符图片创建过程
% 原理为构造隐藏的figure和axes
% 在其上用text绘制字符并保存figure为图片
% 导入图片
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)
    % 若是不存在该字符图片则生成,否则直接导入
    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]);             % 重新调整图片大小
end

% 更改fig ax样式,为方便后期验证码存储
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窗口构建 ====================================================
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='确 认 验 证 码';
acButton.BackgroundColor=[0.31 0.58 0.80];
acButton.FontColor=[1 1 1];
acButton.FontWeight='bold';
acButton.FontSize=14;
acButton.ButtonPushedFcn=@verify;

% 回调函数 ================================================================
    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);  % 验证码
        disp(authCode)
        for ii=1:4
            tPic=strPic{randiNums(ii)};
            tPic=tPic(:,:,1);
            %tempPic(tempPic<250)=150;

            % 将图像旋转-拉伸-旋转
            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'); 

            % 将图像边缘进行模糊,并将模糊的部分数值设置为150
            tPic=255-imfilter(tPic,I_5);
            tPic(tPic~=0&tPic~=255)=150;

            % 为符号和符号边缘赋予不同颜色
            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

        % 绘制散点
        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('验证码正确')
        else
            msgbox('验证码错误')
        end

    end

end

以上就是教你使用Matlab制作图形验证码生成器(app designer)的详细内容,更多关于Matlab图形验证码生成器的资料请关注我们其它相关文章!

(0)

相关推荐

  • JavaScript生成图形验证码

    本文实例为大家分享了js生成图形验证码的具体代码,供大家参考,具体内容如下 getGVerify:function (id) { function GVerify(options) { //创建一个图形验证码对象,接收options对象为参数 this.options = { //默认options参数值 id: "", //容器Id canvasId: "verifyCanvas", //canvas的ID width: "100", //默认

  • php生成图形验证码几种方法小结

    我们先来检查一下自己的php是不是打开了gd库. 复制代码 代码如下: <?phpif(extension_loaded('gd')) {  echo '你可以使用gd<br>';  foreach(gd_info() as $cate=>$value)    echo "$cate: $value<br>";}else  echo '你没有安装gd扩展';?> 如果有返回信息就正确可以常用使用了例1 复制代码 代码如下: <?php/*

  • python生成随机图形验证码详解

    使用python生成随机图片验证码,需要使用pillow模块 1.安装pillow模块 pip install pillow 2.pillow模块的基本使用 1.创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的(255,255,255)颜色的图片 img1=Image.new(mode="RGB",size=(400,400),color=(255,255,255)) #把生成的图片保存为"pi

  • Java生成图形验证码工具类

    生成验证码效果 ValidateCode.java 验证码生成类 package cn.dsna.util.images; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.io.Ou

  • SpringBoot 图形验证码的生成和校验

    1. 编写工具类 package com.cn.beauty.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSes

  • Java Web开发之图形验证码的生成与使用方法

    本文实例讲述了Java Web开发之图形验证码的生成与使用方法.分享给大家供大家参考.具体如下: 图形验证码的主要目的是为了增强的安全性,增加用户通过遍历所有可能性来破解密码的难度. 图形验证码的使用包括如下3部分: ① 图形验证码的生成: ② 在页面中的使用: ③ 验证: 1.图形验证码的生成 假设在Servlet生成图形验证码,在JavaBean或者JSP中生成的基本过程是相同的.设计如下过程: ① 设置响应的文档类型: ② 生成随机码: ③ 把随机码保存到session中: ④ 生成图片:

  • PHP实现生成带背景的图形验证码功能

    本文实例讲述了PHP实现生成带背景的图形验证码功能.分享给大家供大家参考,具体如下: 以前我们利用php生成的都是无背景或同一色彩背景的验证码了,但这种验证容易给机器识别了,这里就来介绍一些生成带背景的图形验证码实例. 1.产生一张png的图片, 2.为图片设置背景色, 3.设置字体颜色和样式, 4.产生4位数的随机的验证码, 5.把产生的每个字符调整旋转角度和位置画到png图片上, 6.加入噪点和干扰线防止注册机器分析原图片来恶意注册, 7.输出图片, 8.释放图片所占内存 authcode.

  • 教你使用Matlab制作图形验证码生成器(app designer)

    目录 1字符图片生成 2刷新按钮生成 3图片处理 3.1图像任意方向拉伸 3.2字符边缘 3.3图像处理部分代码 4线条和散点生成 5关于图像存储 6关于验证码对比 7完整代码 突然发现cla函数也可以应用到app designer控件上,因而对部分内容做出更改,将绘制隐藏像素刷新的方式改为用cla 原 hold(acAxes,'off'); image(acAxes,[-1,0],[-1,0],ones(1,1,3),'visible','off'); hold(acAxes,'on'); d

  • 教你用Matlab制作立体动态相册

    目录 效果 教程部分 1图片导入与大小重设 2figaxes设置 3绘制图形句柄 4立方体旋转 5获取鼠标与中心点的距离 6鼠标移动到fig中心时更新图片 完整代码 效果 教程部分 1 图片导入与大小重设 需要有一个名为album的文件夹和当前m文件在同一文件夹,另外ablum文件夹内至少要有一张jpg格式图片 path='.\album\';%文件夹名称 files=dir(fullfile(path,'*.jpg')); picNum=size(files,1); %遍历路径下每一幅图像 f

  • 教你用Matlab制作黄金矿工小游戏

    目录 效果 步骤 图片准备 背景构建 绘制爪子 让爪子转起来 绘制石块 点击下箭头移动爪子 爪子与石头和边缘碰触判断 抓取石块和显示金钱 完整代码 效果 步骤 图片准备 本文所使用图片在这 背景构建 function goldMiner Mainfig=figure('units','pixels','position',[50 100 750 500],... 'Numbertitle','off','menubar','none','resize','off',... 'name','gol

  • Javascript 制作图形验证码实例详解

    js 图形验证码制作 实际效果 第一步我们来到要展示验证码的页面,当我们按下营业执照的时候让其,弹出一个弹框,弹框的上面就是验证码,如图一所示: (图一) 弹框的样式如图二所示: (图二) 我们要对验证码的值进行校验,判断验证码是否输入正确,当输入不正确的时候,我们提示错误信息,提示信息如图三所示: (图三) 如果页面了验证正确,这不会提示错误信息并且调到我们的目标页面,如图四所示: (图四) --–营业执照页面为私密页面,使用其他页面代替原图. 路由层描述 //1-在路由层进行设置,页面跳转到

  • JS制作图形验证码实现代码

    第一步我们来到要展示验证码的页面,当我们按下营业执照的时候让其,弹出一个弹框,弹框的上面就是验证码,如图一所示: (图一) 弹框的样式如图二所示: (图二) 我们要对验证码的值进行校验,判断验证码是否输入正确,当输入不正确的时候,我们提示错误信息,提示信息如图三所示: (图三) 如果页面了验证正确,这不会提示错误信息并且调到我们的目标页面,如图四所示: (图四) 路由层描述 /** 供货商店铺-店铺简介 */ //1-在路由层进行设置,页面跳转到根目录下/buyer/vshop/info.ejs

  • PHP制作图形验证码代码分享

    效果: myvcode.class.php:封装创建验证码的类 <?php/** file:myvcode.class.php* 验证码类,类名Vcode*/class Vcode{private $width;              /*验证码宽度*/private $height;             /*验证码高度*/private $codeNum;            /*验证码字符个数*/private $checkCode;            /*验证码字符*/pri

  • asp.net使用ashx生成图形验证码的方法示例

    本文实例讲述了asp.net使用ashx生成图形验证码的方法.分享给大家供大家参考,具体如下: 验证码的好处不用我多说,你们都懂的.我在网上看到有人把验证码直接写在aspx页面里,也就是说这种方式请求验证码等于请求一个页面,这样做很不科学.如下所示 <form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server"

  • ASP.NET生成图形验证码的方法详解

    本文实例讲述了ASP.NET生成图形验证码的方法.分享给大家供大家参考,具体如下: 通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3)保存验证码. 新建一个页面为default.aspx,  放置一个TextBox控件和一个Image控件,TextBox控件用于输入生成的字符串,Image控件用于显示字符串,它的图片就为生成的图形验证码image

  • Matlab制作视频并转换成gif动态图的两种方法

    一.第一个方法比较简单,就是使用movie(f)直接取生成AVI视频文件. %% f(t)-->f(4*t+12) 并且验证%% function Signal_change() tic%记录程序运行时间 figure n = 0; t = -2*pi:0.01:2*pi; y = sin(t);%周期为2*pi y_result = sin(4*t); plot(t,y,'b'); xlabel('t'); ylabel('Amplitude'); n = n+1; F(n) = getfra

随机推荐