added JPEG and PNG decoding

This commit is contained in:
ston1th 2016-04-12 22:55:36 +02:00
commit 437a584fc5

View file

@ -1,8 +1,12 @@
package webcam
import (
"bytes"
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"sort"
"syscall"
@ -84,13 +88,6 @@ type buffer struct {
reserved uint32
}
type format_dummy struct {
t uint32
fmt struct {
raw_data [201]byte
}
}
type format struct {
t uint32
pix pix
@ -264,13 +261,13 @@ func (s FrameSize) String() string {
type FrameSizes []FrameSize
func (slice FrameSizes) Len() int { return len(slice) }
func (slice FrameSizes) Less(i, j int) bool {
ls := slice[i].MaxWidth * slice[i].MaxHeight
rs := slice[j].MaxWidth * slice[j].MaxHeight
func (fs FrameSizes) Len() int { return len(fs) }
func (fs FrameSizes) Less(i, j int) bool {
ls := fs[i].MaxWidth * fs[i].MaxHeight
rs := fs[j].MaxWidth * fs[j].MaxHeight
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) {
var s FrameSize
@ -434,6 +431,22 @@ func (w *Webcam) ReadFrame() ([]byte, error) {
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 {
if err := w.mmapReleaseBuffers(); err != nil {
return err