• 树莓派按键中断实现摄像头拍照


    先安装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()
    
    
    
  • 相关阅读:
    Hadoop and net core a match made in docker
    JavaScript封装方法,兼容参数类型为Number和String
    HTML的input类型为hidden导致无法reset改字段的value问题
    MyBatis自动生成Java/C#的Bean(Entity)的等价MYSQL实现函数
    JMX configuration for Tomcat
    Resolved validation conflict with readonly
    FreeMarker example all in one
    Notepad++ 大小写转换
    常用编辑器列模式快捷键
    Redis应用一例(存证数量用计数器实现)
  • 原文地址:https://www.cnblogs.com/guguobao/p/9735686.html
Copyright © 2020-2023  润新知