利用QT实现截屏的四种方法
来源:互联网 发布:免费谈恋爱软件 编辑:程序博客网 时间:2024/06/07 05:11
利用QT实现截屏的四种方法;
一:截取整个屏幕
(1)代码如下:
voidTest::slotGrabFullScreen()
{
QScreen *screen = QGuiApplication::primaryScreen();
QStringfilePathName = "full-";
filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
filePathName += ".jpg";
if(!screen->grabWindow(0).save(filePathName, "jpg"))
{
cout<<"save full screen failed"<<endl;
}
}
(2)效果如下图:
注意:此方法用于保存整个屏幕。
二:截取某个控件(QWidget)
(1)代码如下:
voidTest::slotGrabWidgetScreen()
{
QRectrect = ui.vw->geometry();
QPixmapp = this->grab(QRect(0, 0, 1000, 800));
QStringfilePathName = "widget";
filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
filePathName += ".png";
if(!p.save(filePathName,"png"))
{
cout<<"save widget screen failed"<<endl;
}
}
调用QWidget的grab方法。
(2)效果图:
注意:此方法对截取播放视频的widget无效。播放视频的widget(QVideoWidget)
三:截取视频图片
方法:先截取整个屏幕,保存为图片,然后计算获取到视频在图片的位置。
(1)代码如下:
voidTest::slotCutScreen()
{
// 保存整个屏幕为QPixmap
QScreen *screen = QGuiApplication::primaryScreen();
QStringfilePathName = "cut-";
filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
filePathName += ".png";
QPixmappixmap = screen->grabWindow(0);
if(!pixmap.save(filePathName,"png"))
{
cout<<"cut save png failed"<<endl;
}
// 计算视频的位置和大小
cout<<"Full pixmap width: "<<pixmap.width()<<" height: "<<pixmap.height()<<endl;
QRectgeo = this->geometry();
QRectappGeo = geo; // 整个应用程序在图片中的位置。
cout<<"App x: "<<geo.x()<<" y: "<<geo.y()<<" width: "<<geo.width()<<" height: "<<geo.height()<<endl;
geo = ui.vw->geometry(); // 播放视频在图片中的位置。
cout<<"VW x: "<<geo.x()<<" y: "<<geo.y()<<" width: "<<geo.width()<<" height: "<<geo.height()<<endl;
QWidget *centerWidget = centralWidget(); // QMainWindow在应用程序的位置
QRectcenterRect = centerWidget->geometry();
cout<<"center x: "<<centerRect.x()<<" y: "<<centerRect.y()<<" width: "<<centerRect.width()<<" height: "<<centerRect.height()<<endl;
QRectcopyGeo;
copyGeo.setX(geo.x() + appGeo.x() + centerRect.x()); // x=三个x相加
copyGeo.setY(geo.y() + appGeo.y() + centerRect.y());
copyGeo.setWidth(geo.width());
copyGeo.setHeight(geo.height());
cout<<"VW1 x: "<<copyGeo.x()<<" y: "<<copyGeo.y()<<" width: "<<copyGeo.width()<<" height: "<<copyGeo.height()<<endl;
QPixmappixmapCopy = pixmap.copy(copyGeo); // copy图片
filePathName.prepend("Copy+");
if(!pixmapCopy.save(filePathName,"png"))
{
cout<<"copy cut save png failed"<<endl;
}
}
(2)效果图:整个屏幕
(3)截取视频
注意:此方法有两个弊端
A:此方法截图的视频图片与窗口大小有关。有可能截取出来的像素非常低(播放视频的窗口很小)
B:如果视频的分辨率与窗口大小不一致,则截取出来的图片有黑边框。(看上图左右两边有黑色边框)
三、利用FFMPEG获取视频图片
处理步骤:
1:获取一帧数据
2:利用FFMPEG将YUV格式转换为RGB格式
3:保存图片
(1)源代码
// 以下是两个成员变量。一个是player,一个是侦测帧对象
QMediaPlayer *mPlayer;
QVideoProbe *mVideoProbe;
voidTest::slotGrabMediaScreenFFMPEG()
{
connect(mVideoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(slotProcessFrameFFMPEG(QVideoFrame)));
}
voidTest::slotProcessFrameFFMPEG(constQVideoFrame & buffer)
{
disconnect(mVideoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(slotProcessFrameFFMPEG(QVideoFrame)));
if(!buffer.isValid()) // 数据是否有效
{
cout<<"frame is invalid"<<endl;
connect(mVideoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(slotProcessFrame(QVideoFrame)));
return;
}
QImageimg;
QVideoFrameframe(buffer); // 拷贝数据
frame.map(QAbstractVideoBuffer::ReadOnly); // 将视频缓存映射到内存中
inttotalBytes = frame.width() * frame.height() * 3;
uchar *imageBuffer = (uchar*)malloc(totalBytes);
if(!YV12ToARGB24_FFmpeg(frame.bits(), imageBuffer, frame.width(), frame.height()))
{
cout<<"convert YUV to RGB failed"<<endl;
return;
}
img = QImage(imageBuffer, frame.width(), frame.height(), //frame.bytesPerLine(),
//imageFormat);
QImage::Format_RGB888);
QStringfilePathName = "convert-vedio-";
filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
filePathName += ".png";
if(!img.save(filePathName,"png"))
{
cout<<"save convert vedio screen failed"<<endl;
}
}
boolTest::YV12ToARGB24_FFmpeg(unsignedchar* pYUV,unsignedchar* pBGR24,intwidth,intheight)
{
if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
returnfalse;
AVPicturepFrameYUV, pFrameBGR;
avpicture_fill(&pFrameYUV, pYUV, PIX_FMT_NV12, width, height);
avpicture_fill(&pFrameBGR, pBGR24, AV_PIX_FMT_RGB24, width,height);
structSwsContext* imgCtx = NULL;
imgCtx = sws_getContext(width, height, PIX_FMT_NV12, width, height, AV_PIX_FMT_RGB24, SWS_BICUBIC, 0, 0, 0);
if (imgCtx != NULL){
sws_scale(imgCtx, pFrameYUV.data, pFrameYUV.linesize, 0, height, pFrameBGR.data, pFrameBGR.linesize);
if(imgCtx){
sws_freeContext(imgCtx);
imgCtx = NULL;
}
returntrue;
}
else{
sws_freeContext(imgCtx);
imgCtx = NULL;
returnfalse;
}
}
(2)效果图
注意:这样就把视频给截取出来了。完全和视频一致。
关于FFMPEG的搭建,见
http://blog.csdn.net/huangqi734044860/article/details/60956995
五:结论
终于搞定了。
- 利用QT实现截屏的四种方法
- 利用QT实现截屏的四种方法
- Qt利用QPainter绘制心形以及Qt设置透明窗体的四种方法
- qt学习:四种方法实现界面的显示功能
- 利用同步机制实现读写锁的四种方法
- QT显示图片的四种方法
- Qt实现睡眠的几种方法
- 利用C#播放声音的四种方法
- 四种利用C#播放声音的方法
- 利用 Qt 读取 XML 文件的方法
- 四种实现下载功能的方法
- SWAP方法的四种实现方式
- python实现的四种抽样方法
- 利用Qt实现简单的界面
- qt超易实现录屏程序的方法
- Qt实现环形缓冲区的两种方法
- windows线程同步的总结-------四种实现的方法
- Qt利用QProcess打开路径(含中文路径)的方法
- 中国(北方)大学生程序设计训练赛(第一周)(Problem B: 埃蒙的时空航道-最小割转dp+贪心)
- LeetCode算法题——Reverse Integer
- 斐波那契数列问题(结果正确但运行超时,求解)
- 使用OpenAL和FFMPEG解码并播放音乐
- CUDA编程—硬件基础
- 利用QT实现截屏的四种方法
- NuPlayer处理RTSP请求消息
- 组合算法在接口测试中应用
- Web开发之JS函数
- Java多线程编程中Master-Worker模式的详解
- "std::_List_node_base::reverse()","std::_List_node_base::swap(std::_List_node_base&, std::_List_node
- 【已解决】对JSP页面传值中文乱码的简单解决方法
- Promise实现图片动态加载
- android studio 添加按钮事件的三种方法