首頁 > 軟體

利用Matlab復刻舉牌加油小人生成器

2022-03-02 13:00:15

突然發現cla函數也可以應用到app designer控制元件上,因而對部分內容做出更改。

手癢,用matlab app designer復刻了一款舉牌加油小人生成器,效果如下:

該程式可以生成正方形和長方形舉牌小人,並且可以自己設定背景顏色(點選那些有顏色的小框框),點選綠色旋轉按鈕可以重新整理生成的圖片。

目前需要存圖還是需要通過uiaxes自帶的toolbar(下圖右上角所示):

使用該程式需要小人的素材包,我已經將素材整理為materials.mat檔案,網路硬碟下載連結如下:

連結:https://pan.baidu.com/s/1ISloOsq8bIVDjm8TzRPlKw 

提取碼:1234

使用時需要把materials.mat和m檔案放在同一資料夾

完整程式碼

function upup
global textMsg bkgColor colorList axesType
global textArea
textMsg=[];
axesType=2;
bkgColor=[1,1,1];
colorList=[0.9000         0    0.0700
    0.9200    0.3300    0.0800
    0.9600    0.6200    0.7500
    0.9600    0.9400    0.5500
    0.7600    0.8400    0.2500
    0.3800    0.7600    0.7300
    0.4700    0.8100    0.9400
    0.6000    0.0500    0.4900
    1         1         1     ];


%load pic and create pic ==================================================
materials=load('materials.mat');
if ~exist('materials','dir')
   mkdir('materials');
end
for i=1:size(colorList,1)
    Rc=zeros(60,60);Rc(11:50,11:50)=255.*colorList(i,1);
    Gc=zeros(60,60);Gc(11:50,11:50)=255.*colorList(i,2);
    Bc=zeros(60,60);Bc(11:50,11:50)=255.*colorList(i,3);
    cPic(:,:,1)=Rc;cPic(:,:,2)=Gc;cPic(:,:,3)=Bc;
    imwrite(uint8(cPic),['materialsbkg',num2str(i),'.png']) 
end
imwrite(materials.imgSet.cover,'materialscover.jpg')
imwrite(materials.imgSet.square,'materialssquare.jpg')
imwrite(materials.imgSet.refresh.CData,'materialsrefresh.png','Alpha',materials.imgSet.refresh.AData)



%==========================================================================
upFig=uifigure('units','pixels',...
    'position',[320 50 400 600],...
    'Numbertitle','off',...
    'menubar','none',...
    'resize','off',...
    'name','upup',...
    'color',[0.93 0.6 0]);

%==========================================================================
textAreaLabel=uilabel(upFig);
textAreaLabel.Position=[55 445 290 100];
textAreaLabel.Text='';
textAreaLabel.BackgroundColor=[0,0,0];
textArea=uitextarea(upFig,'ValueChangedFcn',@changeText);
textArea.Position=[60 450 280 90];
textArea.FontSize=23;

%text label hdl part=======================================================
textLabel_1=uilabel(upFig);
textLabel_1.Position=[52 517 296 106];
textLabel_1.Text='YOUR MESSAGE HERE';
textLabel_1.FontSize=24;
textLabel_1.FontWeight='bold';
textLabel_1.HorizontalAlignment='center';

textLabel_2=uilabel(upFig);
textLabel_2.Position=[52 367 296 106];
textLabel_2.Text='BACKGROUND COLORS';
textLabel_2.FontSize=24;
textLabel_2.FontWeight='bold';
textLabel_2.HorizontalAlignment='center';

textLabel_3=uilabel(upFig);
textLabel_3.Position=[10 310 60 50];
textLabel_3.Text='SIZE';
textLabel_3.FontSize=20;
textLabel_3.FontWeight='bold';
textLabel_3.HorizontalAlignment='center';


%bkg color hdl part========================================================
for i=1:size(colorList,1)
    CL(i)=uiimage(upFig);
    CL(i).Position=[10+i*35,365,30,30];
    CL(i).UserData=i;
    CL(i).ImageSource=['materialsbkg',num2str(i),'.png'];
end
set(CL,'ImageClickedFcn',@bkgChange)


%reset Size hdl part=======================================================
sizeBtn(1)=uiimage(upFig);
sizeBtn(1).Position=[70 320 168 30];
sizeBtn(1).UserData=1;
sizeBtn(1).ImageSource='materialscover.jpg';

sizeBtn(2)=uiimage(upFig);
sizeBtn(2).Position=[245 320 80 30];
sizeBtn(2).UserData=2;
sizeBtn(2).ImageSource='materialssquare.jpg';
set(sizeBtn,'ImageClickedFcn',@sizeChange)


