OSG中抓取屏幕保存为图片
来源:互联网 发布:mysql 导入数据乱码 编辑:程序博客网 时间:2024/05/22 12:43
由于需要自动抓取OSG中的场景截图,对OSG掌握不好,花费了一些时间才算搞定。
首先,OSG在类osgViewer::ScreenCaptureHandler中默认情况下按下c(小写c)可以截取单张图片,而且实现得比较全面,在按下C(大写c)时可以连续截图;经过继承osgViewer::ScreenCaptureHandler类和osgViewer::ScreenCaptureHandler::CaptureOperation类,由于需要自动在某个时刻保存,所以些了一个自定义的事件,并将事件压入OSG的事件队列。
上代码:
ScreenCapture.h文件
#pragma once#ifndef SCREENCAPTURE_H#define SCREENCAPTURE_H#include <string>#include <osgDB/WriteFile>#include <osgViewer/ViewerEventHandlers>class CScreenCapture : public osgViewer::ScreenCaptureHandler{public:class WriteToImageFile : public osgViewer::ScreenCaptureHandler::CaptureOperation{public:enum SavePolicy{OVERWRITE,SEQUENTIAL_NUMBER// ... any others?};WriteToImageFile(const std::string& filename, const std::string& extension, SavePolicy savePolicy = SEQUENTIAL_NUMBER);virtual void operator()(const osg::Image& image, const unsigned int context_id);void setFileName(const std::string &filename){ _filename = filename; }void setSavePolicy(SavePolicy savePolicy) { _savePolicy = savePolicy; }SavePolicy getSavePolicy() const { return _savePolicy; }protected:WriteToImageFile& operator = (const WriteToImageFile&) { return *this; }std::string _filename;std::string _extension;SavePolicy _savePolicy;std::vector<unsigned int> _contextSaveCounter;};CScreenCapture(CaptureOperation* defaultOperation = 0, int numFrames = 1);virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);void setFileName(const std::string& filename );};#endif // SCREENCAPTURE_H
ScreenCapture.cpp文件
/***********************************************************************************************************************************************************/#include "stdafx.h"#include "ScreenCapture.h"CScreenCapture::CScreenCapture(CaptureOperation* defaultOperation /*= 0*/, int numFrames/* = 1*/):ScreenCaptureHandler( defaultOperation, numFrames ){}bool CScreenCapture::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ){osgViewer::ViewerBase* viewer = dynamic_cast<osgViewer::View*>(&aa)->getViewerBase();if (!viewer) return false;switch(ea.getEventType()){case (osgGA::GUIEventAdapter::FRAME):{// Booleans aren't the best way of doing this, but I want to do// the actual adding here because I don't want to require// startCapture() take a viewer as argument, which could not be// the right one.if (_startCapture){// Start capturing with the currently set number of frames.// If set to -1 it will capture continuously, if set to >0// it will capture that number of frames._startCapture = false;addCallbackToViewer(*viewer);}else if (_stopCapture){_stopCapture = false;removeCallbackFromViewer(*viewer);}break;}case ( osgGA::GUIEventAdapter::USER )://自定义的事件,需要进入OSG的事件队列{const OwnDefineEventAdpater* adpater= dynamic_cast< const MyEventAdpater *>(ea.getUserData());switch(adpater->m_eventType){case SAVE_IMAGE:// 保存图片的事件{if (getFramesToCapture() == 0){setFramesToCapture(1);}addCallbackToViewer(*viewer);return true;}default:break;}//if (adpater->m_eventType == SaveImage )//{//// Check that we will capture at least one frame.//// Just check for ==0, because >0 is means we're already//// capturing and <0 means it will capture all frames.//if (getFramesToCapture() == 0)//{//setFramesToCapture(1);//}//addCallbackToViewer(*viewer);//return true;//}}default:break;}return false;}void CScreenCapture::setFileName( const std::string& filename ){CScreenCapture::WriteToImageFile* wtf = dynamic_cast<CScreenCapture::WriteToImageFile*>(getCaptureOperation());wtf->setFileName( filename );}void CScreenCapture::WriteToImageFile::operator()( const osg::Image& image, const unsigned int context_id )//仿函数实现图片保存{if ( !_filename.empty() ){osgDB::writeImageFile( image, _filename );}}CScreenCapture::WriteToImageFile::WriteToImageFile( const std::string& filename, const std::string& extension, SavePolicy savePolicy /*= SEQUENTIAL_NUMBER*/ ): _filename( filename ),_extension( extension ),_savePolicy( savePolicy ){}
1 0
- OSG中抓取屏幕保存为图片
- OSG中抓取屏幕保存为图片
- Java抓取屏幕保存为图片
- 抓取屏幕保存为AVI文件
- vs下抓取屏幕选定部分的图片 并保存
- VC将屏幕区域保存为图片
- 将屏幕保存为图片的方法
- java实现截取屏幕保存为图片
- 抓取图片及时保存
- 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
- 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
- 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
- 用Cocoa截取屏幕内容并保存为图片
- 用Cocoa截取屏幕内容并保存为图片(z)
- android 保存当前屏幕或者view为图片bitmap
- iOS 实现截屏效果 OC保存屏幕为图片
- python抓取网页中图片并保存到本地
- python抓取网页中图片并保存到本地
- Yale开放课程博弈论5
- naze32 MWC
- LeetCode-Partition List
- 动态添加文字
- 出现java.lang.UnsupportedClassVersionError 错误的原因
- OSG中抓取屏幕保存为图片
- 下拉菜单
- IOS学习笔记29—提示框第三方库之MBProgressHUD
- Host SMBus controller not enabled的解决方法
- oracle 存储过程中变量名和表的字段名同名时,优先将字段名当做表的字段名
- 九度oj-1008-最短路径问题
- Linux——虚拟内存
- ZOJ Problem Set - 1203
- hdu 3068最长回文子串