blackjack / webcam

Golang webcam library for Linux
MIT License
411 stars 90 forks source link

Invalid argument on capture #51

Open metal3d opened 2 years ago

metal3d commented 2 years ago

Hello,

I'm very interested by your package but nothing works for me...

Here is a sample code:

package main

import (
    "log"
    "strings"

    "github.com/blackjack/webcam"
)

func main() {
    cam, err := webcam.Open("/dev/video0")
    if err != nil {
        panic(err.Error())
    }
    defer cam.Close()

    // Set the camera's resolution
    formats := cam.GetSupportedFormats()
    var format webcam.PixelFormat
    var width, height uint32

    for pf, f := range formats {
        f = strings.ToLower(f)
        log.Println("possible format", f)
        if strings.Contains(f, "jpeg") {
            format = pf
        }
    }
    sizes := cam.GetSupportedFrameSizes(format)
    for _, size := range sizes {
        log.Println("possible size:", size)
        if size.MaxWidth == 0 || size.MaxHeight == 0 {
            continue
        }
        width = size.MaxWidth
        height = size.MaxHeight
    }
    log.Println("Set resolution to: ", width, height, "and pixel format to: ", formats[format], format)

    cam.SetImageFormat(format, width, height)
    cam.SetFramerate(10)
    cam.SetBufferCount(1)

    err = cam.WaitForFrame(10)
    if err != nil {
        panic(err.Error())
    }
    frame, _, err := cam.GetFrame()
    if err != nil {
        log.Println(err.Error())
    } else {
        log.Println(len(frame))
    }
}

Whatever I try:

2022/05/18 14:00:43 possible format yuyv 4:2:2
2022/05/18 14:00:43 possible format motion-jpeg
2022/05/18 14:00:43 possible size: {1280 1280 0 720 720 0}
2022/05/18 14:00:43 possible size: {160 160 0 120 120 0}
2022/05/18 14:00:43 possible size: {176 176 0 144 144 0}
2022/05/18 14:00:43 possible size: {320 320 0 240 240 0}
2022/05/18 14:00:43 possible size: {352 352 0 288 288 0}
2022/05/18 14:00:43 possible size: {640 640 0 480 480 0}
2022/05/18 14:00:43 Set resolution to:  640 480 and pixel format to:  Motion-JPEG 1196444237
2022/05/18 14:00:43 invalid argument

Note that the error is raised on the GetFrame() call. I also try to not wait for the frame and to use ReadFrame but I've always got this error.

Can you tell me what I'm doing wrong ?