Browse Source

add picview_civetweb.

yuchuli 3 years ago
parent
commit
ce689db297

+ 73 - 0
src/tool/picview_civetweb/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 73 - 0
src/tool/picview_civetweb/frontend/indexpic.html

@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>ADCIV WEB UI. Designed Base CivetWeb.</title>
+<script>
+
+//var i_width=window.document.body.clientWidth//网页可用宽度  
+//var i_height=window.document.body.clientHeight;
+//var pic = document.getElementById("myCanvas");
+//pic.style.height=i_height+"px";
+//pic.style.width=i_width+"px";
+var index=0
+var xc=0
+//var int=self.setInterval('clock()',100);
+var int2=self.setInterval('changeImage()',29);
+
+	var n = 0;
+	var image=new Image();//放入函数里面:浏览器运行一分钟左右系统内存用到高达90%,故做一个全局的反冲图片
+	function changeImage()
+	{ 
+		var canvas=document.getElementById("myCanvas2");  
+		var cxt=canvas.getContext("2d");
+		//ctx.restore();
+		image.src="./pic?"+n;
+		n++; 
+		var nerror = 0;
+		image.onload = function () //确保图片已经加载完毕  
+		{  
+			if (image.complete)//如果图片加载完成,绘制
+			{
+				cxt.drawImage(image,0,0);  
+				nerror = 0;
+			}
+			else
+			{
+				nerror++;
+				if(nerror>100)
+				{
+					alert(image.complete);
+					nerror = 0;
+				}
+			}
+			
+		}  
+		image.onerror=function(){  
+			document.getElementById("F2").innerHTML="error N:"+n; 
+		};  
+		//ctx.save();
+  	}
+
+function clock() 
+{ 
+	changeImage();
+} 
+function load() {
+	changeImage();
+}
+</script>
+</head>
+<body onload="changeImage()">
+<!-- <div id='websock_text_field'>No websocket connection yet</div>-->
+<!--  <iframe id='image' src="" width="800" height="600"></iframe>  -->
+
+
+
+<canvas id="myCanvas2" width="1920" height="1080" style="border:0px solid #c3c3c3;">  
+Your browser does not support the canvas element.  
+</canvas>
+ 
+
+</body>
+</html>

+ 176 - 0
src/tool/picview_civetweb/main.cpp

@@ -0,0 +1,176 @@
+#include <QCoreApplication>
+
+#include <QMutex>
+#include <iostream>
+#include <QFile>
+
+#include "xmlparam.h"
+#include "modulecomm.h"
+
+#include "rawpic.pb.h"
+
+#include "CivetServer.h"
+#include <cstring>
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+
+iv::vision::rawpic mRawPic;
+QMutex gMutex;
+qint64 gLastUpdate = 0;
+void * gpa;
+
+#define DOCUMENT_ROOT "./frontend/dist"
+#define PORT "8081"
+
+void Listenpic(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
+{
+    if(nSize<1000)return;
+    iv::vision::rawpic pic;
+    if(false == pic.ParseFromArray(strdata,nSize))
+    {
+        std::cout<<"picview Listenpic fail."<<std::endl;
+        return;
+    }
+
+    gMutex.lock();
+    mRawPic.CopyFrom(pic);
+    gMutex.unlock();
+    gLastUpdate = QDateTime::currentMSecsSinceEpoch();
+
+}
+
+
+class WsStartHandler : public CivetHandler
+{
+  public:
+    bool
+    handleGet(CivetServer *server, struct mg_connection *conn)
+    {
+
+    mg_printf(conn,
+              "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
+              "close\r\n\r\n");
+
+    mg_printf(conn, "<!DOCTYPE html>\n");
+    mg_printf(conn, "<html>\n<head>\n");
+    mg_printf(conn, "<meta charset=\"UTF-8\">\n");
+    mg_printf(conn, "<title>ADC IV Web UI</title>\n");
+
+    QFile xFile;
+    xFile.setFileName("./frontend/indexpic.html");
+    if(xFile.open(QIODevice::ReadOnly))
+    {
+        QByteArray ba = xFile.readAll();
+        mg_printf(conn,ba.data());
+
+    }
+
+    return 1;
+
+
+
+    }
+};
+
+class PicHandler : public CivetHandler
+{
+  public:
+    bool
+    handleGet(CivetServer *server, struct mg_connection *conn)
+    {
+
+        static int ncount;
+ //       return false;
+//        mg_printf(conn,
+//                  "HTTP/1.1 200 OK\r\nContent-Type: "
+//                  "text/html\r\nConnection: close\r\n\r\n");
+//        mg_printf(conn, "<html><body>");
+//        mg_printf(conn, "<h2>This is the Pic handler!!!</h2>");
+//        mg_printf(conn, "</body></html>\n");
+
+        mg_printf(conn,
+                  "HTTP/1.1 200 OK\r\n"
+                  "Connection: close\r\n"
+                  "Max-Age: 0\r\n"
+                  "Expires: 0\r\n"
+                  "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
+                  "Pragma: no-cache\r\n"
+                  "Content-Type: multipart/x-mixed-replace; "
+                  "boundary=--BoundaryString\r\n"
+                  "\r\n");
+
+        mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
+        mg_printf(conn,
+        "<script type=\"text/javascript\">\r\n"
+            "function myrefresh() {\r\n"
+                "window.location.reload();\r\n"
+            "}\r\n"
+            "setTimeout('myrefresh()', 1000);\r\n"
+        "</script>\r\n");
+
+        QByteArray ba;
+        if(gLastUpdate > 0)
+        {
+        iv::vision::rawpic xrawpic;
+        gMutex.lock();
+        xrawpic.CopyFrom(mRawPic);
+        gMutex.unlock();
+        ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
+
+        }
+        mg_printf(conn,
+                  "--BoundaryString\r\n"
+                  "Content-type: image/jpeg\r\n"
+                  "Content-Length: %zu\r\n"
+                  "\r\n",
+                  ba.size());
+
+
+        mg_write(conn, ba.data(), ba.size());
+
+
+        mg_printf(conn, "\r\n\r\n");
+
+
+        ncount++;
+        printf("send pic. %d\n",ncount);
+
+        return true;
+    }
+};
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+    iv::xmlparam::Xmlparam xp("./picview_civetweb.xml");
+    std::string strmsgname =  xp.GetParam("imagemsgname","picfront");
+    gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic);
+
+    mg_init_library(0);
+
+
+    const char *options[] = {
+        "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
+
+    std::vector<std::string> cpp_options;
+    for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
+        cpp_options.push_back(options[i]);
+    }
+
+    // CivetServer server(options); // <-- C style start
+    CivetServer server(cpp_options); // <-- C++ style start
+
+    WsStartHandler h_ws;
+    server.addHandler("/", h_ws);
+
+    PicHandler h_pic;
+    server.addHandler("/pic", h_pic);
+
+    return a.exec();
+}

