经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » MATLAB » 查看文章
利用Matlab复刻举牌加油小人生成器
来源:jb51  时间:2022/3/2 13:43:26  对本文有异议

突然发现cla函数也可以应用到app designer控件上,因而对部分内容做出更改。

手痒,用matlab app designer复刻了一款举牌加油小人生成器,效果如下:

该程序可以生成正方形和长方形举牌小人,并且可以自己设置背景颜色(点击那些有颜色的小框框),点击绿色旋转按钮可以刷新生成的图片。

目前需要存图还是需要通过uiaxes自带的toolbar(下图右上角所示):

使用该程序需要小人的素材包,我已经将素材整理为materials.mat文件,网盘下载链接如下:

链接:https://pan.baidu.com/s/1ISloOsq8bIVDjm8TzRPlKw 

提取码:1234

使用时需要把materials.mat和m文件放在同一文件夹

完整代码

  1. function upup
  2. global textMsg bkgColor colorList axesType
  3. global textArea
  4. textMsg=[];
  5. axesType=2;
  6. bkgColor=[1,1,1];
  7. colorList=[0.9000 0 0.0700
  8. 0.9200 0.3300 0.0800
  9. 0.9600 0.6200 0.7500
  10. 0.9600 0.9400 0.5500
  11. 0.7600 0.8400 0.2500
  12. 0.3800 0.7600 0.7300
  13. 0.4700 0.8100 0.9400
  14. 0.6000 0.0500 0.4900
  15. 1 1 1 ];
  16.  
  17.  
  18. %load pic and create pic ==================================================
  19. materials=load('materials.mat');
  20. if ~exist('materials','dir')
  21. mkdir('materials');
  22. end
  23. for i=1:size(colorList,1)
  24. Rc=zeros(60,60);Rc(11:50,11:50)=255.*colorList(i,1);
  25. Gc=zeros(60,60);Gc(11:50,11:50)=255.*colorList(i,2);
  26. Bc=zeros(60,60);Bc(11:50,11:50)=255.*colorList(i,3);
  27. cPic(:,:,1)=Rc;cPic(:,:,2)=Gc;cPic(:,:,3)=Bc;
  28. imwrite(uint8(cPic),['materials\bkg',num2str(i),'.png'])
  29. end
  30. imwrite(materials.imgSet.cover,'materials\cover.jpg')
  31. imwrite(materials.imgSet.square,'materials\square.jpg')
  32. imwrite(materials.imgSet.refresh.CData,'materials\refresh.png','Alpha',materials.imgSet.refresh.AData)
  33.  
  34.  
  35.  
  36. %==========================================================================
  37. upFig=uifigure('units','pixels',...
  38. 'position',[320 50 400 600],...
  39. 'Numbertitle','off',...
  40. 'menubar','none',...
  41. 'resize','off',...
  42. 'name','upup',...
  43. 'color',[0.93 0.6 0]);
  44.  
  45. %==========================================================================
  46. textAreaLabel=uilabel(upFig);
  47. textAreaLabel.Position=[55 445 290 100];
  48. textAreaLabel.Text='';
  49. textAreaLabel.BackgroundColor=[0,0,0];
  50. textArea=uitextarea(upFig,'ValueChangedFcn',@changeText);
  51. textArea.Position=[60 450 280 90];
  52. textArea.FontSize=23;
  53.  
  54. %text label hdl part=======================================================
  55. textLabel_1=uilabel(upFig);
  56. textLabel_1.Position=[52 517 296 106];
  57. textLabel_1.Text='YOUR MESSAGE HERE';
  58. textLabel_1.FontSize=24;
  59. textLabel_1.FontWeight='bold';
  60. textLabel_1.HorizontalAlignment='center';
  61.  
  62. textLabel_2=uilabel(upFig);
  63. textLabel_2.Position=[52 367 296 106];
  64. textLabel_2.Text='BACKGROUND COLORS';
  65. textLabel_2.FontSize=24;
  66. textLabel_2.FontWeight='bold';
  67. textLabel_2.HorizontalAlignment='center';
  68.  
  69. textLabel_3=uilabel(upFig);
  70. textLabel_3.Position=[10 310 60 50];
  71. textLabel_3.Text='SIZE';
  72. textLabel_3.FontSize=20;
  73. textLabel_3.FontWeight='bold';
  74. textLabel_3.HorizontalAlignment='center';
  75.  
  76.  
  77. %bkg color hdl part========================================================
  78. for i=1:size(colorList,1)
  79. CL(i)=uiimage(upFig);
  80. CL(i).Position=[10+i*35,365,30,30];
  81. CL(i).UserData=i;
  82. CL(i).ImageSource=['materials\bkg',num2str(i),'.png'];
  83. end
  84. set(CL,'ImageClickedFcn',@bkgChange)
  85.  
  86.  
  87. %reset Size hdl part=======================================================
  88. sizeBtn(1)=uiimage(upFig);
  89. sizeBtn(1).Position=[70 320 168 30];
  90. sizeBtn(1).UserData=1;
  91. sizeBtn(1).ImageSource='materials\cover.jpg';
  92.  
  93. sizeBtn(2)=uiimage(upFig);
  94. sizeBtn(2).Position=[245 320 80 30];
  95. sizeBtn(2).UserData=2;
  96. sizeBtn(2).ImageSource='materials\square.jpg';
  97. set(sizeBtn,'ImageClickedFcn',@sizeChange)
  98.  
  99.  
  100. %==========================================================================
  101. refreshBtn=uiimage(upFig);
  102. refreshBtn.Position=[340 317.5 35 35];
  103. refreshBtn.ImageSource='materials\refresh.png';
  104. set(refreshBtn,'ImageClickedFcn',@changeText)
  105.  
  106. %==========================================================================
  107. upAx=uiaxes('Units','pixels',...
  108. 'parent',upFig,...
  109. 'Position',[50 10 300 300],...
  110. 'Color',[0.99 0.99 0.99],...
  111. 'Box','on', ...
  112. 'XTick',[],...
  113. 'YTick',[],...
  114. 'XLimMode','manual',...
  115. 'YLimMode','manual',...
  116. 'XLim',[0 300],...
  117. 'YLim',[0 300], ...
  118. 'BackgroundColor',[0,0,0],...
  119. 'YDir','reverse');
  120. hold(upAx,'on')
  121.  
  122.  
  123.  
  124. %==========================================================================
  125.  
  126. function bkgChange(~,event)
  127. objNum=event.Source.UserData;
  128. upAx.Color=colorList(objNum,:);
  129. end
  130.  
  131. function sizeChange(~,event)
  132. axesType=event.Source.UserData;
  133. switch axesType
  134. case 1
  135. upAx.Position=[10 120 380 141];
  136. upAx.XLim=[0 380];
  137. upAx.YLim=[0 141];
  138. case 2
  139. upAx.Position=[50 10 300 300];
  140. upAx.XLim=[0 300];
  141. upAx.YLim=[0 300];
  142. end
  143. end
  144. function changeText(~,~)
  145. cla(upAx)
  146. % hold(upAx,'off')
  147. % image(upAx,[-1,0],[-1,0],ones(1,1,3),'visible','off');
  148. % hold(upAx,'on')
  149. textMsg=textArea.Value;
  150. for ii=1:length(textMsg)
  151. tempStr=textMsg{ii};
  152. for jj=1:length(tempStr)
  153. if tempStr(jj)~=' '
  154. roleType=randi(24);
  155. image(upAx,[0,103*0.4]+110+28*(jj-1)-27*(ii-1),...
  156. [0,198*0.4]+10+12*(jj-1)+22*(ii-1),...
  157. materials.imgSet.CData{roleType},...
  158. 'AlphaData',materials.imgSet.AData{roleType},...
  159. 'Interpolation','bilinear')
  160. text(upAx,21+110+28*(jj-1)-27*(ii-1),...
  161. 10+10+12*(jj-1)+22*(ii-1),...
  162. tempStr(jj),'rotation',-38,'FontSize',16,...
  163. 'FontWeight','bold','Color',[0.4,0.3,0.3],...
  164. 'FontAngle','italic','HorizontalAlignment','center');
  165. end
  166. end
  167. end
  168. end
  169. end

