通過html和cgi實現拍照顯示功能

作者:任程明,華清遠見嵌入式培訓中心講師。

1. 編寫html網頁 :video.html。

網頁內容如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
         <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <title>video</title>
         <style type="text/css">
         <!--
          .STYLE1 {
         font-size: xx-large;
         font-style: italic;
         color: #0033FF;
          }
          -->
          </style>
          </head>
         <body>
         <table width="652" height="163" background="images/h3.png" border="0" align="center">
         <tr>
         <td><span class="STYLE1">example</span></td>
         </tr>
         </table><tr>
          <div align="center">video</div>
          <table width="500" align="center" height="561" border="0">
          <tr>

(1)前期移植了mjpg-streamer 來獲得視頻流。

(2)通過“ http://192.168.1.200:8080/?action=stream”來查看視頻流的捕獲情況。

<td height="500"><img src="http://192.168.1.200:8080/?action=stream"/></td>
          </tr>
          <tr>
         <td height="55"><form id="form3" name="form3" method="post" action="./cgi-bin/capture.cgi">
         <table width="500" border="1" bgcolor="#CCFFFF" bordercolor="#5500FF">
         <tr>
         <td width="195"><p>
          <label></label>
         <a href="choose.html">choose /a><br />
         </p></td>
         <td width="289">
         <div align="center">

(3)前期移植了boa服務器。

(4)點擊網頁上的“單拍”按鈕後,調用服務器中的cgi:picture.cgi,對圖像數據進行採集。

<input type="submit" name="button3" id="button3" value="picture" />&nbsp;&nbsp;&nbsp;<a href="cgi-bin/picture.cgi">單拍 </a></div></td>
         </tr>
          </table>
          </form></td>
          </tr>
          </table>
          </body>
          </html>

2. 用於獲取圖片的cgi程序:picture.c .

Cgi程序內容如下:

#include <stdio.h>
        #include"cgic.h"
        #include <string.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <errno.h>
        #include <stddef.h>
        #include <sys/stat.h>
        #include <dirent.h>
        #include <iconv.h>
        #include <sys/types.h>
         #include <sys/wait.h>
         int cgiMain()
         {

(1)使用文件系統中的文件夾相應的操作變量:

DIR *dir;
         struct dirent *dirp;

(2)cgi程序將相關的網頁信息到瀏覽器中顯示:

cgiHeaderContentType("text/html");
         fprintf(cgiOut, "<HTML>\n");
         fprintf(cgiOut, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
         fprintf(cgiOut, " <html xmlns=\"http://www.w3.org/1999/xhtml\"><head>");
         fprintf(cgiOut, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
         fprintf(cgiOut, "<link rel=\"stylesheet\" href=\"../images/style.css\" type=\"text/css\" /> <title>history Picture</title></head>");
         fprintf(cgiOut, "<body>");
         fprintf(cgiOut, "<H1 align=\"left\">history Picture:<H1>");

(3)編寫cgi程序讀取文件系統中的圖片文件

if((dir = opendir("../pice/")) == NULL)
         {
         perror("fail to opendir");
         return -1;
         }
         if(dir != NULL)
         {
         while((dirp = readdir(dir)) != NULL)
         {
         if(dirp->d_name[0] == '.') continue;
         fprintf(cgiOut, "<div align=\"center\">");
         fprintf(cgiOut,"%s",dirp->d_name);
          fprintf(cgiOut, "</div>");

(4)通過cgi程序顯示圖片文件到網頁:

fprintf(cgiOut, "<div align=\"center\"><img src=\"../pice/%s\" width=\"320\" height=\"240\" />",dirp->d_name);
         fprintf(cgiOut, "</div>");
         }
         }
         fprintf(cgiOut, "</BODY></HTML>");
         return 0;
         }

原文出處:http://blog.csdn.net/farsight2009/article/details/7737414

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章