|
@@ -0,0 +1,286 @@
|
|
|
+#include <QCoreApplication>
|
|
|
+#include <thread>
|
|
|
+#include <QMutex>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#include "usb_cam.h"
|
|
|
+
|
|
|
+
|
|
|
+#include <iostream>
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <unistd.h>
|
|
|
+
|
|
|
+#include <sys/types.h> // 下面四个头文件是linux系统编程特有的
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <sys/ioctl.h>
|
|
|
+#include <sys/mman.h>
|
|
|
+#include <fcntl.h>
|
|
|
+
|
|
|
+#include <linux/videodev2.h> // 操作摄像头设备
|
|
|
+
|
|
|
+
|
|
|
+#include <signal.h>
|
|
|
+
|
|
|
+QCoreApplication * gApp;
|
|
|
+
|
|
|
+
|
|
|
+void * gpa;
|
|
|
+
|
|
|
+int gindex = 0;
|
|
|
+
|
|
|
+
|
|
|
+int gcamindex = 1;
|
|
|
+
|
|
|
+std::string gstrcamera = "";
|
|
|
+
|
|
|
+std::string gmsgname = "usbpic";
|
|
|
+
|
|
|
+std::string gstrdevname = "/dev/video5";
|
|
|
+
|
|
|
+
|
|
|
+std::string gvideo_device_name_, gio_method_name_, gpixel_format_name_, gcamera_name_, gcamera_info_url_;
|
|
|
+//std::string start_service_name_, start_service_name_;
|
|
|
+bool gstreaming_status_;
|
|
|
+int gimage_width_, gimage_height_, gframerate_, gexposure_, gbrightness_, gcontrast_, gsaturation_, gsharpness_, gfocus_,
|
|
|
+ gwhite_balance_, ggain_;
|
|
|
+bool gautofocus_, gautoexposure_, gauto_white_balance_;
|
|
|
+
|
|
|
+bool gbuserawmjpeg = true;
|
|
|
+
|
|
|
+bool gbcompress = true;
|
|
|
+
|
|
|
+
|
|
|
+void setdefaultcvalue()
|
|
|
+{
|
|
|
+ gbrightness_ = -1;// xp.GetParam("brightness", -1); //0-255, -1 "leave alone"
|
|
|
+ gcontrast_ = -1;//xp.GetParam("contrast", -1); //0-255, -1 "leave alone"
|
|
|
+ gsaturation_ = -1;//xp.GetParam("saturation", -1); //0-255, -1 "leave alone"
|
|
|
+ gsharpness_ = -1;//xp.GetParam("sharpness", -1); //0-255, -1 "leave alone"
|
|
|
+ // possible values: mmap, read, userptr
|
|
|
+// gio_method_name_ = xp.GetParam("io_method", std::string("mmap"));
|
|
|
+// gimage_width_ = xp.GetParam("image_width", 1920);
|
|
|
+// gimage_height_ = xp.GetParam("image_height", 1080);
|
|
|
+// gframerate_ = xp.GetParam("framerate", 30);
|
|
|
+ // possible values: yuyv, uyvy, mjpeg, yuvmono10, rgb24
|
|
|
+// gpixel_format_name_ = xp.GetParam("pixel_format", std::string("mjpeg"));
|
|
|
+ // enable/disable autofocus
|
|
|
+ gautofocus_ = false;//xp.GetParam("autofocus", false);
|
|
|
+
|
|
|
+
|
|
|
+ gfocus_ = -1;//xp.GetParam("focus", -1); //0-255, -1 "leave alone"
|
|
|
+ // enable/disable autoexposure
|
|
|
+ gautoexposure_ = true;//xp.GetParam("autoexposure", true);
|
|
|
+ gexposure_ = 100;//xp.GetParam("exposure", 100);
|
|
|
+ ggain_ = -1;//xp.GetParam("gain", -1); //0-100?, -1 "leave alone"
|
|
|
+ // enable/disable auto white balance temperature
|
|
|
+ gauto_white_balance_ = true;//xp.GetParam("auto_white_balance", true);
|
|
|
+ gwhite_balance_ = 4000;//xp.GetParam("white_balance", 4000);
|
|
|
+
|
|
|
+ // load the camera info
|
|
|
+// xp.GetParam("camera_frame_id", img_.header.frame_id, std::string("head_camera"));
|
|
|
+// gcamera_name_ = "head_camera";// xp.GetParam("camera_name", std::string("head_camera"));
|
|
|
+// gcamera_info_url_ = "";//xp.GetParam("camera_info_url", std::string(""));
|
|
|
+}
|
|
|
+
|
|
|
+using namespace usb_cam;
|
|
|
+void threadcapture()
|
|
|
+{
|
|
|
+
|
|
|
+ usb_cam::UsbCam camx;
|
|
|
+
|
|
|
+ // set the IO method
|
|
|
+ UsbCam::io_method io_method = UsbCam::io_method_from_string("mmap");
|
|
|
+ if(io_method == UsbCam::IO_METHOD_UNKNOWN)
|
|
|
+ {
|
|
|
+ qDebug("Unknown IO method '%s'", gio_method_name_.c_str());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // set the pixel format
|
|
|
+ UsbCam::pixel_format pixel_format = UsbCam::pixel_format_from_string("mjpeg");
|
|
|
+ if (pixel_format == UsbCam::PIXEL_FORMAT_UNKNOWN)
|
|
|
+ {
|
|
|
+ qDebug("Unknown pixel format '%s'", gpixel_format_name_.c_str());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // start the camera
|
|
|
+ camx.start(gvideo_device_name_.c_str(), io_method, pixel_format, gimage_width_,
|
|
|
+ gimage_height_, gframerate_);
|
|
|
+
|
|
|
+ // set camera parameters
|
|
|
+ if (gbrightness_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("brightness", gbrightness_);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (gcontrast_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("contrast", gcontrast_);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (gsaturation_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("saturation", gsaturation_);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (gsharpness_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("sharpness", gsharpness_);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ggain_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("gain", ggain_);
|
|
|
+ }
|
|
|
+
|
|
|
+ // check auto white balance
|
|
|
+ if (gauto_white_balance_)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("white_balance_temperature_auto", 1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("white_balance_temperature_auto", 0);
|
|
|
+ camx.set_v4l_parameter("white_balance_temperature", gwhite_balance_);
|
|
|
+ }
|
|
|
+
|
|
|
+ // check auto exposure
|
|
|
+ if (!gautoexposure_)
|
|
|
+ {
|
|
|
+ // turn down exposure control (from max of 3)
|
|
|
+ camx.set_v4l_parameter("exposure_auto", 1);
|
|
|
+ // change the exposure level
|
|
|
+ camx.set_v4l_parameter("exposure_absolute", gexposure_);
|
|
|
+ }
|
|
|
+
|
|
|
+ // check auto focus
|
|
|
+ if (gautofocus_)
|
|
|
+ {
|
|
|
+ camx.set_auto_focus(1);
|
|
|
+ camx.set_v4l_parameter("focus_auto", 1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("focus_auto", 0);
|
|
|
+ if (gfocus_ >= 0)
|
|
|
+ {
|
|
|
+ camx.set_v4l_parameter("focus_absolute", gfocus_);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ camx.set_useRawMJPEG(gbuserawmjpeg);
|
|
|
+
|
|
|
+ int nserbufsize = 20000000;
|
|
|
+
|
|
|
+ char * strser = new char[nserbufsize];
|
|
|
+
|
|
|
+ char * strbuf = new char[10000000];
|
|
|
+
|
|
|
+
|
|
|
+ int64_t nLastrecv = 0;
|
|
|
+ int nvalid = 0;
|
|
|
+ int nfail = 0;
|
|
|
+ while(1)
|
|
|
+ {
|
|
|
+ int nLen;
|
|
|
+ camx.grab_image(strbuf,&nLen,10000000);
|
|
|
+ int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
|
|
|
+ if(abs(nnow - nLastrecv)<50)
|
|
|
+ {
|
|
|
+ nvalid++;
|
|
|
+ nfail = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ nvalid = 0;
|
|
|
+ nfail++;
|
|
|
+ }
|
|
|
+ if(nvalid > 10)
|
|
|
+ {
|
|
|
+ std::cout<<" camtest success."<<std::endl;
|
|
|
+ exit(0);
|
|
|
+ }
|
|
|
+ if(nfail > 5)
|
|
|
+ {
|
|
|
+ std::cout<<" cantest fail."<<std::endl;
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ nLastrecv = nnow;
|
|
|
+// qDebug("time %ld len: %ld",QDateTime::currentMSecsSinceEpoch(),nLen);
|
|
|
+
|
|
|
+
|
|
|
+// iv::modulecomm::ModuleSendMsg(gpa,strbuf,nLen);
|
|
|
+// continue;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ExitFunc()
|
|
|
+{
|
|
|
+ gApp->quit();
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
+}
|
|
|
+
|
|
|
+void signal_handler(int sig)
|
|
|
+{
|
|
|
+ if(sig == SIGINT)
|
|
|
+ {
|
|
|
+ ExitFunc();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argc, char *argv[])
|
|
|
+{
|
|
|
+ QCoreApplication a(argc, argv);
|
|
|
+ gApp = &a;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ gimage_width_ = 1920;
|
|
|
+ gimage_height_ = 1080;
|
|
|
+ gframerate_ = 30;
|
|
|
+ gvideo_device_name_ = "/dev/video0";
|
|
|
+
|
|
|
+ if(argc>1)
|
|
|
+ {
|
|
|
+ gvideo_device_name_ = argv[1];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(argc>2)
|
|
|
+ {
|
|
|
+ gimage_width_ = atoi(argv[2]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(argc>3)
|
|
|
+ {
|
|
|
+ gimage_height_ = atoi(argv[3]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(argc>4)
|
|
|
+ {
|
|
|
+ gframerate_ = atoi(argv[4]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ std::thread * mthread = new std::thread(threadcapture) ;//new std::thread(VideoThread,0);
|
|
|
+
|
|
|
+// std::thread * conthread = new std::thread(threadConvert);
|
|
|
+
|
|
|
+// std::thread * compressthread = new std::thread(threadCompress);
|
|
|
+
|
|
|
+ (void)mthread;
|
|
|
+// (void)conthread;
|
|
|
+// (void)compressthread;
|
|
|
+
|
|
|
+ signal(SIGINT,signal_handler);
|
|
|
+
|
|
|
+ int nrc = a.exec();
|
|
|
+
|
|
|
+
|
|
|
+ return nrc;
|
|
|
+}
|