另:完整素材包+mat文件+m文件可以在下面下载

百度网盘链接:https://pan.baidu.com/s/1F6Z3_-91_OKtV2zjQUg47Q

注:代码和素材仅作学习用途,勿做他用

若matlab是版本为R2016a以后,R2019a之前,会因为uiimage未被推出而无法正常使用,可以尝试以下代码,该代码依旧需要materials.mat和m文件在同一文件夹:

  1. function upupUiBtn
  2. global textMsg bkgColor colorList axesType
  3. global textArea
  4. textMsg=[];
  5. axesType=2;
  6. bkgColor=[1,1,1];
  7. colorList=[0.9000 0 0.0700
  8. 0.9200 0.3300 0.0800
  9. 0.9600 0.6200 0.7500
  10. 0.9600 0.9400 0.5500
  11. 0.7600 0.8400 0.2500
  12. 0.3800 0.7600 0.7300
  13. 0.4700 0.8100 0.9400
  14. 0.6000 0.0500 0.4900
  15. 1 1 1 ];
  16. %load pic and create pic ==================================================
  17. materials=load('materials.mat');
  18. if ~exist('materials','dir')
  19. mkdir('materials');
  20. end
  21. for i=1:size(colorList,1)
  22. Rc=zeros(60,60);Rc(11:50,11:50)=255.*colorList(i,1);
  23. Gc=zeros(60,60);Gc(11:50,11:50)=255.*colorList(i,2);
  24. Bc=zeros(60,60);Bc(11:50,11:50)=255.*colorList(i,3);
  25. cPic(:,:,1)=Rc;cPic(:,:,2)=Gc;cPic(:,:,3)=Bc;
  26. imwrite(uint8(cPic),['materials\bkg',num2str(i),'.png'])
  27. end
  28. imwrite(materials.imgSet.cover,'materials\cover.jpg')
  29. imwrite(materials.imgSet.square,'materials\square.jpg')
  30. imwrite(materials.imgSet.refresh.CData,'materials\refresh.png','Alpha',materials.imgSet.refresh.AData)
  31.  
  32. %==========================================================================
  33. upFig=uifigure('units','pixels',...
  34. 'position',[320 50 400 600],...
  35. 'Numbertitle','off',...
  36. 'menubar','none',...
  37. 'resize','off',...
  38. 'name','upup',...
  39. 'color',[0.93 0.6 0]);
  40.  
  41. %==========================================================================
  42. textAreaLabel=uilabel(upFig);
  43. textAreaLabel.Position=[55 445 290 100];
  44. textAreaLabel.Text='';
  45. textAreaLabel.BackgroundColor=[0,0,0];
  46. textArea=uitextarea(upFig,'ValueChangedFcn',@changeText);
  47. textArea.Position=[60 450 280 90];
  48. textArea.FontSize=23;
  49.  
  50. %text label hdl part=======================================================
  51. textLabel_1=uilabel(upFig);
  52. textLabel_1.Position=[52 517 296 106];
  53. textLabel_1.Text='YOUR MESSAGE HERE';
  54. textLabel_1.FontSize=24;
  55. textLabel_1.FontWeight='bold';
  56. textLabel_1.HorizontalAlignment='center';
  57.  
  58. textLabel_2=uilabel(upFig);
  59. textLabel_2.Position=[52 367 296 106];
  60. textLabel_2.Text='BACKGROUND COLORS';
  61. textLabel_2.FontSize=24;
  62. textLabel_2.FontWeight='bold';
  63. textLabel_2.HorizontalAlignment='center';
  64.  
  65. textLabel_3=uilabel(upFig);
  66. textLabel_3.Position=[10 310 60 50];
  67. textLabel_3.Text='SIZE';
  68. textLabel_3.FontSize=20;
  69. textLabel_3.FontWeight='bold';
  70. textLabel_3.HorizontalAlignment='center';
  71.  
  72. %bkg color hdl part========================================================
  73. for i=1:size(colorList,1)
  74. CL(i)=uibutton(upFig);
  75. CL(i).Position=[10+i*35,365,30,30];
  76. CL(i).BackgroundColor=[0 0 0];
  77. CL(i).Text='';
  78. CL(i).UserData=i;
  79. CL(i).Icon=['materials\bkg',num2str(i),'.png'];
  80. end
  81. set(CL,'ButtonPushedFcn',@bkgChange)
  82.  
  83. %reset Size hdl part=======================================================
  84. sizeBtn(1)=uibutton(upFig);
  85. sizeBtn(1).Position=[80 320 145 32];
  86. sizeBtn(1).UserData=1;
  87. sizeBtn(1).Text='';
  88. sizeBtn(1).BackgroundColor=[0 0 0];
  89. sizeBtn(1).Icon='materials\cover.jpg';
  90.  
  91. sizeBtn(2)=uibutton(upFig);
  92. sizeBtn(2).Position=[235 320 78 32];
  93. sizeBtn(2).UserData=2;
  94. sizeBtn(2).Text='';
  95. sizeBtn(2).BackgroundColor=[0 0 0];
  96. sizeBtn(2).Icon='materials\square.jpg';
  97. set(sizeBtn,'ButtonPushedFcn',@sizeChange)
  98.  
  99.  
  100. refreshBtn=uibutton(upFig);
  101. refreshBtn.Position=[330 318 35 35];
  102. refreshBtn.Text='';
  103. refreshBtn.BackgroundColor=[0.93 0.6 0];
  104. refreshBtn.Icon='materials\refresh.png';
  105. set(refreshBtn,'ButtonPushedFcn',@changeText)
  106. %==========================================================================
  107. upAx=uiaxes('Units','pixels',...
  108. 'parent',upFig,...
  109. 'Position',[50 10 300 300],...
  110. 'Color',[0.99 0.99 0.99],...
  111. 'Box','on', ...
  112. 'XTick',[],...
  113. 'YTick',[],...
  114. 'XLimMode','manual',...
  115. 'YLimMode','manual',...
  116. 'XLim',[0 300],...
  117. 'YLim',[0 300], ...
  118. 'BackgroundColor',[0,0,0],...
  119. 'YDir','reverse');
  120. hold(upAx,'on')
  121.  
  122.  
  123.  
  124. %==========================================================================
  125.  
  126. function bkgChange(~,event)
  127. objNum=event.Source.UserData;
  128. upAx.Color=colorList(objNum,:);
  129. end
  130.  
  131. function sizeChange(~,event)
  132. axesType=event.Source.UserData;
  133. switch axesType
  134. case 1
  135. upAx.Position=[10 120 380 141];
  136. upAx.XLim=[0 380];
  137. upAx.YLim=[0 141];
  138. case 2
  139. upAx.Position=[50 10 300 300];
  140. upAx.XLim=[0 300];
  141. upAx.YLim=[0 300];
  142. end
  143. end
  144. function changeText(~,~)
  145. cla(upAx)
  146. % hold(upAx,'off')
  147. % image(upAx,[-1,0],[-1,0],ones(1,1,3),'visible','off');
  148. % hold(upAx,'on')
  149. textMsg=textArea.Value;
  150. for ii=1:length(textMsg)
  151. tempStr=textMsg{ii};
  152. for jj=1:length(tempStr)
  153. if tempStr(jj)~=' '
  154. roleType=randi(24);
  155. image(upAx,[0,103*0.4]+110+28*(jj-1)-27*(ii-1),...
  156. [0,198*0.4]+10+12*(jj-1)+22*(ii-1),...
  157. materials.imgSet.CData{roleType},...
  158. 'AlphaData',materials.imgSet.AData{roleType},...
  159. 'Interpolation','bilinear')
  160. text(upAx,21+110+28*(jj-1)-27*(ii-1),...
  161. 10+10+12*(jj-1)+22*(ii-1),...
  162. tempStr(jj),'rotation',-38,'FontSize',16,...
  163. 'FontWeight','bold','Color',[0.4,0.3,0.3],...
  164. 'FontAngle','italic','HorizontalAlignment','center');
  165. end
  166. end
  167. end
  168. end
  169. end

非uiimage版按钮长这样:

以上就是利用Matlab复刻举牌加油小人生成器的详细内容,更多关于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号