樹莓派按鍵中斷實現攝像頭拍照
阿新 • • 發佈:2018-10-01
cau *** res tro exce HERE tar view 3.3
先安裝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()
樹莓派按鍵中斷實現攝像頭拍照