import sensor, image, time
from pyb import Pin
from pyb import LED,Pin

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.VGA)

sensor.set_windowing(320,320)

sensor.set_auto_whitebal(False)

#gain_par =0.8
#sensor.set_auto_gain(False, gain_db_ceiling=gain_par)  # Default gain 10.

sensor.set_auto_gain(False)  # Default gain 10.

EXPOSURE_MICROSECONDS = 1000000
#sensor.set_auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)
sensor.set_auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)

try:
    # The camera will now focus on whatever is in front of it.
    sensor.ioctl(sensor.IOCTL_TRIGGER_AUTO_FOCUS)
except:
    raise (Exception("Auto focus is not supported by your sensor/board combination."))

sensor.skip_frames(time = 2000)
clock = time.clock()

blue_led = LED(1)
KEY = Pin('C13',Pin.IN,Pin.PULL_DOWN)

print("You're on camera!")
keycount=0
# 轻触开关
pin0 = Pin('P0', Pin.IN, Pin.PULL_UP)

waitkeyflag=0
keyval=0
#等开关按下并松开
def wait_key():
    global keyval,waitkeyflag
    while KEY.value():
        img = sensor.snapshot()
        time.sleep_ms(10)
        while KEY.value():
            while KEY.value(): #wait key up
                i=0   #anything you like
                print("here01")
            keyval=100
            waitkeyflag=1
#            print(keyval)
#            print(waitkeyflag)
#            print("key press11")
#        for y in range(len(rois)):
#            for x in range(len(rois[y])):
#                img.draw_rectangle(rois[y][x])
#    while not KEY.value():
#        time.sleep_ms(1)


while(True):
    clock.tick()

#    if waitkeyflag==0:
#        wait_key()
#        print(waitkeyflag)
#        print("wait key00")
#    elif waitkeyflag==1 :
#        #        img = sensor.snapshot().lens_corr(1.8)

#        img = sensor.snapshot()
#        print(clock.fps())
    img = sensor.snapshot()
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color=127)
        #img.draw_rectangle(roi=ROI, color=127)
        print(code)
    print(clock.fps())

mv:代码改进001测试二维码,曝光,白平衡,设置窗口,参考案例tag,设置不同的图片格式_stm32