Capture the output of a QR scanner from a raspberry in C++ - c++

I have a QR scanner attached to my raspberry pi 3 B+. I need to capture the output of the scanner from C++ on raspbian. The scanner writes the read information to the window with the focus, like a keyboard input.
I have tried to capture the keyboard input with the ncurses library and other similar choices but sometimes I get an incomplete code, because I don't know when the read info ends, and I don't know when I have a complete input.
I tried something like this:
#include <stdio.h>
#include <iostream>
#include <ncurses.h>
#include <future>
using namespace std;
int main(int argc, char *argv[])
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
char buffer[1000];
while (true) {
getstr(buffer);
cout<< buffer << endl;
napms(500);
}
return 0;
}
The problem is that I may get the code in a fragmented way if it's too long or if the output slows down for whatever reason.

Related

Why is color not working in the Ncurses library in C++?

I'm trying to write an ncurses program in c++ that uses color, except every time I compile it acts like my terminal does not support colors, rejecting me when I include a color check, and not printing in color when not checking colors. My terminal does support colors (the terminal itself is terminator term emulator w/ zsh, have tried on xterm and tty). Is there some option I need to add to g++ to tell it to compile with color support? I don't get any errors during compilation or runtime.
Here's my code if it helps:
#include <stdio.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
if(!has_colors()) {
printf("Terminal does not support colors \n");
return 0;
}
initscr();
start_color();
init_pair(1, COLOR_BLACK, COLOR_RED);
/* Get terminal dimensions */
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
/* Make X and Y variables global for later */
extern int x;
extern int y;
/* Print test text */
attron(COLOR_PAIR(1));
printw(" This text should be black on red ");
attroff(COLOR_PAIR(1));
refresh();
getch();
return 0;
}
The compiled program just spits out "terminal does not support colors" even though it does. Excluding the check still doesn't work, just spits it out in monochrome.
And before you ask, yes I googled it, and no it didn't help.

How to disable input method in C++ console?

I'm making a text-based console game.
The Chinese input method is annoying as it blocks normal key input.
I tried to send NULL to ImmAssociateContext but it doesn't work.
#include<Windows.h>
#include <imm.h>
#include <atlstr.h>
#include<handleapi.h>
#include<iostream>
#pragma comment ( lib,"imm32.lib" )
using namespace std;
int main(int argc, char* argv[])
{
SetConsoleTitle(TEXT("test"));
char ch;
ImmAssociateContext(FindWindow(NULL, TEXT("test")), NULL);
cin >> ch;
cout << ch;
return 0;
}
Edit:
I don't need to input Chinese in this game.
About Chinese input:
When you input a letter in console, Chinese input methods would receive the input as pinyin, and turn it into Chinese characters.
(In the screenshot, aabbcc with dashline below is pinyin)
The same thing happened to Japanese input method.
This is not what I need. All I want is, when I press A, the console receives A.
About the code:
I'm using PDCurses in my project to draw a text-based gui and get key inputs.
Everything looks fine when the input method is turned off.
The code above shows ImmAssociateContext (google says it can turn off input method) doesn't work to me.

Ncurses and C++ string concatenation not working properly

I'm trying to display a statusbar using ncurses with "---" between items, like most terminal programs do.
I have the following code so far:
#include <iostream>
#include <cmath>
#include <string>
#include <curses.h>
using namespace std;
int main() {
initscr();
start_color();
init_pair(3, COLOR_WHITE, COLOR_RED);
getch();
attron(COLOR_PAIR(3));
string a = "--- Tickets: 455";
a.append(70, '-');
mvprintw(LINES-1, 0, a.c_str());
getch();
endwin();
}
The mvprintw should output the whole width of the screen until the right border. But in fact, only one "-" is appended.
I'm not sure if this is a an ncurses problem or if strings are not converted to c style strings properly. Could someone help me identify the issue?

SDL not printing to console

I'm trying to use SDL with Visual Studio 2019 but my programs are only showing an empty console. At the moment I just want to be able to compile my program with the SDL libraries.
#include <iostream>
#include <SDL.h>
int main(int argc, char** argv)
{
std::cout << "yee haw!" << std::endl;
return 0;
}
This code is just giving me a console with the text:
(process 32) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Where I would want 'yee haw!' preceding it.
It works fine when I take out the #include <SDL.h> (but I want the SDL.h)
I've heard that SDL now redirects to a stdout.txt file but I couldn't find that anywhere. I've also tried displaying a window with code from a tutorial I found, but that also gives me the empty console.
I'm using Visual Studio 2019 on Windows and SDL 2.0.9
Thanks!
By default, SDL uses a macro hack to replace the main function. The user defined main function must be in the following format:
int main(int argc, char** argv)
{
// whatever
return 0;
}
Alternatively, if you don't want this behaviour you can use SDL_SetMainReady.
#define SDL_MAIN_HANDLED
#include <SDL.h>
int main()
{
SDL_SetMainReady();
// whatever
return 0;
}

OpenCV: Reading the frames of a video sequence

Anyone help me ,I am trying to run code to read frames from video in folder its success in building but when debugging there isn't any output
* I am using Visual studio 2012 ,opencv 2.4.11 version
the code is :
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
// Open the video file
cv::VideoCapture capture("C:/Users/asus/Desktop/A.mp4");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
int rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
Your main() function is never executed. The only thing, that gets executed is _tmain(), which does nothing and returns immediately.
I haven't done much Windows programming in a while, but if I remember correctly this is how it works:
When Unicode is enabled for your compiler
int _tmain(int argc, _TCHAR* argv[])
gets compiled as
int wmain(int argc, wchar * argv[])
which is then used as the program entry point.
Since you seem not to be using any Windows-APIs in your code I would ignore the Microsoft specific way of doing multibyte character strings, which is non-portable, and simply use plain ASCII strings as you did in the main() function, that you intended to use.
So to solve your problem simply throw out the _tmain() function. Maybe you also need to disable Unicode in your project settings if you get linker errors.