onilink_

Untitled

Oct 21st, 2020
628
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <onidev.h>
  2. #include <onidev/image/experimental/image.h>
  3. #include "toupcam.h"
  4.  
  5. #include <iostream>
  6. #include <thread>
  7. #include <chrono>
  8.  
  9. struct CameraCtx
  10. {
  11.     HToupcam camera;
  12.     int resolution = 0;
  13.     int bits = 32; // 8 or 32
  14.     int wid = 0, hei = 0;
  15.     unsigned n_wid = 0, n_hei = 0;
  16.    
  17.     std::vector<unsigned char> im_data;
  18. };
  19.  
  20. void callback(unsigned nEvent, void* pCallbackCtx)
  21. {
  22.     CameraCtx* ctx = reinterpret_cast<CameraCtx*>(pCallbackCtx);
  23.    
  24.     if(nEvent == TOUPCAM_EVENT_IMAGE)
  25.     {
  26.         ctx->n_wid = ctx->wid;
  27.         ctx->n_hei = ctx->hei;
  28.         ctx->im_data.resize(ctx->wid * ctx->hei * 4);
  29.        
  30.         if(Toupcam_PullImage(ctx->camera, &ctx->im_data[0], ctx->bits, &ctx->n_wid, &ctx->n_hei) != 0) {
  31.             std::cerr << "Toupcam_PullImage failed" << std::endl;
  32.         }
  33.     }
  34.     else if(nEvent == TOUPCAM_EVENT_STILLIMAGE)
  35.     {
  36.         ctx->n_wid = ctx->wid;
  37.         ctx->n_hei = ctx->hei;
  38.         ctx->im_data.resize(ctx->wid * ctx->hei * 4);
  39.        
  40.         if(Toupcam_PullStillImage(ctx->camera, &ctx->im_data[0], ctx->bits, &ctx->n_wid, &ctx->n_hei) != 0) {
  41.             std::cerr << "Toupcam_PullStillImage failed" << std::endl;
  42.         }
  43.     }
  44. }
  45.  
  46. int main()
  47. {
  48.     CameraCtx ctx;
  49.     ctx.camera = Toupcam_Open(nullptr);
  50.    
  51.     if(!ctx.camera) {
  52.         return 1;
  53.     }
  54.    
  55.     Toupcam_put_eSize(ctx.camera, ctx.resolution);
  56.     if(Toupcam_get_Size(ctx.camera, &ctx.wid, &ctx.hei) != 0) {
  57.         return 1;
  58.     }
  59.    
  60.     Toupcam_StartPullModeWithCallback(ctx.camera, callback, &ctx);
  61.    
  62.     std::this_thread::sleep_for(std::chrono::seconds(1));
  63.    
  64.     od::experimental::Image im({ctx.n_wid, ctx.n_hei}, &ctx.im_data[0]);
  65.     for(od::Color& c: im) {
  66.         c.a = 255;
  67.     }
  68.     im.save("../out.png");
  69.    
  70.     return 0;
  71. }
  72.  
  73.  
RAW Paste Data