+ 50 - 0
src/tool/picview_civetweb/picview_civetweb.pro

@@ -0,0 +1,50 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+        ../../../thirdpartylib/civetweb/CivetServer.cpp \
+        ../../../thirdpartylib/civetweb/civetweb.c \
+        ../../include/msgtype/rawpic.pb.cc \
+        main.cpp
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+INCLUDEPATH += $$PWD/../../../thirdpartylib/civetweb
+
+INCLUDEPATH += $$PWD/../../include/msgtype
+
+
+LIBS += -lprotobuf
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+HEADERS += \
+    ../../../thirdpartylib/civetweb/CivetServer.h \
+    ../../../thirdpartylib/civetweb/civetweb.h \
+    ../../include/msgtype/rawpic.pb.h
+
+DEFINES += NO_SSL
+#DEFINES += NO_SSL_DL
+DEFINES += USE_WEBSOCKET
+
+
+
+LIBS += -ldl -lrt

+ 12 - 3
src/ui/ui_ads_web/main.cpp

@@ -254,9 +254,8 @@ class WebSocketHandler : public CivetWebSocketHandler {
         QString strsend = QString(QJsonDocument(obj1).toJson(QJsonDocument::Compact));
 
 
-//        printf(strsend.toLatin1().data());
+
                 mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT,strsend.toLatin1().data(),strsend.length());
-          //      mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT,"hello",5);
                 return (data_len<5);
 
         char strout[100];
@@ -288,6 +287,7 @@ class PicHandler : public CivetHandler
     handleGet(CivetServer *server, struct mg_connection *conn)
     {
 
+        static int ncount;
  //       return false;
 //        mg_printf(conn,
 //                  "HTTP/1.1 200 OK\r\nContent-Type: "
@@ -307,6 +307,14 @@ class PicHandler : public CivetHandler
                   "boundary=--BoundaryString\r\n"
                   "\r\n");
 
+        mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
+        mg_printf(conn,
+        "<script type=\"text/javascript\">\r\n"
+            "function myrefresh() {\r\n"
+                "window.location.reload();\r\n"
+            "}\r\n"
+            "setTimeout('myrefresh()', 1000);\r\n"
+        "</script>\r\n");
 
         QFile fFile;
         fFile.setFileName("/home/yuchuli/1.jpg");
@@ -329,7 +337,8 @@ class PicHandler : public CivetHandler
         mg_printf(conn, "\r\n\r\n");
 
 
-        printf("send pic.\n");
+        ncount++;
+        printf("send pic. %d\n",ncount);
 
         return true;
     }

+ 3 - 2
src/ui/ui_ads_web/ui_ads_web.pro

@@ -58,8 +58,9 @@ INCLUDEPATH += $$PWD/../../include/msgtype
 
 LIBS += -lprotobuf
 
-INCLUDEPATH += $$PWD/../../../include/
-LIBS += -L$$PWD/../../../bin/ -lmodulecomm -livlog
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
 
 unix:LIBS += -lboost_thread -lboost_system -lboost_serialization