树莓派按键中断实现摄像头拍照
Posted guguobao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树莓派按键中断实现摄像头拍照相关的知识,希望对你有一定的参考价值。
先安装PiCamera模块
使用Python中断函数add_event_detect,并定义好回调函数call_back()
- add_event_detect(channel, GPIO.RISING, callback=test_callback, bouncetime=200)
- 上升沿检测,关联回调,bouncetime用于按键软件防抖
调用PiCamera方法
def catpure_img():
camera = PiCamera()
camera.resolution = (1024,768)
camera.start_preview() #预览2秒
# Camera warm-up time 2s,beacause it need 2‘s to ***
GPIO.output(22,GPIO.HIGH)
sleep(2)
camera.capture(‘img_catpure/two.jpg‘) #捕捉图片
camera.stop_preview() # 关闭预览
camera.close() #要关闭,不然第二次中断响应会报错
GPIO.output(22, GPIO.LOW)
总代吗
#!coding:utf-8
from time import sleep
from picamera import PiCamera
import RPi.GPIO as GPIO
BtnPin = 11
Gpin = 12
Rpin = 13
def catpure_img():
camera = PiCamera()
camera.resolution = (1024,768)
camera.start_preview()
# Camera warm-up time 2s,beacause it need 2‘s to ***
GPIO.output(22,GPIO.HIGH)
sleep(2)
camera.capture(‘img_catpure/two.jpg‘)
camera.stop_preview()
camera.close()
GPIO.output(22, GPIO.LOW)
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPios by physical location
GPIO.setup(Gpin, GPIO.OUT) # Set Green Led Pin mode to output
GPIO.setup(Rpin, GPIO.OUT) # Set Red Led Pin mode to output
GPIO.setup(22, GPIO.OUT)
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin‘s mode is input, and pull up to high level(3.3V)
GPIO.add_event_detect(BtnPin, GPIO.BOTH, callback=detect, bouncetime=200)
def Led(x):
if x == 0:
GPIO.output(Rpin, 1)
GPIO.output(Gpin, 0)
catpure_img()
if x == 1:
GPIO.output(Rpin, 0)
GPIO.output(Gpin, 1)
def Print(x):
if x == 0:
print(‘ ***********************‘)
print(‘ * Button Pressed! *‘)
print(‘ ***********************‘)
def detect(chn):
Led(GPIO.input(BtnPin))
Print(GPIO.input(BtnPin))
def loop():
while True:
pass
def destroy():
GPIO.output(Gpin, GPIO.HIGH) # Green led off
GPIO.output(Rpin, GPIO.HIGH) # Red led off
GPIO.cleanup() # Release resource
if __name__ == ‘__main__‘: # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When ‘Ctrl+C‘ is pressed, the child program destroy() will be executed.
destroy()
以上是关于树莓派按键中断实现摄像头拍照的主要内容,如果未能解决你的问题,请参考以下文章