%==========================================================================
refreshBtn=uiimage(upFig);
refreshBtn.Position=[340 317.5 35 35];
refreshBtn.ImageSource='materialsrefresh.png';
set(refreshBtn,'ImageClickedFcn',@changeText)

%==========================================================================
upAx=uiaxes('Units','pixels',...
      'parent',upFig,...
      'Position',[50 10 300 300],...
      'Color',[0.99 0.99 0.99],...
      'Box','on', ...
      'XTick',[],...
      'YTick',[],...
      'XLimMode','manual',...
      'YLimMode','manual',...
      'XLim',[0 300],...
      'YLim',[0 300], ...
      'BackgroundColor',[0,0,0],...
      'YDir','reverse');
hold(upAx,'on')



%==========================================================================

    function bkgChange(~,event)
        objNum=event.Source.UserData;
        upAx.Color=colorList(objNum,:); 
    end

    function sizeChange(~,event)
        axesType=event.Source.UserData;
        switch axesType
            case 1
                upAx.Position=[10 120 380 141];
                upAx.XLim=[0 380];
                upAx.YLim=[0 141];     
            case 2
                upAx.Position=[50 10 300 300];
                upAx.XLim=[0 300];
                upAx.YLim=[0 300];
        end
    end
        
    function changeText(~,~)
        cla(upAx)
%         hold(upAx,'off')
%         image(upAx,[-1,0],[-1,0],ones(1,1,3),'visible','off');
%         hold(upAx,'on')
        textMsg=textArea.Value;
        for ii=1:length(textMsg)
            tempStr=textMsg{ii};
            for jj=1:length(tempStr)
                if tempStr(jj)~=' '
                    roleType=randi(24);
                    image(upAx,[0,103*0.4]+110+28*(jj-1)-27*(ii-1),...
                        [0,198*0.4]+10+12*(jj-1)+22*(ii-1),...
                        materials.imgSet.CData{roleType},...
                        'AlphaData',materials.imgSet.AData{roleType},...
                        'Interpolation','bilinear')
                    text(upAx,21+110+28*(jj-1)-27*(ii-1),...
                        10+10+12*(jj-1)+22*(ii-1),...
                        tempStr(jj),'rotation',-38,'FontSize',16,...
                        'FontWeight','bold','Color',[0.4,0.3,0.3],...
                        'FontAngle','italic','HorizontalAlignment','center');
                end
            end
        end
    end
end

另:完整素材包+mat檔案+m檔案可以在下面下載

百度網路硬碟連結:https://pan.baidu.com/s/1F6Z3_-91_OKtV2zjQUg47Q

注:程式碼和素材僅作學習用途,勿做他用

若matlab是版本為R2016a以後,R2019a之前,會因為uiimage未被推出而無法正常使用,可以嘗試以下程式碼,該程式碼依舊需要materials.mat和m檔案在同一資料夾:

function upupUiBtn
global textMsg bkgColor colorList axesType
global textArea
textMsg=[];
axesType=2;
bkgColor=[1,1,1];
colorList=[0.9000         0    0.0700
    0.9200    0.3300    0.0800
    0.9600    0.6200    0.7500
    0.9600    0.9400    0.5500
    0.7600    0.8400    0.2500
    0.3800    0.7600    0.7300
    0.4700    0.8100    0.9400
    0.6000    0.0500    0.4900
    1         1         1     ];
%load pic and create pic ==================================================
materials=load('materials.mat');
if ~exist('materials','dir')
   mkdir('materials');
end
for i=1:size(colorList,1)
    Rc=zeros(60,60);Rc(11:50,11:50)=255.*colorList(i,1);
    Gc=zeros(60,60);Gc(11:50,11:50)=255.*colorList(i,2);
    Bc=zeros(60,60);Bc(11:50,11:50)=255.*colorList(i,3);
    cPic(:,:,1)=Rc;cPic(:,:,2)=Gc;cPic(:,:,3)=Bc;
    imwrite(uint8(cPic),['materialsbkg',num2str(i),'.png']) 
end
imwrite(materials.imgSet.cover,'materialscover.jpg')
imwrite(materials.imgSet.square,'materialssquare.jpg')
imwrite(materials.imgSet.refresh.CData,'materialsrefresh.png','Alpha',materials.imgSet.refresh.AData)

%==========================================================================
upFig=uifigure('units','pixels',...
    'position',[320 50 400 600],...
    'Numbertitle','off',...
    'menubar','none',...
    'resize','off',...
    'name','upup',...
    'color',[0.93 0.6 0]);

%==========================================================================
textAreaLabel=uilabel(upFig);
textAreaLabel.Position=[55 445 290 100];
textAreaLabel.Text='';
textAreaLabel.BackgroundColor=[0,0,0];
textArea=uitextarea(upFig,'ValueChangedFcn',@changeText);
textArea.Position=[60 450 280 90];
textArea.FontSize=23;

