第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在對(duì)象中縮放 mjpeg

在對(duì)象中縮放 mjpeg

Go
慕桂英546537 2022-12-19 21:05:47
使用gocv我正在將圖像流式傳輸?shù)給bject我的 html5 頁(yè)面上的元素。該頁(yè)面是:<!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>
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

我在代碼中找到了答案go,即使用以下方法調(diào)整圖像大?。?/p>


"image/jpeg"

"golang.org/x/image/draw"

作為:


    // Decode the image (from PNG to image.Image):

    src, _ := j.Decode(bytes.NewReader(jpeg))


    // Set the expected size that you want:

    dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/3, src.Bounds().Max.Y/3))


    // Resize:

    draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)


    buf := new(bytes.Buffer)


    // Encode to `buf`:

    j.Encode(buf, dst, nil)


    copy(s.frame, header)

    //  copy(s.frame[len(header):], jpeg)

    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:

//  stream = mjpeg.NewStream()

//  http.Handle("/camera", stream)

// Then push new JPEG frames to the connected clients using stream.UpdateJPEG().

package mjpeg


import (

    "bytes"

    "fmt"

    "image"

    j "image/jpeg"

    "log"

    "net/http"

    "sync"

    "time"


    "golang.org/x/image/draw"

)


// Stream represents a single video feed.

type Stream struct {

    m             map[chan []byte]bool

    frame         []byte

    lock          sync.Mutex

    FrameInterval time.Duration

}


const boundaryWord = "MJPEGBOUNDARY"

const headerf = "\r\n" +

    "--" + boundaryWord + "\r\n" +

    "Content-Type: image/jpeg\r\n" +

    "Content-Length: %d\r\n" +

    "X-Timestamp: 0.000000\r\n" +

    "\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) {

    log.Println("Stream:", r.RemoteAddr, "connected")

    w.Header().Add("Content-Type", "multipart/x-mixed-replace;boundary="+boundaryWord)


    c := make(chan []byte)

    s.lock.Lock()

    s.m[c] = true

    s.lock.Unlock()


    for {

        time.Sleep(s.FrameInterval)

        b := <-c

        _, err := w.Write(b)

        if err != nil {

            break

        }

    }


    s.lock.Lock()

    delete(s.m, c)

    s.lock.Unlock()

    log.Println("Stream:", r.RemoteAddr, "disconnected")

}


// UpdateJPEG pushes a new JPEG frame onto the clients.

func (s *Stream) UpdateJPEG(jpeg []byte) {

    header := fmt.Sprintf(headerf, len(jpeg))

    if len(s.frame) < len(jpeg)+len(header) {

        s.frame = make([]byte, (len(jpeg)+len(header))*2)

    }


    // Decode the image (from PNG to image.Image):

    src, _ := j.Decode(bytes.NewReader(jpeg))


    // Set the expected size that you want:

    dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/3, src.Bounds().Max.Y/3))


    // Resize:

    draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)


    buf := new(bytes.Buffer)


    // Encode to `buf`:

    j.Encode(buf, dst, nil)


    copy(s.frame, header)

    //  copy(s.frame[len(header):], jpeg)

    copy(s.frame[len(header):], buf.Bytes())


    s.lock.Lock()

    for c := range s.m {

        // Select to skip streams which are sleeping to drop frames.

        // This might need more thought.

        select {

        case c <- s.frame:

        default:

        }

    }

    s.lock.Unlock()

}


// NewStream initializes and returns a new Stream.

func NewStream() *Stream {

    return &Stream{

        m:             make(map[chan []byte]bool),

        frame:         make([]byte, len(headerf)),

        FrameInterval: 50 * time.Millisecond,

    }

}

我的任何輸出變成:

http://img1.sycdn.imooc.com//63a061c90001378f06530421.jpg

查看完整回答
反對(duì) 回復(fù) 2022-12-19
  • 1 回答
  • 0 關(guān)注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)