mirror of
https://onedev.site.tesses.net/tesses-framework/tessesframework-gfx
synced 2026-02-08 08:25:46 +00:00
Change device to webcam
This commit is contained in:
@@ -10,33 +10,32 @@ using namespace Tesses::Framework::Streams;
|
|||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
Tesses::Framework::TF_Init();
|
Tesses::Framework::TF_Init();
|
||||||
auto devs = Device::GetDevices();
|
auto devs = Webcam::GetWebcams();
|
||||||
for(auto dev : devs)
|
for(auto dev : devs)
|
||||||
{
|
{
|
||||||
for(auto res : dev.Resolutions)
|
for(auto res : dev.Resolutions)
|
||||||
{
|
{
|
||||||
|
|
||||||
Device dev2(dev.Device, res,10);
|
Webcam dev2(dev.Device, res,10);
|
||||||
dev2.Open();
|
dev2.Open();
|
||||||
usleep(5000000);
|
usleep(5000000);
|
||||||
auto frame = dev2.ReadFrame();
|
Image frame;
|
||||||
if(frame != nullptr)
|
Image clock;
|
||||||
{
|
Image clock2;
|
||||||
Image clock;
|
dev2.ReadFrame(&frame);
|
||||||
Image clock2;
|
auto strm = std::make_shared<FileStream>("capture.png","wb");
|
||||||
auto strm = std::make_shared<FileStream>("capture.png","wb");
|
auto date = Tesses::Framework::Date::DateTime::Now();
|
||||||
auto date = Tesses::Framework::Date::DateTime::Now();
|
auto dateStr = date.ToString("%Y/%m/%d %I:%M %p");
|
||||||
auto dateStr = date.ToString("%Y/%m/%d %I:%M %p");
|
|
||||||
|
|
||||||
|
|
||||||
clock.SetSize(GetCharWidth(dateStr.size()),CharHeight+2, Color(0,0,0,0));
|
clock.SetSize(GetCharWidth(dateStr.size()),CharHeight+2, Color(0,0,0,0));
|
||||||
//frame->FillRectangle(Rectangle(0,0,w,CharHeight+2),Colors::Blue);
|
//frame->FillRectangle(Rectangle(0,0,w,CharHeight+2),Colors::Blue);
|
||||||
clock.DrawString(dateStr,Point(0,1),Colors::Black);
|
clock.DrawString(dateStr,Point(0,1),Colors::Black);
|
||||||
clock.Resize(&clock2,Size(clock.Width()*2,clock.Height()*2));
|
clock.Resize(&clock2,Size(clock.Width()*2,clock.Height()*2));
|
||||||
frame->DrawImage(&clock2,Point(0,0), ImageCopyEffect::InvertIfNotTransparent);
|
frame.DrawImage(&clock2,Point(0,0), ImageCopyEffect::InvertIfNotTransparent);
|
||||||
Formats::Png.Save(strm,frame.get());
|
Formats::Png.Save(strm,&frame);
|
||||||
|
|
||||||
}
|
|
||||||
dev2.Close();
|
dev2.Close();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using namespace Tesses::Framework::Streams;
|
|||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
auto devs = Device::GetDevices();
|
auto devs = Webcam::GetWebcams();
|
||||||
for(auto dev : devs)
|
for(auto dev : devs)
|
||||||
{
|
{
|
||||||
std::cout << dev.Name << std::endl;
|
std::cout << dev.Name << std::endl;
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ namespace Tesses::Framework::Graphics {
|
|||||||
uint32_t Width();
|
uint32_t Width();
|
||||||
uint32_t Height();
|
uint32_t Height();
|
||||||
void SetSize(uint32_t w, uint32_t h);
|
void SetSize(uint32_t w, uint32_t h);
|
||||||
void SetSize(uint32_t w, uint32_t h, Color c);
|
void SetSize(uint32_t w, uint32_t h, const Color& c);
|
||||||
void SetPixel(uint32_t x, uint32_t y, Color c);
|
void SetPixel(uint32_t x, uint32_t y, const Color& c);
|
||||||
Color GetPixel(uint32_t x, uint32_t y);
|
Color GetPixel(uint32_t x, uint32_t y);
|
||||||
|
|
||||||
std::vector<Color>& Data();
|
std::vector<Color>& Data();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
namespace Tesses::Framework::Graphics
|
namespace Tesses::Framework::Graphics
|
||||||
{
|
{
|
||||||
|
|
||||||
class DeviceInfo {
|
class WebcamInfo {
|
||||||
public:
|
public:
|
||||||
uint8_t Device;
|
uint8_t Device;
|
||||||
std::string Name;
|
std::string Name;
|
||||||
@@ -12,22 +12,22 @@ namespace Tesses::Framework::Graphics
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Device {
|
class Webcam {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<HiddenFieldData> field;
|
std::shared_ptr<HiddenFieldData> field;
|
||||||
public:
|
public:
|
||||||
Device(uint8_t device, const Size& sz, uint8_t fps=10);
|
Webcam(uint8_t device, const Size& sz, uint8_t fps=10);
|
||||||
void Open();
|
void Open();
|
||||||
|
|
||||||
std::shared_ptr<Image> ReadFrame();
|
void ReadFrame(Image* img);
|
||||||
|
|
||||||
void Close();
|
void Close();
|
||||||
~Device();
|
~Webcam();
|
||||||
|
|
||||||
static bool IsEnabled();
|
static bool IsEnabled();
|
||||||
|
|
||||||
static std::vector<DeviceInfo> GetDevices();
|
static std::vector<WebcamInfo> GetWebcams();
|
||||||
static void GetDevices(std::vector<DeviceInfo>& devices);
|
static void GetWebcams(std::vector<WebcamInfo>& devices);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ namespace Tesses::Framework::Graphics {
|
|||||||
this->h = h;
|
this->h = h;
|
||||||
this->data.resize(w * h);
|
this->data.resize(w * h);
|
||||||
}
|
}
|
||||||
void Image::SetSize(uint32_t w, uint32_t h, Color c)
|
void Image::SetSize(uint32_t w, uint32_t h, const Color& c)
|
||||||
{
|
{
|
||||||
this->w = w;
|
this->w = w;
|
||||||
this->h = h;
|
this->h = h;
|
||||||
this->data.resize(w * h);
|
this->data.resize(w * h);
|
||||||
for(size_t i = 0; i < this->data.size(); i++) this->data[i] = c;
|
for(size_t i = 0; i < this->data.size(); i++) this->data[i] = c;
|
||||||
}
|
}
|
||||||
void Image::SetPixel(uint32_t x, uint32_t y, Color c)
|
void Image::SetPixel(uint32_t x, uint32_t y, const Color& c)
|
||||||
{
|
{
|
||||||
this->data[y*w+x] = c;
|
this->data[y*w+x] = c;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace Tesses::Framework::Graphics::ImageFormats {
|
|||||||
JpegFormat Formats::Jpeg;
|
JpegFormat Formats::Jpeg;
|
||||||
ImageFormat* Formats::FromExtension(std::string ext)
|
ImageFormat* Formats::FromExtension(std::string ext)
|
||||||
{
|
{
|
||||||
|
ext = Http::HttpUtils::ToLower(ext);
|
||||||
if(ext == ".jpg" || ext == ".jpeg") return &Jpeg;
|
if(ext == ".jpg" || ext == ".jpeg") return &Jpeg;
|
||||||
if(ext==".png") return &Png;
|
if(ext==".png") return &Png;
|
||||||
if(ext==".bmp") return &Bitmap;
|
if(ext==".bmp") return &Bitmap;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace Tesses::Framework::Graphics {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
Device::Device(uint8_t device, const Size& sz, uint8_t fps)
|
Webcam::Webcam(uint8_t device, const Size& sz, uint8_t fps)
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
webcam::video_settings set2;
|
webcam::video_settings set2;
|
||||||
@@ -29,7 +29,7 @@ namespace Tesses::Framework::Graphics {
|
|||||||
this->field = std::make_shared<WebcamDevice>(device, set2);
|
this->field = std::make_shared<WebcamDevice>(device, set2);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void Device::Open()
|
void Webcam::Open()
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
||||||
@@ -37,22 +37,20 @@ namespace Tesses::Framework::Graphics {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Image> Device::ReadFrame()
|
void Webcam::ReadFrame(Image* img)
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
||||||
if(dev != nullptr)
|
if(dev != nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::shared_ptr<Image> image=nullptr;
|
|
||||||
auto frame = dev->dev.read();
|
auto frame = dev->dev.read();
|
||||||
if(frame != nullptr)
|
if(frame != nullptr)
|
||||||
{
|
{
|
||||||
image = std::make_shared<Image>();
|
auto strm = std::make_shared<Tesses::Framework::Streams::MemoryStream>(true);
|
||||||
auto strm = std::make_shared<Tesses::Framework::Streams::MemoryStream>(true);
|
strm->WriteBlock((const uint8_t*)frame->get_data(),(size_t)frame->get_data_lenght());
|
||||||
strm->WriteBlock((const uint8_t*)frame->get_data(),(size_t)frame->get_data_lenght());
|
strm->Seek(0,Tesses::Framework::Streams::SeekOrigin::Begin);
|
||||||
strm->Seek(0,Tesses::Framework::Streams::SeekOrigin::Begin);
|
ImageFormats::Formats::Jpeg.Load(strm,image);
|
||||||
ImageFormats::Formats::Jpeg.Load(strm,image.get());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
delete frame;
|
delete frame;
|
||||||
@@ -64,19 +62,19 @@ namespace Tesses::Framework::Graphics {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Device::Close()
|
void Webcam::Close()
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
auto dev = std::dynamic_pointer_cast<WebcamDevice>(this->field);
|
||||||
if(dev != nullptr) dev->dev.close();
|
if(dev != nullptr) dev->dev.close();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
Device::~Device()
|
Webcam::~Webcam()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::IsEnabled()
|
bool Webcam::IsEnabled()
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
return true;
|
return true;
|
||||||
@@ -84,13 +82,13 @@ namespace Tesses::Framework::Graphics {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<DeviceInfo> Device::GetDevices()
|
std::vector<WebcamInfo> Webcam::GetWebcams()
|
||||||
{
|
{
|
||||||
std::vector<DeviceInfo> info;
|
std::vector<WebcamInfo> info;
|
||||||
GetDevices(info);
|
GetWebcams(info);
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
void Device::GetDevices(std::vector<DeviceInfo>& devices)
|
void Webcam::GetWebcams(std::vector<WebcamInfo>& webcams)
|
||||||
{
|
{
|
||||||
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
#if defined(TESSESFRAMEWORKGFX_ENABLE_WEBCAM)
|
||||||
const webcam::device_info_enumeration & enumeration = webcam::enumerator::enumerate();
|
const webcam::device_info_enumeration & enumeration = webcam::enumerator::enumerate();
|
||||||
@@ -101,7 +99,7 @@ namespace Tesses::Framework::Graphics {
|
|||||||
const webcam::device_info & device_info = enumeration.get(device_index);
|
const webcam::device_info & device_info = enumeration.get(device_index);
|
||||||
|
|
||||||
const webcam::model_info & model_info = device_info.get_model_info();
|
const webcam::model_info & model_info = device_info.get_model_info();
|
||||||
DeviceInfo info;
|
WebcamInfo info;
|
||||||
info.Device = (uint8_t)(device_index+1);
|
info.Device = (uint8_t)(device_index+1);
|
||||||
info.Name = model_info.get_name();
|
info.Name = model_info.get_name();
|
||||||
|
|
||||||
@@ -118,7 +116,7 @@ namespace Tesses::Framework::Graphics {
|
|||||||
info.Resolutions.push_back(Size(sz.get_width(),sz.get_height()));
|
info.Resolutions.push_back(Size(sz.get_width(),sz.get_height()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
devices.push_back(info);
|
webcams.push_back(info);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user