Skip to content

Instantly share code, notes, and snippets.

@yushulx
Created July 10, 2020 06:39
Show Gist options
  • Save yushulx/0925616db9c3280f47144fb7492079fc to your computer and use it in GitHub Desktop.
Save yushulx/0925616db9c3280f47144fb7492079fc to your computer and use it in GitHub Desktop.
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
pageData := "<!DOCTYPE>" +
"<html>" +
" <head>" +
" <title>HttpListener Example</title>" +
" </head>" +
" <body>" +
"<img id=\"image\"/>" +
" <script type=\"text/javascript\">var image = document.getElementById('image');function refresh() {image.src = \"/image?\" + new Date().getTime();image.onload= function(){setTimeout(refresh, 30);}}refresh();</script> " +
" </body>" +
"</html>"
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(pageData))
} else if strings.HasPrefix(r.URL.Path, "/image") {
webcam.Read(&amp;img)
jpg, _ := gocv.IMEncode(".jpg", img)
w.Write(jpg)
} else {
fmt.Fprintf(w, "Page Not Found")
}
}
func main() {
fmt.Println("Running at port 2020...")
webcam, _ = gocv.OpenVideoCapture(0)
img = gocv.NewMat()
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":2020", nil))
webcam.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment