Overlay a picture collection on a video file that is playing

Source: Internet
Author: User
Tags transparent color

Zz:

Http://blog.csdn.net/diskonline/archive/2008/04/10/2277828.aspx

Introduced
It seems difficult to display some images on a video that is playing. Here, I try to display a picture on a video. Not a static picture, but also a dynamic display of several consecutive pictures.
Upgrade
I upgraded my Cvideo class to animate a few pictures. Not only these, but also control the transparency of the picture display. Take a look at "deep DirectShow" and you'll see what you can do.
Background:
The dynamic display of pictures on video requires DirectShow techniques and filtering techniques for the image. For example, add graphics filter VMR9 (video mixing renderer). VMR9 is a key that allows us to display pictures on a video.
Setting up the Environment:
All programs that want to use DirectShow must contain Dshow.h header files and use Strmiids.lib library files.
Deep DirectShow:
The image technology displayed on the video is based on the IVMRMixerBitmap9 class, which has only three member functions.
There is also a structure vmr9alphabitmap that is used to transmit data in functions. Some of the VMR9ALPHABITMAP members are as follows:
DWORD dwflags;
HDC HDC;
RECT rsrc;
Vmr9normalizedrect rdest;
FLOAT Falpha;
COLORREF Clrsrckey;

Now say how to use these. Let's see how we can introduce them at the beginning. First, you need to know:
We selected (SelectObject) pictures of the device description hdc,
Other important is the size of the picture,
The third thing is the location of the picture on the video (a rect),
The degree of transparency, specify the transparency of the picture on the video,
hides the color value of a picture's background colors; set it to a background color; you can give any color value, not just a hidden background, and then the color can be displayed as a transparent color on the video, and finally, set the flag to add data to.
Now some are OK, just call Setalphabitmap ().
to learn more, visit your blog.
Basic initialization:
First, we need to create an image management filter object (filtering image management is used to control filtering and data updates). Implemented by calling CoCreateInstance (). In this way, we set up an image filtering management object and get a pointer to the Igraphbuilder class. This class is used to create a user filtering image (filtering graphics is the DirectShow filter to display the entire function of the image file).
CoCreateInstance (Clsid_filtergraph, NULL, Clsctx_inproc_server,
Iid_igraphbuilder, (void * *) &pgraph);
After creating the Filter Graph Manager, the other important issue are the addition of VMR9 in the filter graph. But before addition, we need to create it, and after creation, we'll add this 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. As is the case here, we set the video to display a video in the provided window, setting the 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 are used to provide a tool for displaying pictures on video. and the bitmap information structure contains the data to display the picture. Here for the first time call Shidu Qing 0.
Pwc->queryinterface (IID_IVMRMIXERBITMAP9, (LPVOID *) &pbmp);
ZeroMemory (&bmpinfo, sizeof (Bmpinfo));

The following are classes that are important in any DirectShow program because they provide the tools to control data updates through IMediaControl and to focus on any important events. For example, the end of a file playback, the class that provides this feature is Imediaeventex. To use this class, you need to set a window that accepts events. Other important classes have imediaseeking that provide the ability to find media files.
Pgraph->queryinterface (Iid_imediacontrol, void * * *) &PMC);
Pgraph->queryinterface (Iid_imediaeventex, void * * *) &PME);
Pme->setnotifywindow ((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'll start playing in our given window.
Pgraph->renderfile (Mfiletoplay, NULL);
Pmc->run ();

Show Pictures:
What we need to do is to select the picture device handle, picture size, and need the transparent background color of the picture. Start now. First get the handle to play the video window. Now you need another device that must be compatible with the video window's handle and now select the picture 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 the picture begins. Introduce bitmap information structure and set parameters, for example, set tag, set device handle and resource, picture size, etc.
Bmpinfo.dwflags |= Vmrbitmap_hdc;
BMPINFO.HDC = pdc->m_hdc;
BMPINFO.RSRC = rectsize; Size of the image picture

Set the rectangular area where the picture is displayed on the screen

Rdest the rectangle (0.0f to 1.0f) of the specified destination synthetic region
BmpInfo.rDest.right = 1.0f;
BmpInfo.rDest.left = 1.0f-0.3;
BmpInfo.rDest.top =1.0-0.3;
BmpInfo.rDest.bottom = 1.0f;
Set the transparency level. Set the level of transparency (1.0 opaque, 0.0 transparent)
Bmpinfo.falpha = 0.0;

Set the color values and tags, which are set to green, which means that any picture on the green will be displayed as transparent.
Set COLORREF so that the outer frame of the bitmap is transparent
Bmpinfo.dwflags |= Vmrbitmap_srccolorkey;
Bmpinfo.clrsrckey = RGB (0, 255, 0);
Pbmp->setalphabitmap (&bmpinfo);

Use code:
I tried to simplify as much as possible. I set up a Cvideo class. This class hides all the complex parts. All you need to do is call the method of the class, and the method in the class solves all the problems.
The 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 the methods of DirectShow image creation.
Play (), playing a video
ShowImage () simply provides a device pointer (cdc* PDC), which selects the picture to display.
Hideimage () Hides or displays a picture.
settransparency (int transparencylevel) controls the transparency of the picture on the video.
Animate () Update the rectangular area of the display picture to show the animation effect
At last, clean () releases the DirectShow class object.

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.