经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » MATLAB » 查看文章
教你使用Matlab制作图形验证码生成器(app designer)
来源:jb51  时间:2022/2/28 13:23:45  对本文有异议

突然发现cla函数也可以应用到app designer控件上,因而对部分内容做出更改,将绘制隐藏像素刷新的方式改为用cla

  1. hold(acAxes,'off');
  2. image(acAxes,[-1,0],[-1,0],ones(1,1,3),'visible','off');
  3. hold(acAxes,'on');
  4.  
  5. delete(findobj('tag','ax'));

  1. cla(acAxes)
  2. cla(ax)

0效果

1字符图片生成

如果我们单纯的用text绘制图形,就无法做到效果中符号和符号边缘两个颜色,也无法做到更大程度的变形,因此我们需要将字符转换为矩阵形式。

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

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

  1. % 字符图片矩阵构造 ========================================================
  2. % 以下为字符图片创建过程
  3. % 原理为构造隐藏的figureaxes
  4. % 在其上用text绘制字符并保存figure为图片
  5. % 导入图片
  6. if ~exist('Materials','dir')
  7. mkdir('Materials');
  8. end
  9. fig=figure('units','pixels',...
  10. 'position',[20 80 200 200],...
  11. 'Numbertitle','off',...
  12. 'Color',[1 1 1],...
  13. 'resize','off',...
  14. 'visible','off',...
  15. 'menubar','none');
  16. ax=axes('Units','pixels',...
  17. 'parent',fig,...
  18. 'Color',[1 1 1],...
  19. 'Position',[0 0 200 200],...
  20. 'XLim',[0 200],...
  21. 'YLim',[0 200],...
  22. 'XColor',[1 1 1],...
  23. 'YColor',[1 1 1]);
  24. strPic{length(strElement)}=[];
  25. for i=1:length(strElement)
  26. % 若是不存在该字符图片则生成,否则直接导入
  27. if ~exist(['.\Materials\',strElement(i),'.png'],'file')
  28. delete(findobj('tag','textStr'));
  29. text(ax,100,100,strElement(i),'HorizontalAlignment',...
  30. 'center','FontSize',140,'tag','textStr','FontWeigh','bold')
  31. saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存图片
  32. end
  33. tempPic=imread(['.\Materials\',strElement(i),'.png']); % 读取图片
  34. strPic{i}=imresize(tempPic,[150,150]); % 重新调整图片大小
  35. end

2刷新按钮生成

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

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

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

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

textbtn.html长这样:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=UTF-8>
  5. <script type="text/javascript">
  6. function setup(htmlComponent) {
  7. document.getElementById("btnonclink").addEventListener("click", function(event) {
  8. htmlComponent.Data="test";
  9. });
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a>
  15. </body>
  16. </html>

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

  1. % .html文件自动生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
  2. htmlContent={'<!DOCTYPE html><html><head><meta charset=UTF-8>';
  3. '<script type="text/javascript">';
  4. 'function setup(htmlComponent){';
  5. 'document.getElementById("btnonclink").addEventListener("click",function(event){';
  6. 'htmlComponent.Data="test";});}</script></head>';
  7. '<body><a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a></body></html>'};
  8. if ~exist('.\Materials\textbtn.html','file')
  9. fid=fopen('.\Materials\textbtn.txt','w');
  10. for i=1:length(htmlContent)
  11. fprintf(fid,'%s\r\n',htmlContent{i});
  12. end
  13. fclose(fid);
  14. copyfile('.\Materials\textbtn.txt','.\Materials\textbtn.html');
  15. delete('.\Materials\textbtn.txt')
  16. end

3图片处理

3.1图像任意方向拉伸

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

3.2字符边缘

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

3.3图像处理部分代码

  1. randColor=@()randi([0,200],[1,3]); % 生成随机颜色的匿名函数
  2.  
  3. % 从图像集合中提取图像
  4. tPic=strPic{randiNums(ii)};
  5. tPic=tPic(:,:,1);
  6.  
  7. % 将图像旋转-拉伸-旋转
  8. randiTheta1=randi([0,90]);
  9. randiTheta2=randi([-30,30]);
  10. randiLenth=randi([0,70]);
  11. tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
  12. tPic=imresize(tPic,[150+randiLenth,150]);
  13. tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
  14.  
  15. % 将图像边缘进行模糊,并将模糊的部分数值设置为150
  16. tPic=255-imfilter(tPic,I_5);
  17. tPic(tPic~=0&tPic~=255)=150;
  18.  
  19. % 为符号和符号边缘赋予不同颜色
  20. tempColor1=randColor();tempColor2=randColor();
  21. tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
  22. tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
  23. tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
  24. tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
  25.  
  26. tempPic_3=uint8(zeros([size(tPic),3]));
  27. tempPic_3(:,:,1)=tempPicR;
  28. tempPic_3(:,:,2)=tempPicG;
  29. tempPic_3(:,:,3)=tempPicB;

4线条和散点生成

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

  1. randColor=@()randi([0,200],[1,3]); % 生成随机颜色的匿名函数
  2. randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n个随机颜色的匿名函数
  3. randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n个随机点的匿名函数
  4.  
  5. % 绘制散点
  6. pPonintsNum=randi([6,10]);
  7. pPoints=randPoint_n(pPonintsNum);
  8. pPointsColor=randColor_n(pPonintsNum);
  9. scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
  10. 'CData',pPointsColor,'AlphaData',0.6)
  11.  
  12. % 绘制线
  13. lPonintsNum=randi([5,7]);
  14. lPoints=randPoint_n(lPonintsNum);
  15. lPointsColor=[randColor()./255,0.6];
  16. x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
  17. y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
  18. plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)

5关于图像存储

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

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

6关于验证码对比

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

  1. codeInPut=acEditField.Value;

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

  1. codeInPut=upper(codeInPut);

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

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

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

  1. if strcmp(codeInPut,authCode)
  2. msgbox('验证码正确')
  3. else
  4. msgbox('验证码错误')
  5. end

7完整代码

  1. function authCode
  2. strElement=char([49:57,65:90]); % 1-9A-Z的字符
  3. randColor=@()randi([0,200],[1,3]); % 生成随机颜色的匿名函数
  4. randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n个随机颜色的匿名函数
  5. randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n个随机点的匿名函数
  6. global authCode; % 全局变量:验证码
  7.  
  8. % 字符图片矩阵构造 ========================================================
  9. % 以下为字符图片创建过程
  10. % 原理为构造隐藏的figureaxes
  11. % 在其上用text绘制字符并保存figure为图片
  12. % 导入图片
  13. if ~exist('Materials','dir')
  14. mkdir('Materials');
  15. end
  16. fig=figure('units','pixels',...
  17. 'position',[20 80 200 200],...
  18. 'Numbertitle','off',...
  19. 'Color',[1 1 1],...
  20. 'resize','off',...
  21. 'visible','off',...
  22. 'menubar','none');
  23. ax=axes('Units','pixels',...
  24. 'parent',fig,...
  25. 'Color',[1 1 1],...
  26. 'Position',[0 0 200 200],...
  27. 'XLim',[0 200],...
  28. 'YLim',[0 200],...
  29. 'XColor',[1 1 1],...
  30. 'YColor',[1 1 1]);
  31. strPic{length(strElement)}=[];
  32. for i=1:length(strElement)
  33. % 若是不存在该字符图片则生成,否则直接导入
  34. if ~exist(['.\Materials\',strElement(i),'.png'],'file')
  35. delete(findobj('tag','textStr'));
  36. text(ax,100,100,strElement(i),'HorizontalAlignment',...
  37. 'center','FontSize',140,'tag','textStr','FontWeigh','bold')
  38. saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存图片
  39. end
  40. tempPic=imread(['.\Materials\',strElement(i),'.png']); % 读取图片
  41. strPic{i}=imresize(tempPic,[150,150]); % 重新调整图片大小
  42. end
  43.  
  44. % 更改fig ax样式,为方便后期验证码存储
  45. fig.Position=[100 100 200 70];
  46. ax.Position=[1 1 199.5 70];
  47. ax.XTick=[];
  48. ax.YTick=[];
  49. ax.XLim=[0,200];
  50. ax.YLim=[0,70];
  51. ax.XColor=[0.7 0.7 0.7];
  52. ax.YColor=[0.7 0.7 0.7];
  53. ax.Box='on';
  54. ax.YDir='reverse';
  55. hold(ax,'on');
  56.  
  57.  
  58. % APP designer窗口构建 ====================================================
  59. acFigure=uifigure();
  60. acFigure.Position=[100 100 370 90];
  61. acFigure.Name='authCode';
  62. acFigure.Resize='off';
  63.  
  64. acAxes=uiaxes(acFigure);
  65. acAxes.Position=[10 10 200 70];
  66. acAxes.XTick=[];
  67. acAxes.YTick=[];
  68. acAxes.XLim=[0,200];
  69. acAxes.YLim=[0,70];
  70. acAxes.XColor=[0.7 0.7 0.7];
  71. acAxes.YColor=[0.7 0.7 0.7];
  72. acAxes.Box='on';
  73. acAxes.YDir='reverse';
  74. hold(acAxes,'on');
  75.  
  76. acEditField=uieditfield(acFigure,'text');
  77. acEditField.Position=[220 52 70 23];
  78. acEditField.FontSize=16;
  79. acEditField.FontWeight='bold';
  80. acEditField.FontColor=[0.3,0.3,0.3];
  81.  
  82. % .html文件自动生成及引入 - - - - - - - - - - - - - - - - - - - - - - - - -
  83. htmlContent={'<!DOCTYPE html><html><head><meta charset=UTF-8>';
  84. '<script type="text/javascript">';
  85. 'function setup(htmlComponent){';
  86. 'document.getElementById("btnonclink").addEventListener("click",function(event){';
  87. 'htmlComponent.Data="test";});}</script></head>';
  88. '<body><a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" btnonclink">看不清?</a></body></html>'};
  89. if ~exist('.\Materials\textbtn.html','file')
  90. fid=fopen('.\Materials\textbtn.txt','w');
  91. for i=1:length(htmlContent)
  92. fprintf(fid,'%s\r\n',htmlContent{i});
  93. end
  94. fclose(fid);
  95. copyfile('.\Materials\textbtn.txt','.\Materials\textbtn.html');
  96. delete('.\Materials\textbtn.txt')
  97. end
  98. acHTML=uihtml(acFigure);
  99. acHTML.HTMLSource='.\Materials\textbtn.html';
  100. acHTML.DataChangedFcn=@refresh;
  101. acHTML.Position=[300 50 88 26];
  102. % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  103.  
  104. acButton=uibutton(acFigure);
  105. acButton.Position=[220 15 140 30];
  106. acButton.Text=' ';
  107. acButton.BackgroundColor=[0.31 0.58 0.80];
  108. acButton.FontColor=[1 1 1];
  109. acButton.FontWeight='bold';
  110. acButton.FontSize=14;
  111. acButton.ButtonPushedFcn=@verify;
  112.  
  113. % 回调函数 ================================================================
  114. function refresh(~,~)
  115. cla(acAxes)
  116. cla(ax)
  117. I_5=fspecial('average',[5,5]); % 5*5均值滤波模板
  118. randiNums=randi([1,length(strElement)],[1,4]);
  119. authCode=strElement(randiNums); % 验证码
  120. disp(authCode)
  121. for ii=1:4
  122. tPic=strPic{randiNums(ii)};
  123. tPic=tPic(:,:,1);
  124. %tempPic(tempPic<250)=150;
  125. % 将图像旋转-拉伸-旋转
  126. randiTheta1=randi([0,90]);
  127. randiTheta2=randi([-30,30]);
  128. randiLenth=randi([0,70]);
  129. tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
  130. tPic=imresize(tPic,[150+randiLenth,150]);
  131. tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
  132. % 将图像边缘进行模糊,并将模糊的部分数值设置为150
  133. tPic=255-imfilter(tPic,I_5);
  134. tPic(tPic~=0&tPic~=255)=150;
  135.  
  136. % 为符号和符号边缘赋予不同颜色
  137. tempColor1=randColor();tempColor2=randColor();
  138. tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
  139. tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
  140. tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
  141. tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
  142. tempPic_3=uint8(zeros([size(tPic),3]));
  143. tempPic_3(:,:,1)=tempPicR;
  144. tempPic_3(:,:,2)=tempPicG;
  145. tempPic_3(:,:,3)=tempPicB;
  146. % 显示图片
  147. image(acAxes,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
  148. [-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
  149. tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
  150. image(ax,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
  151. [-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
  152. tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
  153. end
  154. % 绘制散点
  155. pPonintsNum=randi([6,10]);
  156. pPoints=randPoint_n(pPonintsNum);
  157. pPointsColor=randColor_n(pPonintsNum);
  158. scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
  159. 'CData',pPointsColor,'AlphaData',0.6)
  160. scatter(ax,pPoints(:,1),pPoints(:,2),6,'filled',...
  161. 'CData',pPointsColor,'AlphaData',0.6)
  162. % 绘制线
  163. lPonintsNum=randi([5,7]);
  164. lPoints=randPoint_n(lPonintsNum);
  165. lPointsColor=[randColor()./255,0.6];
  166. x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
  167. y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
  168. plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
  169. plot(ax,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
  170. saveas(fig,'.\authCode.png');
  171. end
  172. refresh()
  173.  
  174. function verify(~,~)
  175. codeInPut=acEditField.Value;
  176. codeInPut=upper(codeInPut);
  177. codeInPut(codeInPut=='0')='O';
  178. if strcmp(codeInPut,authCode)
  179. msgbox('验证码正确')
  180. else
  181. msgbox('验证码错误')
  182. end
  183. end
  184.  
  185. end

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

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

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

  1. function authCode2
  2. strElement=char([49:57,65:90]); % 1-9A-Z的字符
  3. randColor=@()randi([0,200],[1,3]); % 生成随机颜色的匿名函数
  4. randColor_n=@(n)randi([0,200],[n,3])./255; % 生成n个随机颜色的匿名函数
  5. randPoint_n=@(n)[randi([5,195],[n,1]),randi([5,65],[n,1])];% 生成n个随机点的匿名函数
  6. global authCode; % 全局变量:验证码
  7.  
  8. % 字符图片矩阵构造 ========================================================
  9. % 以下为字符图片创建过程
  10. % 原理为构造隐藏的figureaxes
  11. % 在其上用text绘制字符并保存figure为图片
  12. % 导入图片
  13. if ~exist('Materials','dir')
  14. mkdir('Materials');
  15. end
  16. fig=figure('units','pixels',...
  17. 'position',[20 80 200 200],...
  18. 'Numbertitle','off',...
  19. 'Color',[1 1 1],...
  20. 'resize','off',...
  21. 'visible','off',...
  22. 'menubar','none');
  23. ax=axes('Units','pixels',...
  24. 'parent',fig,...
  25. 'Color',[1 1 1],...
  26. 'Position',[0 0 200 200],...
  27. 'XLim',[0 200],...
  28. 'YLim',[0 200],...
  29. 'XColor',[1 1 1],...
  30. 'YColor',[1 1 1]);
  31. strPic{length(strElement)}=[];
  32. for i=1:length(strElement)
  33. % 若是不存在该字符图片则生成,否则直接导入
  34. if ~exist(['.\Materials\',strElement(i),'.png'],'file')
  35. delete(findobj('tag','textStr'));
  36. text(ax,100,100,strElement(i),'HorizontalAlignment',...
  37. 'center','FontSize',140,'tag','textStr','FontWeigh','bold')
  38. saveas(fig,['.\Materials\',strElement(i),'.png']); % 保存图片
  39. end
  40. tempPic=imread(['.\Materials\',strElement(i),'.png']); % 读取图片
  41. strPic{i}=imresize(tempPic,[150,150]); % 重新调整图片大小
  42. end
  43.  
  44. % 更改fig ax样式,为方便后期验证码存储
  45. fig.Position=[100 100 200 70];
  46. ax.Position=[1 1 199.5 70];
  47. ax.XTick=[];
  48. ax.YTick=[];
  49. ax.XLim=[0,200];
  50. ax.YLim=[0,70];
  51. ax.XColor=[0.7 0.7 0.7];
  52. ax.YColor=[0.7 0.7 0.7];
  53. ax.Box='on';
  54. ax.YDir='reverse';
  55. hold(ax,'on');
  56.  
  57.  
  58. % APP designer窗口构建 ====================================================
  59. acFigure=uifigure();
  60. acFigure.Position=[100 100 370 90];
  61. acFigure.Name='authCode';
  62. acFigure.Resize='off';
  63.  
  64. acAxes=uiaxes(acFigure);
  65. acAxes.Position=[10 10 200 70];
  66. acAxes.XTick=[];
  67. acAxes.YTick=[];
  68. acAxes.XLim=[0,200];
  69. acAxes.YLim=[0,70];
  70. acAxes.XColor=[0.7 0.7 0.7];
  71. acAxes.YColor=[0.7 0.7 0.7];
  72. acAxes.Box='on';
  73. acAxes.YDir='reverse';
  74. hold(acAxes,'on');
  75.  
  76. acEditField=uieditfield(acFigure,'text');
  77. acEditField.Position=[220 52 70 23];
  78. acEditField.FontSize=16;
  79. acEditField.FontWeight='bold';
  80. acEditField.FontColor=[0.3,0.3,0.3];
  81.  
  82. acfreshBtn=uibutton(acFigure);
  83. acfreshBtn.Text='看不清?';
  84. acfreshBtn.ButtonPushedFcn=@refresh;
  85. acfreshBtn.Position=[300 50 60 27];
  86. % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87.  
  88. acButton=uibutton(acFigure);
  89. acButton.Position=[220 15 140 30];
  90. acButton.Text=' ';
  91. acButton.BackgroundColor=[0.31 0.58 0.80];
  92. acButton.FontColor=[1 1 1];
  93. acButton.FontWeight='bold';
  94. acButton.FontSize=14;
  95. acButton.ButtonPushedFcn=@verify;
  96.  
  97. % 回调函数 ================================================================
  98. function refresh(~,~)
  99. cla(acAxes)
  100. cla(ax)
  101. % hold(acAxes,'off');
  102. % image(acAxes,[-1,0],[-1,0],ones(1,1,3),'visible','off');
  103. % hold(acAxes,'on');
  104. % delete(findobj('tag','ax'));
  105. I_5=fspecial('average',[5,5]); % 5*5均值滤波模板
  106. randiNums=randi([1,length(strElement)],[1,4]);
  107. authCode=strElement(randiNums); % 验证码
  108. disp(authCode)
  109. for ii=1:4
  110. tPic=strPic{randiNums(ii)};
  111. tPic=tPic(:,:,1);
  112. %tempPic(tempPic<250)=150;
  113. % 将图像旋转-拉伸-旋转
  114. randiTheta1=randi([0,90]);
  115. randiTheta2=randi([-30,30]);
  116. randiLenth=randi([0,70]);
  117. tPic=imrotate(255-tPic,randiTheta1,'bilinear','crop');
  118. tPic=imresize(tPic,[150+randiLenth,150]);
  119. tPic=imrotate(tPic,-randiTheta1+randiTheta2,'bilinear','crop');
  120. % 将图像边缘进行模糊,并将模糊的部分数值设置为150
  121. tPic=255-imfilter(tPic,I_5);
  122. tPic(tPic~=0&tPic~=255)=150;
  123.  
  124. % 为符号和符号边缘赋予不同颜色
  125. tempColor1=randColor();tempColor2=randColor();
  126. tempPicR=tPic;tempPicG=tPic;tempPicB=tPic;
  127. tempPicR(tPic==150)=tempColor1(1);tempPicR(tPic==0)=tempColor2(1);
  128. tempPicG(tPic==150)=tempColor1(2);tempPicG(tPic==0)=tempColor2(2);
  129. tempPicB(tPic==150)=tempColor1(3);tempPicB(tPic==0)=tempColor2(3);
  130. tempPic_3=uint8(zeros([size(tPic),3]));
  131. tempPic_3(:,:,1)=tempPicR;
  132. tempPic_3(:,:,2)=tempPicG;
  133. tempPic_3(:,:,3)=tempPicB;
  134. % 显示图片
  135. image(acAxes,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
  136. [-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
  137. tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
  138. image(ax,[-size(tempPic_3,2)/2,size(tempPic_3,2)/2]./3.5+40*ii+randi([-5,5]),...
  139. [-size(tempPic_3,1)/2,size(tempPic_3,1)/2]./3.5+35+randi([-5,5]),...
  140. tempPic_3,'AlphaData',tempPic_3(:,:,1)~=255,'Interpolation','bilinear')
  141. end
  142. % 绘制散点
  143. pPonintsNum=randi([6,10]);
  144. pPoints=randPoint_n(pPonintsNum);
  145. pPointsColor=randColor_n(pPonintsNum);
  146. scatter(acAxes,pPoints(:,1),pPoints(:,2),6,'filled',...
  147. 'CData',pPointsColor,'AlphaData',0.6)
  148. scatter(ax,pPoints(:,1),pPoints(:,2),6,'filled',...
  149. 'CData',pPointsColor,'AlphaData',0.6)
  150. % 绘制线
  151. lPonintsNum=randi([5,7]);
  152. lPoints=randPoint_n(lPonintsNum);
  153. lPointsColor=[randColor()./255,0.6];
  154. x_lPoints=interp1(1:lPonintsNum,lPoints(:,1),1:0.01:lPonintsNum,'spline');
  155. y_lPoints=interp1(1:lPonintsNum,lPoints(:,2),1:0.01:lPonintsNum,'spline');
  156. plot(acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
  157. plot(ax,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5)
  158. saveas(fig,'.\authCode.png');
  159. end
  160. refresh()
  161.  
  162. function verify(~,~)
  163. codeInPut=acEditField.Value;
  164. codeInPut=upper(codeInPut);
  165. codeInPut(codeInPut=='0')='O';
  166. if strcmp(codeInPut,authCode)
  167. msgbox('验证码正确')
  168. else
  169. msgbox('验证码错误')
  170. end
  171. end
  172.  
  173. end

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号