步骤
1、运行示例二维码图像,生成二维码,读取二维码
看下效果。
需要读懂程序。
建议看星瞳的视频教程。
2、结合LCD显示二维码信息。
新建一个程序
把LCDtest.py的程序拷贝进行。
然后在里面加上检测二维码的程序语句
。如果检测到二维码,是给payload的成员。
把payload在LCD显示出来。
比如二维码是
123+321
code.payload=‘123+321’
直接显示就可以了。
然后将程序下载到图像模块中
断开IDE的连接。
按下模块的复位按键,最左边的那个。
模块可以离线运行程序,检测二维码数据,显示在屏幕中。
下面是具体的代码
# WeAct Studio
# 微行工作室
# TFT ST7735 Test
import sensor, image, time
from ST7735 import TFT,TFTColor
from sysfont import sysfont
from machine import Pin, SPI
import pyb,time
spi = SPI(-1, baudrate=20000000, polarity=0, phase=0, sck=Pin('E12'), mosi=Pin('E14'), miso=Pin('A0'))
# DC - RS/DC data/command flag
# CS - Chip Select, enable communication
# RST/RES - Reset
dc = Pin('E13', Pin.OUT, Pin.PULL_NONE)
cs = Pin('E11', Pin.OUT, Pin.PULL_NONE)
rst = Pin('E15', Pin.OUT, Pin.PULL_NONE)
lcd_led = Pin('E10', Pin.OUT, Pin.PULL_NONE)
lcd_led.value(1)
Lcd_LEDCount=0
LCD_LEDSet=0
def LCD_LEDCtrl(timer):
global Lcd_LEDCount,LCD_LEDSet
Lcd_LEDCount=Lcd_LEDCount+1
if Lcd_LEDCount == 10:
Lcd_LEDCount=0
if LCD_LEDSet > Lcd_LEDCount: lcd_led.value(0)
else: lcd_led.value(1)
lcd_led_tim = pyb.Timer(2)
lcd_led_tim.init(freq=4000) # Freq: 4KHz
lcd_led_tim.callback(LCD_LEDCtrl)
def LCD_ShowBmp(_tft,FileName):
f=open(FileName, 'rb')
print(FileName)
if f.read(2) == b'BM': #header
dummy = f.read(8) #file size(4), creator bytes(4)
offset = int.from_bytes(f.read(4), 'little')
hdrsize = int.from_bytes(f.read(4), 'little')
width = int.from_bytes(f.read(4), 'little')
height = int.from_bytes(f.read(4), 'little')
if int.from_bytes(f.read(2), 'little') == 1: #planes must be 1
depth = int.from_bytes(f.read(2), 'little')
if depth == 24 and int.from_bytes(f.read(4), 'little') == 0:#compress method == uncompressed
print("Image size:", width, "x", height)
rowsize = (width * 3 + 3) & ~3
if height < 0:
height = -height
flip = False
else:
flip = True
w, h = width, height
if w > 128: w = 128
if h > 160: h = 160
tft._setwindowloc((0,0),(w - 1,h - 1))
for row in range(h):
if flip:
pos = offset + (height - 1 - row) * rowsize
else:
pos = offset + row * rowsize
if f.tell() != pos:
dummy = f.seek(pos)
for col in range(w):
bgr = f.read(3)
_tft._pushcolor(TFTColor(bgr[0],bgr[1],bgr[2]))
else:
print(FileName+'is not 24bit pic')
tft=TFT(spi,dc,rst,cs)
tft.init_7735(TFT.REDTAB)
tft.rotation(2)
LCD_ShowBmp(tft,'WeAct_logo_128_160.bmp')
time.sleep(50)
LCD_LEDSet=1
time.sleep(1000)
tft.fill(TFT.BLACK)
tft.text((0, 30), 'Hello WeAct!', TFT.WHITE, sysfont, 2, nowrap=False)
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
if sensor.get_id() == sensor.OV7725:
sensor.set_hmirror(True)
sensor.set_vflip(True)
sensor.skip_frames(time = 1000) # Wait for settings take effect.
clock = time.clock() # Create a clock object to track the FPS.
time.sleep(2000)
tft.fill(TFT.WHITE)
while(True):
clock.tick() # Update the FPS clock.
img = sensor.snapshot() # Take a picture and return the image.
img.lens_corr(0.1) # strength of 1.8 is good for the 2.8mm lens.
for code in img.find_qrcodes():
img.draw_rectangle(code.rect(), color = (255, 0, 0))
print(code) # to the IDE. The FPS should increase once disconnected.
tft.text((0, 30), code.payload(), TFT.RED, sysfont,8, nowrap=False)