From a9c82d2d6e04cb43b33bbfc18105b8ef60466253 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Fri, 6 Oct 2017 10:29:21 +0200 Subject: [PATCH] Add option to draw images with WebGL --- README.md | 22 ++++----- cam2ip.go | 1 + handlers/html.go | 115 ++++++++++++++++++++++++++++++++++++++++++++--- server/server.go | 3 +- 4 files changed, 124 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 57a755a..ee9b673 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,10 @@ or Binaries are compiled with static OpenCV library: - - [Android 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.1/cam2ip-1.1-android.tar.gz) - - [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.1/cam2ip-1.1-64bit.tar.gz) - - [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.1/cam2ip-1.1-RPi.tar.gz) - - [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.1/cam2ip-1.1.zip) + - [Android 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-android.tar.gz) + - [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-64bit.tar.gz) + - [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-RPi.tar.gz) + - [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2.zip) ### Installation @@ -35,17 +35,19 @@ This will install app in `$GOPATH/bin/cam2ip`. ``` Usage of ./cam2ip: -bind-addr string - Bind address (default ":56000") + Bind address (default ":56000") -delay int - Delay between frames, in milliseconds (default 10) + Delay between frames, in milliseconds (default 10) -frame-height float - Frame height (default 480) + Frame height (default 480) -frame-width float - Frame width (default 640) + Frame width (default 640) -htpasswd-file string - Path to htpasswd file, if empty auth is disabled + Path to htpasswd file, if empty auth is disabled -index int - Camera index + Camera index + -webgl + Use WebGL to draw images ``` ### Handlers diff --git a/cam2ip.go b/cam2ip.go index b66ebff..68a7f85 100644 --- a/cam2ip.go +++ b/cam2ip.go @@ -21,6 +21,7 @@ func main() { flag.IntVar(&srv.Delay, "delay", 10, "Delay between frames, in milliseconds") flag.Float64Var(&srv.FrameWidth, "frame-width", 640, "Frame width") flag.Float64Var(&srv.FrameHeight, "frame-height", 480, "Frame height") + flag.BoolVar(&srv.WebGL, "webgl", false, "Use WebGL to draw images") flag.StringVar(&srv.Bind, "bind-addr", ":56000", "Bind address") flag.StringVar(&srv.Htpasswd, "htpasswd-file", "", "Path to htpasswd file, if empty auth is disabled") flag.Parse() diff --git a/handlers/html.go b/handlers/html.go index 8f135a7..3f47b51 100644 --- a/handlers/html.go +++ b/handlers/html.go @@ -13,7 +13,7 @@ type HTML struct { } // NewHTML returns new HTML handler. -func NewHTML(bind string, width, height float64) *HTML { +func NewHTML(bind string, width, height float64, gl bool) *HTML { h := &HTML{} b := strings.Split(bind, ":") @@ -21,11 +21,16 @@ func NewHTML(bind string, width, height float64) *HTML { bind = "127.0.0.1" + bind } - html = strings.Replace(html, "{BIND}", bind, -1) - html = strings.Replace(html, "{WIDTH}", fmt.Sprintf("%.0f", width), -1) - html = strings.Replace(html, "{HEIGHT}", fmt.Sprintf("%.0f", height), -1) + tpl := html + if gl { + tpl = htmlWebGL + } - h.Template = []byte(html) + tpl = strings.Replace(tpl, "{BIND}", bind, -1) + tpl = strings.Replace(tpl, "{WIDTH}", fmt.Sprintf("%.0f", width), -1) + tpl = strings.Replace(tpl, "{HEIGHT}", fmt.Sprintf("%.0f", height), -1) + + h.Template = []byte(tpl) return h } @@ -51,7 +56,7 @@ var html = ` var image = new Image(); ws.onopen = function() { - var context = document.getElementById("canvas").getContext("2d"); + var context = document.getElementById("canvas").getContext("2d", {alpha: false}); image.onload = function() { context.drawImage(image, 0, 0); } @@ -72,3 +77,101 @@ var html = ` ` + +var htmlWebGL = ` + + + cam2ip + + + + + + + +
+ +
+ +` diff --git a/server/server.go b/server/server.go index 0530457..7525c5c 100644 --- a/server/server.go +++ b/server/server.go @@ -24,6 +24,7 @@ type Server struct { Delay int FrameWidth float64 FrameHeight float64 + WebGL bool Camera *camera.Camera } @@ -42,7 +43,7 @@ func (s *Server) ListenAndServe() error { basic = auth.NewBasicAuthenticator(realm, auth.HtpasswdFileProvider(s.Htpasswd)) } - http.Handle("/html", newAuthHandler(handlers.NewHTML(s.Bind, s.FrameWidth, s.FrameHeight), basic)) + http.Handle("/html", newAuthHandler(handlers.NewHTML(s.Bind, s.FrameWidth, s.FrameHeight, s.WebGL), basic)) http.Handle("/jpeg", newAuthHandler(handlers.NewJPEG(s.Camera), basic)) http.Handle("/mjpeg", newAuthHandler(handlers.NewMJPEG(s.Camera, s.Delay), basic))