在对象中缩放 mjpeg

使用gocv我正在将图像流式传输到object我的 html5 页面上的元素。


该页面是:


<!DOCTYPE html>

<html>

    <head>

        <title>Cam Streaming with gocv</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link href="css/style.css" rel="stylesheet">

    </head>

    <body>

    <!-- <div id ="content"></div> -->

        <object data="http://localhost:8080/camera" width="300" height="200" alt="Cam streaming"></object>

    </body>

    <<script>

     /*   (function(){

                document.getElementById("content").innerHTML='<object type="text/html" data="http://localhost:8080/cam" ></object>';

        })();

        */

    </script>

</html>


慕桂英546537
浏览 98回答 1
1回答

泛舟湖上清波郎朗

我在代码中找到了答案go,即使用以下方法调整图像大小:"image/jpeg""golang.org/x/image/draw"作为:&nbsp; &nbsp; // Decode the image (from PNG to image.Image):&nbsp; &nbsp; src, _ := j.Decode(bytes.NewReader(jpeg))&nbsp; &nbsp; // Set the expected size that you want:&nbsp; &nbsp; dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/3, src.Bounds().Max.Y/3))&nbsp; &nbsp; // Resize:&nbsp; &nbsp; draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)&nbsp; &nbsp; buf := new(bytes.Buffer)&nbsp; &nbsp; // Encode to `buf`:&nbsp; &nbsp; j.Encode(buf, dst, nil)&nbsp; &nbsp; copy(s.frame, header)&nbsp; &nbsp; //&nbsp; copy(s.frame[len(header):], jpeg)&nbsp; &nbsp; copy(s.frame[len(header):], buf.Bytes())所以,我的流媒体完整代码变成了:// Package mjpeg implements a simple MJPEG streamer.//// Stream objects implement the http.Handler interface, allowing to use them with the net/http package like so://&nbsp; stream = mjpeg.NewStream()//&nbsp; http.Handle("/camera", stream)// Then push new JPEG frames to the connected clients using stream.UpdateJPEG().package mjpegimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "image"&nbsp; &nbsp; j "image/jpeg"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "sync"&nbsp; &nbsp; "time"&nbsp; &nbsp; "golang.org/x/image/draw")// Stream represents a single video feed.type Stream struct {&nbsp; &nbsp; m&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map[chan []byte]bool&nbsp; &nbsp; frame&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[]byte&nbsp; &nbsp; lock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sync.Mutex&nbsp; &nbsp; FrameInterval time.Duration}const boundaryWord = "MJPEGBOUNDARY"const headerf = "\r\n" +&nbsp; &nbsp; "--" + boundaryWord + "\r\n" +&nbsp; &nbsp; "Content-Type: image/jpeg\r\n" +&nbsp; &nbsp; "Content-Length: %d\r\n" +&nbsp; &nbsp; "X-Timestamp: 0.000000\r\n" +&nbsp; &nbsp; "\r\n"// ServeHTTP responds to HTTP requests with the MJPEG stream, implementing the http.Handler interface.func (s *Stream) ServeHTTP(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; log.Println("Stream:", r.RemoteAddr, "connected")&nbsp; &nbsp; w.Header().Add("Content-Type", "multipart/x-mixed-replace;boundary="+boundaryWord)&nbsp; &nbsp; c := make(chan []byte)&nbsp; &nbsp; s.lock.Lock()&nbsp; &nbsp; s.m[c] = true&nbsp; &nbsp; s.lock.Unlock()&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(s.FrameInterval)&nbsp; &nbsp; &nbsp; &nbsp; b := <-c&nbsp; &nbsp; &nbsp; &nbsp; _, err := w.Write(b)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; s.lock.Lock()&nbsp; &nbsp; delete(s.m, c)&nbsp; &nbsp; s.lock.Unlock()&nbsp; &nbsp; log.Println("Stream:", r.RemoteAddr, "disconnected")}// UpdateJPEG pushes a new JPEG frame onto the clients.func (s *Stream) UpdateJPEG(jpeg []byte) {&nbsp; &nbsp; header := fmt.Sprintf(headerf, len(jpeg))&nbsp; &nbsp; if len(s.frame) < len(jpeg)+len(header) {&nbsp; &nbsp; &nbsp; &nbsp; s.frame = make([]byte, (len(jpeg)+len(header))*2)&nbsp; &nbsp; }&nbsp; &nbsp; // Decode the image (from PNG to image.Image):&nbsp; &nbsp; src, _ := j.Decode(bytes.NewReader(jpeg))&nbsp; &nbsp; // Set the expected size that you want:&nbsp; &nbsp; dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/3, src.Bounds().Max.Y/3))&nbsp; &nbsp; // Resize:&nbsp; &nbsp; draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)&nbsp; &nbsp; buf := new(bytes.Buffer)&nbsp; &nbsp; // Encode to `buf`:&nbsp; &nbsp; j.Encode(buf, dst, nil)&nbsp; &nbsp; copy(s.frame, header)&nbsp; &nbsp; //&nbsp; copy(s.frame[len(header):], jpeg)&nbsp; &nbsp; copy(s.frame[len(header):], buf.Bytes())&nbsp; &nbsp; s.lock.Lock()&nbsp; &nbsp; for c := range s.m {&nbsp; &nbsp; &nbsp; &nbsp; // Select to skip streams which are sleeping to drop frames.&nbsp; &nbsp; &nbsp; &nbsp; // This might need more thought.&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case c <- s.frame:&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; s.lock.Unlock()}// NewStream initializes and returns a new Stream.func NewStream() *Stream {&nbsp; &nbsp; return &Stream{&nbsp; &nbsp; &nbsp; &nbsp; m:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;make(map[chan []byte]bool),&nbsp; &nbsp; &nbsp; &nbsp; frame:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;make([]byte, len(headerf)),&nbsp; &nbsp; &nbsp; &nbsp; FrameInterval: 50 * time.Millisecond,&nbsp; &nbsp; }}我的任何输出变成:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go