Using window API to realize MATLAB screen fetching function

Source: Internet
Author: User
Tags range

Absrtact: How to use the window API to realize the MATLAB screen grab function, introduced in MATLAB How to use the interface of MATLAB and C/A + +, invoke operating system API, extend MATLAB function.

Introduction

The product family of MATLAB is the ideal integrated environment for concept design, algorithm development, modeling simulation and real time implementation developed by MathWorks Company in USA. Because of its complete professional system and advanced design and development ideas, MATLAB has a wide range of application space in a variety of fields.

Although MATLAB in scientific research and industrial technology development has a very wide range of applications, but it is not omnipotent. In some cases, the functions and combinations of MATLAB can not fully meet the requirements of users, but must be realized by invoking the API function of the operating system. Matlab in the design has taken into account this, providing us with the MEX command, can be used to invoke the operating system API function of the C program compiled into a DLL file, that is, MEX file, so that it becomes an extension of Matlab function. In this way, we can call the extension function directly when programming in the MATLAB environment, and achieve the purpose of indirectly calling the operating system API function. This article will demonstrate the above process through how to write a screen grab function for MATLAB.

Mex interface

MEX is the abbreviation of MATLAB executable, namely can execute in MATLAB. This is the interface between MATLAB and other main programming languages such as C/c++,fortran. Ordinary C + + or FORTRAN source program, as long as the addition of a special interface function, you can through the MEX command in MATLAB compiled into a special dynamic link library functions, and this function can be programmed in the MATLAB environment directly call, and MATLAB embedded functions like. This special interface function is equivalent to the main function in C program, is the entrance of the program, the execution of the program starts from this entry function. Its prototype is

void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const mxarray *prhs[])

Where the parameters nlhs and NRHS are the number of output and input variables, the parameters PLHS and PRHS are pointers to output and input variable pointers, PRHS is an array of pointers for input variables of length NRHS, and PLHS is an array of pointers for output variables of length nlhs.

Using window API to realize MATLAB screen fetching function

In essence, the use of C language to invoke window API functions to achieve screen capture function, at the same time, must deal with C language and MATLAB interface problem and C language array storage and the transformation of the array storage in MATLAB.

The following is the C source program, with detailed comments.

Matlab Mex file to snap the screen
Compile to Use>> Mex screensnap.c User32.lib gdi32.lib
Usage:1, >>a=screensnap (0)%exclude the MATLAB window
>>imshow (a);
2, >>a=screensnap (1); %include the Matlab window
>> Imshow (a);
Designed by Darnshong Chenzushang@sina.com
2005,12,18
#include <windows.h>
#include <string.h>
#include "mex.h"
void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const mxarray *prhs[])
{
int cx,cy,recnum;
int dims[3],i,j,k;
Char *pchar,*mloc;
BOOL bshowmatlab;//screen when you screen the main MATLAB window
HWND hwin,hactw;
HDC DC,MEMDC;
RECT RECT;
Hbitmap Hbitm,hold;
Bitmapinfoheader Binfoh;
if (nrhs!=1)//test the input parameters
Mexerrmsgtxt ("Need 1 argument!\n");
if (!mxisdouble (Prhs[0]))
Mexerrmsgtxt ("the input argument must be a numeric!\n");
if (* (double*) (Mxgetdata (prhs[0)) ==0)
Bshowmatlab=false;
Else
Bshowmatlab=true;
Hactw=getforegroundwindow ()//Get the window handle of MATLAB
Hwin=getdesktopwindow ()//Get Desktop Window handle
DC=GETWINDOWDC (Hwin);/Get Desktop Window DC
GetWindowRect (hwin,&rect);//Get Desktop window size
Cx=rect.right-rect.left;
Cy=rect.bottom-rect.top;
mexprintf ("CX:%d cy:%d\n!", Cx,cy);
Memdc=createcompatibledc (DC);//Create a memory DC that is compatible with the desktop window DC
mexprintf ("Handles:%d%d%d\n!", HWIN,DC,MEMDC);
Hbitm=createcompatiblebitmap (dc,cx,cy);//Create compatible bitmap
if (hbitm==0)
Mexerrmsgtxt ("Fail to create a compatible bitmap!\n");
if (!) ( Hold=selectobject (MEMDC, HBITM))//Select the new bitmap into the memory DC
Mexerrmsgtxt ("Compatible Bitmap selection!\n");
Hide the application window.
if (!bshowmatlab)
{
ShowWindow (HACTW, sw_hide); Screen MATLAB main window
Sleep (100);//delay 100ms, because in the process of shielding MATLAB main window can not grasp the screen
}
Copy color data for the entire display into a
Bitmap is selected into a compatible DC.
if (! BitBlt (memdc,0,0,cx,cy,dc,0,0,srccopy))//Copy Desktop window DC to memory DC
Mexerrmsgtxt ("screen to Compat Blt Failed");

dims[0]=cy;//Note the order of Cx,cy
DIMS[1]=CX;
dims[2]=3;
Plhs[0]=mxcreatenumericarray (3,dims,mxuint8_class,mxreal);
Create output variable space to pass image data
Because it is a color image, contains RGB three components, so for three-dimensional data
Pchar= (char*) mxgetdata (Plhs[0]);
Binfoh.bisize=sizeof (Bitmapinfoheader);
BINFOH.BIWIDTH=CX;
Binfoh.biheight=-cy;
Binfoh.biplanes=1;
binfoh.bibitcount=24;
Binfoh.bicompression=bi_rgb;
binfoh.bisizeimage=0;
binfoh.bixpelspermeter=0;
Binfoh.biypelspermeter = 0;
Binfoh.biclrused=0;
binfoh.biclrimportant=0;
Configure Bitmap Information Header structure
Mloc= (char*) Mxmalloc (cx*cy*3);
Application space to store image data Recnum=getdibits (Memdc,hbitm,0,cy,mloc, (bitmapinfo*) &binfoh,dib_rgb_colors);
Copy image data into Mloc space
mexprintf ("Copyed%d lines%d\n!", RECNUM,HBITM);
for (k=0;k<3;k++)
for (j=0;j<cy;j++)
for (i=0;i<cx;i++)
{
pchar[i*cy+j+k*cx*cy]=mloc[(J*cx+i) *3+2-k];
Since the data storage in MATLAB is the first column, and then in the C program after the column is not the same
Image data must be properly processed when copied to an output variable
}
Mxfree (mloc);/free memory space for the application
SelectObject (Hbitm,hold);
DeleteDC (MEMDC);
ReleaseDC (HWIN,DC);
DeleteObject (HBITM);
Do the necessary processing to prevent memory leaks after completion
if (!bshowmatlab)
ShowWindow (HACTW, sw_show);
Restore the display of MATLAB main window
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.