%text label hdl part=======================================================
textLabel_1=uilabel(upFig);
textLabel_1.Position=[52 517 296 106];
textLabel_1.Text='YOUR MESSAGE HERE';
textLabel_1.FontSize=24;
textLabel_1.FontWeight='bold';
textLabel_1.HorizontalAlignment='center';

textLabel_2=uilabel(upFig);
textLabel_2.Position=[52 367 296 106];
textLabel_2.Text='BACKGROUND COLORS';
textLabel_2.FontSize=24;
textLabel_2.FontWeight='bold';
textLabel_2.HorizontalAlignment='center';

textLabel_3=uilabel(upFig);
textLabel_3.Position=[10 310 60 50];
textLabel_3.Text='SIZE';
textLabel_3.FontSize=20;
textLabel_3.FontWeight='bold';
textLabel_3.HorizontalAlignment='center';

%bkg color hdl part========================================================
for i=1:size(colorList,1)
    CL(i)=uibutton(upFig);
    CL(i).Position=[10+i*35,365,30,30];
    CL(i).BackgroundColor=[0 0 0];
    CL(i).Text='';
    CL(i).UserData=i;
    CL(i).Icon=['materialsbkg',num2str(i),'.png'];
end
set(CL,'ButtonPushedFcn',@bkgChange)

%reset Size hdl part=======================================================
sizeBtn(1)=uibutton(upFig);
sizeBtn(1).Position=[80 320 145 32];
sizeBtn(1).UserData=1;
sizeBtn(1).Text='';
sizeBtn(1).BackgroundColor=[0 0 0];
sizeBtn(1).Icon='materialscover.jpg';

sizeBtn(2)=uibutton(upFig);
sizeBtn(2).Position=[235 320 78 32];
sizeBtn(2).UserData=2;
sizeBtn(2).Text='';
sizeBtn(2).BackgroundColor=[0 0 0];
sizeBtn(2).Icon='materialssquare.jpg';
set(sizeBtn,'ButtonPushedFcn',@sizeChange)


refreshBtn=uibutton(upFig);
refreshBtn.Position=[330 318 35 35];
refreshBtn.Text='';
refreshBtn.BackgroundColor=[0.93 0.6 0];
refreshBtn.Icon='materialsrefresh.png';
set(refreshBtn,'ButtonPushedFcn',@changeText)
%==========================================================================
upAx=uiaxes('Units','pixels',...
      'parent',upFig,...
      'Position',[50 10 300 300],...
      'Color',[0.99 0.99 0.99],...
      'Box','on', ...
      'XTick',[],...
      'YTick',[],...
      'XLimMode','manual',...
      'YLimMode','manual',...
      'XLim',[0 300],...
      'YLim',[0 300], ...
      'BackgroundColor',[0,0,0],...
      'YDir','reverse');
hold(upAx,'on')



%==========================================================================

    function bkgChange(~,event)
        objNum=event.Source.UserData;
        upAx.Color=colorList(objNum,:); 
    end

    function sizeChange(~,event)
        axesType=event.Source.UserData;
        switch axesType
            case 1
                upAx.Position=[10 120 380 141];
                upAx.XLim=[0 380];
                upAx.YLim=[0 141];     
            case 2
                upAx.Position=[50 10 300 300];
                upAx.XLim=[0 300];
                upAx.YLim=[0 300];
        end
    end
        
    function changeText(~,~)
        cla(upAx)
%         hold(upAx,'off')
%         image(upAx,[-1,0],[-1,0],ones(1,1,3),'visible','off');
%         hold(upAx,'on')
        textMsg=textArea.Value;
        for ii=1:length(textMsg)
            tempStr=textMsg{ii};
            for jj=1:length(tempStr)
                if tempStr(jj)~=' '
                    roleType=randi(24);
                    image(upAx,[0,103*0.4]+110+28*(jj-1)-27*(ii-1),...
                        [0,198*0.4]+10+12*(jj-1)+22*(ii-1),...
                        materials.imgSet.CData{roleType},...
                        'AlphaData',materials.imgSet.AData{roleType},...
                        'Interpolation','bilinear')
                    text(upAx,21+110+28*(jj-1)-27*(ii-1),...
                        10+10+12*(jj-1)+22*(ii-1),...
                        tempStr(jj),'rotation',-38,'FontSize',16,...
                        'FontWeight','bold','Color',[0.4,0.3,0.3],...
                        'FontAngle','italic','HorizontalAlignment','center');
                end
            end
        end
    end
end

非uiimage版按鈕長這樣:

以上就是利用Matlab復刻舉牌加油小人生成器的詳細內容,更多關於Matlab舉牌加油小人生成器的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com