C + + programming uses the QT5 SDK to record the screen and display

Source: Internet
Author: User
Tags home screen

In the project, we have the need to record the screen, there are several ways to record the screen, can be based on Windows API BitBlt, FFmpeg Avdevice, DirectX and QT, this article explains the QT5-based capture screen. The advantages of QT relative to other kinds of screen-catching are better cross-platform, simple interface, inferior to DirectX capture screen, but DirectX can only be windows. After testing DirectX capture screen speed is the fastest, the other interface speed is basically the same.


QT 1080P screen capture on my computer consumes 60~80 milliseconds, 720p consumes 30~50, that is, if you capture screen 1080 I record up to 10 frames a second. This performance should be good, otherwise it will cause blocking.


    • Get the Home screen first and then grab the screen and save it to Qpixmap

Get home Screen

QScreen *SCR = qguiapplication::p rimaryscreen ();

Screen capture

Qpixmap pix= Scr->grabwindow (qapplication::d esktop ()->winid ());


    • Convert Qpixmap to qimage with direct access to RGB data

Just two lines of code is very simple, after the capture of the data in the PIX, the Qpixmap converted to Qimage can be taken to the RGB data.

Qimage img= pix.toimage ();

Uchar *date = Img.bits ();


    • Qimage Format Analysis

After taking the screenshot data, we are going to encode compression, encoding can use ffmpeg, need to determine the data format to get, can be obtained through the Img.format members, my side is format_rgb32. It is also important to note that the image data in Qt does not have to be continuously stored per pixel, each line is 4 aligned, and the simplest solution is to ensure that the width of your image is a multiple of 4. If there is no guarantee, you can get the number of bytes per row by Img.bytesperline ().


    • If you want to draw the desktop to Qwidget test, overload the PaintEvent function

void Qtscreen::p aintevent (qpaintevent *event)

{

QScreen *SCR = qguiapplication::p rimaryscreen ();

Screen capture

Qpixmap pix= Scr->grabwindow (qapplication::d esktop ()->winid ());

Qimage img= pix.toimage ();


Qpainter p;

P.begin (this);

P.drawimage (qpoint (0, 0), Qi);

P.end ();

}



More information can also focus on my 51CTO video course

Summer Teacher's Classroom http://edu.51cto.com/lecturer/index/user_id-12016059.html

C + + cross-platform development and audio and video combat theme packagesC + + combat ffmpeg Audio Video Coding video lesson-based on QT5 and FFmpeg SDK

Http://edu.51cto.com/course/10359.html

650) this.width=650; "src=" Https://s1.51cto.com/images/201706/30/1406b01a322070e017b12a6e0f05c8d9.png "style=" FLOAT:LEFT;WIDTH:500PX;HEIGHT:299PX, "title=" "alt=" 1406b01a322070e017b12a6e0f05c8d9.png "width=" "vspace=" 0 " Hspace= "0" height= "299" border= "0"/>







This article is from the "Xia Chaojun" blog, make sure to keep this source http://xiacaojun.blog.51cto.com/12016059/1951341

C + + programming uses the QT5 SDK to record the screen and display

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.