N1 路:
这应该适用于 python,您不需要安装任何额外的东西,但一定要通过 apt-get 进行更新和升级:
#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *
import pygame.camera
width = 640
height = 480
#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()
#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')
#take a picture
image = cam.get_image()
cam.stop()
#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')
这行得通,它不是那么快速和干净,但行得通。使用 pygame 是捕获它的经典方法之一。
N2路:
这是需要这个库v4l2capture 的另一种方式,你应该像这样使用它:
import Image
import select
import v4l2capture
# Open the video device.
video = v4l2capture.Video_device("/dev/video0")
# Suggest an image size to the device. The device may choose and
# return another size if it doesn't support the suggested one.
size_x, size_y = video.set_format(1280, 1024)
# Create a buffer to store image data in. This must be done before
# calling 'start' if v4l2capture is compiled with libv4l2. Otherwise
# raises IOError.
video.create_buffers(1)
# Send the buffer to the device. Some devices require this to be done
# before calling 'start'.
video.queue_all_buffers()
# Start the device. This lights the LED if it's a camera that has one.
video.start()
# Wait for the device to fill the buffer.
select.select((video,), (), ())
# The rest is easy :-)
image_data = video.read()
video.close()
image = Image.fromstring("RGB", (size_x, size_y), image_data)
image.save("image.jpg")
print "Saved image.jpg (Size: " + str(size_x) + " x " + str(size_y) + ")"
安装
对于这个库,您需要安装 libv4l,例如 sudo apt-get install libv4l。
v4l2capture 默认需要 libv4l。您可以编译 v4l2capture
没有 libv4l,但这会减少对 YUYV 输入的图像格式支持
和 RGB 输出。
python-v4l2capture 使用 distutils。
构建:sudo ./setup.py build
构建
并安装:sudo ./setup.py install
N3 路:
我个人的方式是通过 node.js 服务器,它提供了自动和网络的能力。这是我的工作示例:initalazie_server
但那是没有相机的,你应该补充:
var camera = require('v4l2camera');
var cam = new camera.Camera("/dev/video0");
cam.start();
cam.capture(function (success) {
var frame = cam.frameRaw();
fs.createWriteStream("/home/pi/result.jpg").end(Buffer(frame));
});
如何安装node.js的解释在这里:Instalation of node