github.com/aamcrae/webcam@v0.0.0-20210915060337-934acc13bdc3/examples/getcontrols/getcontrols.go (about)

     1  // Example program that reads the list of available controls and prints them.
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"sort"
     8  
     9  	"github.com/aamcrae/webcam"
    10  )
    11  
    12  var device = flag.String("input", "/dev/video0", "Input video device")
    13  
    14  type control struct {
    15  	id       webcam.ControlID
    16  	name     string
    17  	min, max int32
    18  }
    19  
    20  func main() {
    21  	flag.Parse()
    22  	cam, err := webcam.Open(*device)
    23  	if err != nil {
    24  		panic(fmt.Errorf("%s: %v", *device, err.Error()))
    25  	}
    26  	defer cam.Close()
    27  
    28  	fmap := cam.GetSupportedFormats()
    29  	fmt.Println("Available Formats: ")
    30  	for p, s := range fmap {
    31  		var pix []byte
    32  		for i := 0; i < 4; i++ {
    33  			pix = append(pix, byte(p>>uint(i*8)))
    34  		}
    35  		fmt.Printf("ID:%08x ('%s') %s\n   ", p, pix, s)
    36  		for _, fs := range cam.GetSupportedFrameSizes(p) {
    37  			fmt.Printf(" %s", fs.GetString())
    38  		}
    39  		fmt.Printf("\n")
    40  	}
    41  
    42  	cmap := cam.GetControls()
    43  	fmt.Println("Available controls: ")
    44  	var clist []control
    45  	for id, cm := range cmap {
    46  		var c control
    47  		c.id = id
    48  		c.name = cm.Name
    49  		c.min = cm.Min
    50  		c.max = cm.Max
    51  		clist = append(clist, c)
    52  	}
    53  	sort.Slice(clist, func(i, j int) bool {
    54  		return clist[i].name < clist[j].name
    55  	})
    56  	for _, cl := range clist {
    57  		fmt.Printf("ID:%08x %-32s  Min: %4d  Max: %5d\n", cl.id,
    58  			cl.name, cl.min, cl.max)
    59  	}
    60  }