#include <stdio.h> #include <iostream> #include <chrono> #include <thread> #include <DjiRtspImageSource.h> #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" static inline int64_t now() { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); } static int write_data_to_file(const char* name, uint8_t* data, int size) { FILE* fd = fopen(name, "wb"); if(fd) { int w = (int)fwrite(data, 1, size, fd); fclose(fd); return w; } else { return -1; } } char rtsp_url = "rtsp://192.168.42.142:8554/live"; int main(int argc, char** argv) { if(argc < 1) return -1; if(argc == 1) { std::cout << "Usage : " << argv[0] << " <url>" << std::endl; return -1; } int64_t ts = now(); DjiRtspImageSource service(rtsp_url); service.setImageCallback(nullptr, [&ts](void* handler, uint8_t* frmdata, int frmsize, int width, int height, int pixfmt) -> void { printf("Image %d@%p -- %dx%d -- %d\n", frmsize, frmdata, width, height, pixfmt); if(frmdata) { int64_t t = now(); if(t - ts > 1000) { ts = t; char name[64]; static int counter = 0; sprintf(name, "pictures/%dx%d-%d_%d.jpg", width, height, pixfmt, ++counter); if(pixfmt == 5) stbi_write_jpg(name, width, height, 3, frmdata, 80); } } }); service.start(); for(;;) //for(int i=0; i<30; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } service.stop(); std::cout << "done." << std::endl; return 0; } 利用上述代码实现提取并解码二维码的信息,并将解码结果保存到tta文件夹下保存为文件名为 list_of_goods,给出c++源码
时间: 2024-04-16 21:29:12 浏览: 180
#include <stdio.h>
4星 · 用户满意度95%
以下是使用 C++ 代码从图像中提取和解码二维码信息,并将解码结果保存到名为 "list_of_goods.txt" 的文件中:
```cpp
#include <stdio.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <DjiRtspImageSource.h>
#include <zbar.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
using namespace std;
using namespace zbar;
static inline int64_t now()
{
return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
}
static int write_data_to_file(const char* name, uint8_t* data, int size)
{
FILE* fd = fopen(name, "wb");
if(fd)
{
int w = (int)fwrite(data, 1, size, fd);
fclose(fd);
return w;
}
else
{
return -1;
}
}
void save_qr_code_data(const char* data)
{
FILE* file = fopen("tta/list_of_goods.txt", "a");
if (file)
{
fprintf(file, "%s\n", data);
fclose(file);
}
}
char rtsp_url[] = "rtsp://192.168.42.142:8554/live";
int main(int argc, char** argv)
{
if(argc < 1) return -1;
if(argc == 1)
{
cout << "Usage : " << argv[0] << " <url>" << endl;
return -1;
}
int64_t ts = now();
DjiRtspImageSource service(rtsp_url);
service.setImageCallback(nullptr, [&ts](void* handler, uint8_t* frmdata, int frmsize, int width, int height, int pixfmt) -> void {
printf("Image %d@%p -- %dx%d -- %d\n", frmsize, frmdata, width, height, pixfmt);
if(frmdata)
{
int64_t t = now();
if(t - ts > 1000)
{
ts = t;
char name[64];
static int counter = 0;
sprintf(name, "pictures/%dx%d-%d_%d.jpg", width, height, pixfmt, ++counter);
if(pixfmt == 5)
{
stbi_write_jpg(name, width, height, 3, frmdata, 80);
// Decode QR code
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Image image(width, height, "Y800", frmdata, width * height);
int n = scanner.scan(image);
for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
{
cout << "QR code data: " << symbol->get_data() << endl;
save_qr_code_data(symbol->get_data().c_str());
}
}
}
}
});
service.start();
for(;;)
//for(int i=0; i<30; i++)
{
this_thread::sleep_for(chrono::milliseconds(1000));
}
service.stop();
cout << "done." << endl;
return 0;
}
```
注意,上述代码使用了 `zbar` 库来进行二维码解码。确保在编译和运行之前已经安装了该库,并根据需要修改保存文件的路径。同时,需要确保 "tta" 文件夹已经存在,以便保存解码结果。
阅读全文