Created
July 10, 2020 06:39
-
-
Save yushulx/0925616db9c3280f47144fb7492079fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(&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