In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Matlab to make a graphic CAPTCHA generator, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Suddenly found that the cla function can also be applied to the app designer control, so change some of the content, change the way of drawing hidden pixels to refresh with cla
Original
Hold (acAxes,'off'); image (acAxes, [- 1 tag','ax' 0], [- 1 tag','ax' 0], ones (1 recorder 1), 'visible','off'); hold (acAxes,'on'); delete (findobj (' tag','ax'))
New
Cla (acAxes) cla (ax)
0 effect
1 character picture generation
If we simply use text to draw graphics, we will not be able to achieve the two colors of the symbol and the edge of the symbol, nor can we achieve a greater degree of deformation, so we need to convert the characters into matrix form.
To achieve this is also very simple, we just need to create an invisible fig, draw characters on it, save fig as a png format image, and then read the image through imread to get the character matrix:
The first time you run the program, it will be slow because you want to generate a character picture. If you run it again, you can read the picture that has been generated before:
% character picture matrix construction = below for character picture creation process% principle is to construct hidden figure and axes% to draw characters with text and save figure as picture% import picture if ~ exist ('Materials','dir') mkdir (' Materials'); endfig=figure ('units','pixels',...) 'position', [20 80 200 200],... 'Numbertitle','off',... 'Color', [1 11],... 'resize','off',... 'visible','off',... 'menubar','none'); ax=axes (' Units','pixels',... 'parent',fig,... 'Color', [1 11],... 'Position', [00 200 200],... 'XLim', [0200],... 'YLim', [0200],... 'XColor', [1 11],... 'YColor', [11]); strPic {length (strElement)} = []; for i=1:length (strElement)% is generated if the character does not exist, otherwise it is directly imported into 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']);% Save picture end tempPic=imread (['.\ Materials\', strElement (I), '.png']);% read picture strPic {I} = imresize (tempPic, [150150]);% resize picture end
2 Refresh button generation
You can see that the style of this button is different from most buttons:
This is actually a HTML control, and entering the location of the html file can create an effect similar to that of an embedded page:
AcHTML=uihtml (acFigure); acHTML.HTMLSource='.\ Materials\ textbtn.html';acHTML.DataChangedFcn=@refresh;acHTML.Position= [300 50 88 26]
As shown in the code, we are importing the textbtn.html file in the Materials folder
Textbtn.html looks like this:
Function setup (htmlComponent) {document.getElementById ("btnonclink") .addEventListener ("click", function (event) {htmlComponent.Data= "test";});} can't see clearly?
Of course, to prevent you from creating it, I wrote a piece of code in the m file that can automatically create a html file. The principle is to write string information into txt, and then change the suffix of the txt file to html:
% .html files are automatically generated and introduced-htmlContent= {';'; 'function setup (htmlComponent) {'; 'document.getElementById ("btnonclink"). AddEventListener ("click", function (event) {'; 'htmlComponent.Data= "test";});' can't see?' }; if ~ exist ('.\ Materials\ textbtn.html','file') fid=fopen ('.\ Materials\ textbtn.txt','w'); for i=1:length (htmlContent) fprintf (fid,'%s\ r\ nHtmlContent {I}); end fclose (fid); copyfile ('.\ Materials\ textbtn.txt','.\ Materials\ textbtn.html') Delete ('.\ Materials\ textbtn.txt') end3 Image processing 3.1 Image stretching in any direction
This part of the principle is to rotate the image at a certain angle, stretch in the vertical direction, and then rotate back.
3.2 character Edge
In this part of the principle, after the character mean is filtered, the part that is not completely black is set to gray, and then set to other colors later.
3.3 Image processing part code randColor=@ () randi ([0200], [1p3]);% anonymous function to generate random colors% extract image tPic=strPic {randiNums (ii)} from the image collection; tPic=tPic (:,:, 1);% rotate-stretch-rotate randiTheta1=randi ([0magin90]); randiTheta2=randi ([- 30jin30]); randiLenth=randi ([0jing70]); tPic=imrotate (255flowtPicrandiTheta1) TPic=imresize (tPic); tPic=imrotate (tPic,-randiTheta1+randiTheta2,'bilinear','crop');% blur the edge of the image and set the blurred part to 150tPic=255-imfilter (tPic,I_5); tPic (tPic~=0&tPic~=255) = 150% assign different colors to symbols and edges 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 line and scatter generation
Scatter is to generate a bunch of random location points and some random colors and then draw them with the scatter function. Lines are generated and interpolated into lines using the 'spline' interpolation method and then drawn:
RandColor=@ () randi ([0200], [1Magol 3]);% generate anonymous function randColor_n=@ (n) randi ([0200], [nMagol 3]). / 255;% generate anonymous function randPoint_n=@ (n) of n random colors [randi ([5195], [nMagol 1]), randi ([5CoJ 65], [nMagol 1])] % generate anonymous functions for n random points,% draw scatter pPonintsNum=randi ([6Magazine 10]); pPoints=randPoint_n (pPonintsNum); pPointsColor=randColor_n (pPonintsNum); scatter (acAxes,pPoints (:, 1), pPoints (:, 2), 6 paint scatter points. 'CData',pPointsColor,'AlphaData',0.6)% draw line lPonintsNum=randi ([5Magne7]); lPoints=randPoint_n (lPonintsNum); lPointsColor= [randColor (). / 255 line 0.6]; x_lPoints=interp1 (1), 1), 1), 1)); y_lPoints=interp1 (1). Plot (acAxes,x_lPoints,y_lPoints,'Color',lPointsColor,'LineWidth',1.5) 5 about Image Storage
Since the current version of uifigure does not support saving as an image, we draw the image in figure and uifigure respectively, in which figure is still invisible, which is mainly used to save the image verification code to png format, which can be seen in the complete code.
At the same time, the program is set to refresh the authCode.png to the latest CAPTCHA under the current folder every time you refresh the graphic CAPTCHA. If you need to save it, please rename it or copy it and save it in time:
6 about the comparison of verification codes
First of all, you need to extract the verification code in the box:
CodeInPut=acEditField.Value
Because our CAPTCHA characters are all uppercase, change the input text to uppercase with the upper function:
CodeInPut=upper (codeInPut)
At the same time, because 0 and O look too much alike, we do not distinguish them, but directly change the 0 in the input CAPTCHA to O:
CodeInPut (codeInPut=='0') ='O'
You can then use the strcmp function to compare the current CAPTCHA with the entered CAPTCHA:
If strcmp (codeInPut,authCode) msgbox ('CAPTCHA is correct') else msgbox ('CAPTCHA error') end7 complete code function authCodestrElement=char ([49msgbox 57mag65]);% 1-9 characters randColor=@ () randi ([0200], [1jue 3]);% generate anonymous functions of random colors randColor_n=@ (n) randi ([0200], [nMed 3]). / 255 % generate anonymous function randPoint_n=@ (n) of n random colors [randi ([5195], [nmage1]), randi ([5magin65], [nmage1])];% generate anonymous function global authCode of n random points % global variable: CAPTCHA% character picture matrix construction = =% below for character picture creation process% principle is to construct hidden figure and axes% to draw characters with text and save figure as picture% import picture if ~ exist ('Materials','dir') mkdir (' Materials'); endfig=figure ('image) 'position', [20 80 200 200],... 'Numbertitle','off',... 'Color', [1 11],... 'resize','off',... 'visible','off',... 'menubar','none'); ax=axes (' Units','pixels',... 'parent',fig,... 'Color', [1 11],... 'Position', [00 200 200],... 'XLim', [0200],... 'YLim', [0200],... 'XColor', [1 11],... 'YColor', [11]); strPic {length (strElement)} = []; for i=1:length (strElement)% is generated if the character does not exist, otherwise it is directly imported into 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']);% Save picture end tempPic=imread (['.\ Materials\', strElement (I), '.png']);% read picture strPic {I} = imresize (tempPic, [150150]) % resize picture end% change fig ax style to store fig.Position= [100 100 200 70]; ax.Position= [1 1 199.5 70]; ax.XTick= []; ax.YTick= []; ax.XLim= [0200]; ax.YLim= [0. 7 70]; ax.XColor= [0.7. 7]; ax.YColor= [0.7. 7. 7]; ax.Box='on';ax.YDir='reverse' Hold (ax,'on');% APP designer window Construction = 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= [0200]; acAxes.YLim= [0Magol 70]; acAxes.XColor= [0.7]; acAxes.YColor= [0.7]; acAxes.Box='on';acAxes.YDir='reverse' Hold (acAxes,'on'); acEditField=uieditfield (acFigure,'text'); acEditField.Position= [22052 7023]; acEditField.FontSize=16;acEditField.FontWeight='bold';acEditField.FontColor= [0.3 record0.3];% .html files are automatically generated and introduced-htmlContent= {';'; 'function setup (htmlComponent) {' 'document.getElementById ("btnonclink"). AddEventListener ("click", function (event) {'; 'htmlComponent.Data= "test";});' can't see clearly?' }; if ~ exist ('.\ Materials\ textbtn.html','file') fid=fopen ('.\ Materials\ textbtn.txt','w'); for i=1:length (htmlContent) fprintf (fid,'%s\ r\ nHtmlContent {I}); end fclose (fid); copyfile ('.\ Materials\ textbtn.txt','.\ Materials\ textbtn.html'); delete ('.\ Materials\ textbtn.txt') endacHTML=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=' verification code'; acButton.BackgroundColor= [0.31 0.58 0.80]; acButton.FontColor= [11 1]; acButton.FontWeight='bold';acButton.FontSize=14;acButton.ButtonPushedFcn=@verify;% callback function = function refresh (~, ~) cla (acAxes) cla (ax) I_5=fspecial ('average', [5L5]) % 5 mean filter template randiNums=randi ([1jue length (strElement)], [1M4]); authCode=strElement (randiNums);% CAPTCHA disp (authCode) for ii=1:4 tPic=strPic {randiNums (ii)}; tPic=tPic (:,:, 1);% tempPic (tempPic)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.