Program code
Introduction
It seems difficult to display some images on the video being played. Here, I am trying to display an image on a video. It is not a static image, but also displays several consecutive images dynamically.
Upgrade
I upgraded my cvideo class to dynamically display several images. In addition, the image display transparency must be controlled. Let's take a look at DirectShow.
Background:
The technologies related to DirectShow and image filtering are required to dynamically display images on videos. For example, add the image filter vmr9 (Video mixing Renderer ). Vmr9 is a key that allows us to display images on a video.
Environment creation:
All programs that want to use DirectShow must contain the dshow. h header file and use the strmiids. Lib library file.
In-depth DirectShow:
Image Display Technology on videos is based on ivmrmixerbitmap9 class, which has only three member functions.
Another structure, vmr9alphabitmap, is used to transmit data in functions. Some vmr9alphabitmap members are as follows:
DWORD dwflags;
HDC;
Rect rsrc;
Vmr9normalizedrect rdest;
Float falpha;
Colorref clrsrckey;
Now let's talk about how to use these. Let's see how to introduce it at the beginning. First, you need to know:
The device description HDC of the selected (SelectObject) image,
Other important aspects are the image size,
The third thing is to display the image position (a rect) on the video ),
Transparency: Specifies the transparency of the image on the video,
Hide the color value of the background color of an image. Set it to the background color. You can not only hide the background, but also give any color value. Then, the color can be displayed as a transparent color on the video. Finally, set the flag for adding data.
Now some of them are okay, just call setalphabitmap.
For more information, visit the blog.
Basic initialization:
First, we need to create an object for image management and filtering (filtering image management is used to control filtering and data update ). You can call cocreateinstance. In this way, we create an image filter management object and get a pointer to the igraphbuilder class. This class is used to create a user filter image (filtering graphics is all the functions of DirectShow to filter and display image files ).
Cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server,
Iid_igraphbuilder, (void **) & pgraph );
After creating the filter graph manager, the other important issue is the addition of vmr9 in the filter graph. but before addition, we need to create it, and after creation, we will add that filter (vmr9) to our filter graph.
Cocreateinstance (clsid_videomixingrenderer9, null, clsctx_inproc,
Iid_ibasefilter, (void **) & pvmr );
Pgraph-> addfilter (pvmr, l "video ");
Now you need to define vmr9. In this case, we set a video to be displayed in the provided window and set the video location.
Pvmr-> QueryInterface (iid_ivmrfilterconfig9, (void **) & pconfig );
Pconfig-> setrenderingmode (vmr9mode_windowless );
Pvmr-> QueryInterface (iid_ivmrwindowlesscontrol9, (void **) & PwC );
PwC-> setvideoposition (null, prect );
PwC-> setvideoclippingwindow (pparentwnd-> m_hwnd );
The following ivmrmixerbitmap9 is a tool for displaying images on a video. And the bitmap information structure contains the data of the image to be displayed. Here, 0 is cleared during the first call.
PwC-> QueryInterface (iid_ivmrmixerbitmap9, (lpvoid *) & pbmp );
Zeromemory (& BMP info, sizeof (BMP info ));
The following are important classes in any DirectShow program, because it provides tools to control data updates through imediacontrol and focus on any important events. For example, if the playback ends, the class that provides this function is imediaeventex. To use this class, you must set a window for accepting events. Other important classes include imediaseeking, which provides the function of searching for media files.
Pgraph-> QueryInterface (iid_imediacontrol, (void **) & PMC );
Pgraph-> QueryInterface (iid_imediaeventex, (void **) & PME );
PME-> setpolicywindow (oahwnd) pparentwnd-> getparent ()-> m_hwnd,
Wm_graphnotify, 0 );
Pgraph-> QueryInterface (iid_imediaseeking, (void **) & PMS );
Now create the custom filter graph. And then run it. After this, the video will start playing in our given window.
Pgraph-> renderfile (mfiletoplay, null );
PMC-> Run ();
Show image:
What we need to do is to select the image device handle, image size, and transparent background color. Start now. Obtain the handle of the video window. Now you need another device, which must be compatible with the video window handle. Now select the image in the device.
CDC * PDC = getdc ();
CDC mcompatibledc;
Mcompatibledc. createcompatibledc (PDC );
Mcompatibledc. SelectObject (hbitmap );
Crect resrc (0, 0, mbitmapwidth, mbitmapheight );
Now, the actual work of displaying images starts. Introduce bitmap information structure and set parameters, such as setting tags, device handles and resources, and image size.
BMP info. dwflags | = vmrbitmap_hdc;
BMP info. HDC = PDC-> m_hdc;
BMP info. rsrc = rectsize; // size of the image size
Set the rectangular area of the image to be displayed on the screen.
// Rdest specifies the rectangle of the target synthetic area (0.0f to 1.0f)
BMP info. rdest. Right = 1.0f;
BMP info. rdest. Left = 1.0f-0.3;
BMP info. rdest. Top = 1.0-0.3;
BMP info. rdest. Bottom = 1.0f;
Set the transparency level. // set the Transparency Level (1.0 opacity, 0.0 transparency)
BMP info. falpha = 0.0;
Set the color value and flag. Here it is set to green, which means that the green on any image will be displayed as transparent.
// Set colorref so that the bitmap's outer frame is transparent.
BMP info. dwflags | = vmrbitmap_srccolorkey;
BMP info. clrsrckey = RGB (0,255, 0 );
Pbmp-> setalphabitmap (& BMP info );
Code:
I tried to simplify it as much as possible. I created a cvideo class. This class hides all complex parts. You only need to call the class method, which solves all the problems.
This class has the following methods:
Void initinterfaces (BSTR mfiletoplay, cwnd * pparentwnd, crect * prect );
Void clean (void );
Void play (void );
Void showimage (CDC * PDC, crect rectsize );
Void hideimage (void );
Void settransparency (INT tansparencylevel)
Void animate ();
Initinterfaces () encapsulates all methods for establishing DirectShow images.
Play (), play a video
Showimage () only provides the device pointer (CDC * PDC), which selects the image to be displayed.
Hideimage () hides or displays an image.
Settransparency (INT transparencylevel) controls the transparency of an image on the video.
Animate () updates the rectangular area of the displayed image and displays the animation effect.
At last, clean () releases the DirectShow class object.