求助:如何用matlab在excel中插入背景图片

sunbader sunbader 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

希望实现的功能:

1 打开指定的excel文件

2 插入指定的背景图片

解决问题的思路:

    采用java控制键盘鼠标实现上述功能。

目前存在的问题:

弹出的对话框中,如何指定文件名

主程序代码如下:

clc,clear all
close all
%%
import java.awt.Robot
mouse=Robot;
%% 
sz=get(0,'screensize')  ;
%% 打开exce1表格,并关闭激活向导
he=actxserver('Excel.Application');
he.WindowState='xlMaximized';           % 最大化
workbook = he.Workbooks.Open('E:\R 软件学习\Matlab\工具箱\图片转表格\Template2.xlsx');
% he.Workbooks.Add;
he.Visible=1;   

%% 控制鼠标移动,并点击页面设置
A=uint8(ScreenCapture([],'screencapture.png'));    %  A=imread('E:\R 软件学习\Matlab\工具箱\图片转表格\Excel_UI.png');
%控制机标移动及点击页面设置
A_YMSZ=imread('E:\R 软件学习\Matlab\工具箱\图片转表格\Excel_YMSZ.png');
c=normxcorr2(rgb2gray(A_YMSZ),rgb2gray(A));
[ypeak,xpeak]=find(c==max(c(:)));
xc=xpeak-floor(size(A_YMSZ,2)/2)%市心与
yc=ypeak-floor(size(A_YMSZ,1)/2)%鼠标移动 至坐标5137,即
mouse.mouseMove(xc,yc);
mouse.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
mouse.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
mouse.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK)
%% 控制鼠标移动,并点击背景
A=uint8(ScreenCapture([],'screencapture.png'));    %  
A_YMSZ=imread('E:\R 软件学习\Matlab\工具箱\图片转表格\Excel_YMSZ_BG.png');
c=normxcorr2(rgb2gray(A_YMSZ),rgb2gray(A));
[ypeak,xpeak]=find(c==max(c(:)));% 中心点% 中心点
mouse.mouseMove(xc,yc);         %鼠标移动至坐标5137,即matlab主页选项卡
mouse.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)  %点击
mouse.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK) % 释放
xc=xpeak-floor(size(A_YMSZ,2)/2);
yc=ypeak-floor(size(A_YMSZ,1)/2);
mouse.mouseMove(xc,yc);
mouse.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
mouse.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK)

子程序(截图)代码如下:

function outputImage=ScreenCapture(subRegion,outputFile)
% SCREENCAPTURE    


    if exist('subRegion','var') & ~isempty(subRegion)
        x=subRegion(1);
        y=subRegion(2);
        w=subRegion(3);
        h=subRegion(4);
    end

    robo = java.awt.Robot;

    if ~exist('subRegion','var') | isempty(subRegion)
        t = java.awt.Toolkit.getDefaultToolkit();
        rectangle = java.awt.Rectangle(t.getScreenSize());
    else
        rectangle = java.awt.Rectangle(x,y,w,h);
    end
    image1 = robo.createScreenCapture(rectangle);
    
    if nargout
        h=image1.getHeight();
        w=image1.getWidth();
        data=image1.getData();
        pix=data.getPixels(0,0,w,h,[]);
        tmp=reshape(pix(:),3,w,h);
        for ii=1:3
            outputImage(:,:,ii)=squeeze(tmp(ii,:,:))';
        end
    end
    
    if exist('outputFile','var')
        filehandle = java.io.File(outputFile);
        javax.imageio.ImageIO.write(image1,'jpg',filehandle);
    end

 

需要使用的匹配图

 

 

 

 

 

...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
sunbader
Matlab学习社区 123,239 成员 309 内容