树莓派门禁系统模块:拍照

模块功能

  • 连续拍四张照片
  • 把照片名称写入数据库

使用picamera模块来拍照,每个照片以第一张时间为命名,并且_1,_2,_3,_4结尾。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
title="拍照模块">default_encoding = 'utf-8'
import time
import picamera
import datetime
import sqlite3

def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S")

def shoot():
pic_format = 'jpeg'
conn = sqlite3.connect('database.db')
cam = picamera.PiCamera()
PicName = getFileName()
conn.execute("INSERT INTO PICLIST (ID,NAME) VALUES (NULL, ?)",(PicName,))
cam.resolution = (1024, 768)
cam.start_preview()
save_path = '/root/app/test/static/img/'
for i in range(1,5):
t = '_' + str(i)
cam.annotate_text = PicName + t
cam.capture(save_path + PicName + t + '.' + pic_format, format=pic_format)
print('Captured image %s' % PicName + t)
time.sleep(0.25)
cam.stop_preview()
conn.commit()
conn.close()
return

shoot()