Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <onidev.h>
- #include <onidev/image/experimental/image.h>
- #include "toupcam.h"
- #include <iostream>
- #include <thread>
- #include <chrono>
- struct CameraCtx
- {
- HToupcam camera;
- int resolution = 0;
- int bits = 32; // 8 or 32
- int wid = 0, hei = 0;
- unsigned n_wid = 0, n_hei = 0;
- std::vector<unsigned char> im_data;
- };
- void callback(unsigned nEvent, void* pCallbackCtx)
- {
- CameraCtx* ctx = reinterpret_cast<CameraCtx*>(pCallbackCtx);
- if(nEvent == TOUPCAM_EVENT_IMAGE)
- {
- ctx->n_wid = ctx->wid;
- ctx->n_hei = ctx->hei;
- ctx->im_data.resize(ctx->wid * ctx->hei * 4);
- if(Toupcam_PullImage(ctx->camera, &ctx->im_data[0], ctx->bits, &ctx->n_wid, &ctx->n_hei) != 0) {
- std::cerr << "Toupcam_PullImage failed" << std::endl;
- }
- }
- else if(nEvent == TOUPCAM_EVENT_STILLIMAGE)
- {
- ctx->n_wid = ctx->wid;
- ctx->n_hei = ctx->hei;
- ctx->im_data.resize(ctx->wid * ctx->hei * 4);
- if(Toupcam_PullStillImage(ctx->camera, &ctx->im_data[0], ctx->bits, &ctx->n_wid, &ctx->n_hei) != 0) {
- std::cerr << "Toupcam_PullStillImage failed" << std::endl;
- }
- }
- }
- int main()
- {
- CameraCtx ctx;
- ctx.camera = Toupcam_Open(nullptr);
- if(!ctx.camera) {
- return 1;
- }
- Toupcam_put_eSize(ctx.camera, ctx.resolution);
- if(Toupcam_get_Size(ctx.camera, &ctx.wid, &ctx.hei) != 0) {
- return 1;
- }
- Toupcam_StartPullModeWithCallback(ctx.camera, callback, &ctx);
- std::this_thread::sleep_for(std::chrono::seconds(1));
- od::experimental::Image im({ctx.n_wid, ctx.n_hei}, &ctx.im_data[0]);
- for(od::Color& c: im) {
- c.a = 255;
- }
- im.save("../out.png");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement