简体   繁体   English

如何使用CImg库显示临时图像

[英]How can I show a temporary image with CImg library

I was wondering if someone can help me with this... I need to display my image for 15 seconds and i also have to use the draw_text function to display on the image some details with int value, and it asks for const char*const text value. 我想知道是否有人可以帮助我...我需要显示我的图像15秒钟,而且我还必须使用draw_text函数在图像上显示一些带有int值的细节,并且它要求const char * const文字值。 I am not supposed to add libraries, just the iostream and of course CImg. 我不应该添加库,而只能添加iostream和CImg。

this is the current code i've been trying to use. 这是我一直在尝试使用的当前代码。

CImg<unsigned char> image(filename);
 unsigned char red[] = { 255,0,0 };
 unsigned char blue[] = { 0,0,255 };
 image.draw_text(5,5,"Here i want to call the int value",red,blue,1,10);
 if (opcion == 0x33)
 {
     //Here i want to display the following line for 15 seconds
     image.display("hello world");
 }

I am not sure whether your question is about getting an integer into a string, or about displaying an image for 15 seconds. 我不确定您的问题是关于将整数放入字符串中还是要显示图像15秒钟。 However, the following code should show you a way of doing both. 但是,以下代码应显示两种方法。

The main thing is that the main loop waits up to 100ms for events, and if you do that 150 times, your screen will display for around 15 seconds. 最主要的是,主循环最多可等待100ms的事件,如果您执行150次,则屏幕将显示约15秒。

#include <iostream>
#include "CImg.h"
using namespace std;
using namespace cimg_library;

#define BUFSIZE 128

int main(int argc, char** const argv)
{
   // Create empty yellow image
   CImg<unsigned char> image(256,256,1,3);
   cimg_forXY(image,x,y) { image(x,y,0,0)=255; image(x,y,0,1)=255; image(x,y,0,2)=0; }

   unsigned char red[]  = { 255,0,0 };
   unsigned char blue[] = { 0,0,255 };
   char text[BUFSIZE];

   int someInt=42;
   snprintf(text,BUFSIZE,"Hello, the integer is %d",someInt);

   image.draw_text(5,5,text,red,blue,1,10);
   CImgDisplay main_disp(image,"Hello world");

   // Loop 150 times @100ms each
   for(int i=0;i<150;i++){
      if(main_disp.is_closed()){break;}
      main_disp.wait(100);
   }
}

There are other, more C++, ways of getting an integer into a string, but this method will get you started. 还有其他一些将整数转换为字符串的C ++方法,但是这种方法可以帮助您入门。

The method of running 15 seconds is also quite crude, but I suspect it is only a nominal 15 seconds you want. 运行15秒的方法也很粗略,但我怀疑这只是您想要的15秒。 You could get the time at the start of the program, using time() and then get the time again on each pass through the loop and break if the difference exceeds 15 - but that is a little less clear than the simple method above. 您可以使用time()在程序开始time()获取时间,然后在每次循环遍历时再次获取时间,如果相差超过15,则中断,但比上面的简单方法还不清楚。 That code could look like this: 该代码如下所示:

#include <iostream>
#include <ctime>
#include "CImg.h"
using namespace std;
using namespace cimg_library;

#define BUFSIZE 128

int main(int argc, char** const argv)
{
   // Create empty yellow image
   CImg<unsigned char> image(256,256,1,3);
   cimg_forXY(image,x,y) { image(x,y,0,0)=255; image(x,y,0,1)=255; image(x,y,0,2)=0; }

   unsigned char red[]  = { 255,0,0 };
   unsigned char blue[] = { 0,0,255 };
   char text[BUFSIZE];

   int someInt=42;
   snprintf(text,BUFSIZE,"Hello, the integer is %d",someInt);

   image.draw_text(5,5,text,red,blue,1,10);
   CImgDisplay main_disp(image,"Hello world");

   time_t start,diff;
   start=time(NULL);

   // Loop till display closed or 15 seconds elapsed
   while(1){
      // Exit if closed
      if(main_disp.is_closed()){break;}

      // Exit if 15 seconds are up
      diff=time(NULL)-start;
      if(diff>15){break;}

      main_disp.wait(10);
   }
}

You can do the string stuff in a more C++ way like this: 您可以使用以下更C ++的方式来处理字符串:

#include <iostream>
#include <sstream>
#include <ctime>
#include <string>
#include "CImg.h"
using namespace std;
using namespace cimg_library;

int main(int argc, char** const argv)
{
   // Create empty yellow image
   CImg<unsigned char> image(256,256,1,3);
   cimg_forXY(image,x,y) { image(x,y,0,0)=255; image(x,y,0,1)=255; image(x,y,0,2)=0; }

   unsigned char red[]  = { 255,0,0 };
   unsigned char blue[] = { 0,0,255 };

   int someInt=42;
   string text="The integer is: ";
   text += to_string(someInt);

   image.draw_text(5,5,text.c_str(),red,blue,1,10);
   CImgDisplay main_disp(image,"Hello world");

   time_t start,diff;
   start=time(NULL);

   // Loop till display closed or 15 seconds elapsed
   while(1){
      // Exit if closed
      if(main_disp.is_closed()){break;}

      // Exit if 15 seconds are up
      diff=time(NULL)-start;
      if(diff>15){break;}

      main_disp.wait(10);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM