added JPEG and PNG decoding
This commit is contained in:
parent
0a6f56e2cc
commit
437a584fc5
1 changed files with 25 additions and 12 deletions
37
webcam.go
37
webcam.go
|
|
@ -1,8 +1,12 @@
|
||||||
package webcam
|
package webcam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
@ -84,13 +88,6 @@ type buffer struct {
|
||||||
reserved uint32
|
reserved uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type format_dummy struct {
|
|
||||||
t uint32
|
|
||||||
fmt struct {
|
|
||||||
raw_data [201]byte
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type format struct {
|
type format struct {
|
||||||
t uint32
|
t uint32
|
||||||
pix pix
|
pix pix
|
||||||
|
|
@ -264,13 +261,13 @@ func (s FrameSize) String() string {
|
||||||
|
|
||||||
type FrameSizes []FrameSize
|
type FrameSizes []FrameSize
|
||||||
|
|
||||||
func (slice FrameSizes) Len() int { return len(slice) }
|
func (fs FrameSizes) Len() int { return len(fs) }
|
||||||
func (slice FrameSizes) Less(i, j int) bool {
|
func (fs FrameSizes) Less(i, j int) bool {
|
||||||
ls := slice[i].MaxWidth * slice[i].MaxHeight
|
ls := fs[i].MaxWidth * fs[i].MaxHeight
|
||||||
rs := slice[j].MaxWidth * slice[j].MaxHeight
|
rs := fs[j].MaxWidth * fs[j].MaxHeight
|
||||||
return ls < rs
|
return ls < rs
|
||||||
}
|
}
|
||||||
func (slice FrameSizes) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] }
|
func (fs FrameSizes) Swap(i, j int) { fs[i], fs[j] = fs[j], fs[i] }
|
||||||
|
|
||||||
func (w *Webcam) GetFrameSizes(code uint32) (fs FrameSizes) {
|
func (w *Webcam) GetFrameSizes(code uint32) (fs FrameSizes) {
|
||||||
var s FrameSize
|
var s FrameSize
|
||||||
|
|
@ -434,6 +431,22 @@ func (w *Webcam) ReadFrame() ([]byte, error) {
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Webcam) ReadJPEG() (image.Image, error) {
|
||||||
|
buf, err := w.ReadFrame()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return jpeg.Decode(bytes.NewBuffer(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Webcam) ReadPNG() (image.Image, error) {
|
||||||
|
buf, err := w.ReadFrame()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return png.Decode(bytes.NewBuffer(buf))
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Webcam) Close() error {
|
func (w *Webcam) Close() error {
|
||||||
if err := w.mmapReleaseBuffers(); err != nil {
|
if err := w.mmapReleaseBuffers(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue