blackjack / webcam

Golang webcam library for Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to store the data from webcam in a file?

jabrena opened this issue · comments

Hi,

I am a newbie Go developer and I would like to know how to store the picture saved in HDD in a image format as (png/jpg)

Can you help me?

Image in a byte array that is received by webcam.GetImg("/dev/your_webcam") will be in YUV format.
First you have to convert YUV to RGB using formula
R = Y + 1.140V
G = Y - 0.395U - 0.581V
B = Y + 2.032U

And then save the image as JPEG using https://golang.org/pkg/image/jpeg/#Encode

I cannot give you exact code snippet right now, but will try to do it today.

I think you can even define image straight in YUV color mode using https://golang.org/src/image/color/ycbcr.go?s=2328:2366#L72

Hi @blackjack Many thanks for the support. I will try to implement this way.

I just want to state that this library in current state is not really suitable to reliably work with webcam (better way is maybe use OpenCL bindings for that. But since it looks like it's pretty popular already (21 star on github) I maybe will have to prettify it :)

Hi @blackjack yes, besides, I found another "sugar" of your idea in another repo.
Many thanks for the support.

Hi @blackjack , very cool library. I too am looking for a way to spit out a single JPEG image from a webcam.

I've tried reading a single frame and attempting to write it out as a JPEG image, but I keep running into a variety of errors. Is there a simple way to make this work?

Thanks for your time.

Oops, sorry, I didn't understand you correctly.
So first, you have to look at the formats your webcam supports.
If there's MJPEG format, grab a frame from camera and just save it - it should be a valid JPEG image. Otherwise you have to convert received frame to JPEG by yourself.