jpg_decode(jpg_filename {, x, y, width, height})
解码 JPG 文件,并以 (buffer, width, height) 元组形式返回整张图像或其中一部分。buffer 是兼容 color565 和 blit_buffer 的字节数组,需要占用 width * height * 2 字节内存。
如果提供了可选参数 x、y、width 和 height,buffer 将只包含图像中的指定区域。
blit_buffer(buffer, x, y, width, height {, alpha})
将 bytes() 或 bytearray() 的内容复制到屏幕内部存储器中由 x、y、width、height 指定的区域。注意:数组中的每个颜色值需要 2 字节,alpha 的默认值为 255。
基于 制作半透明矩形文本框 示例,将 text_rect 方法改写为一个类并添加 erase 方法。在其中使用 jpg_decode 方法保存半透明文本框所在区域的背景,再使用 blit_buffer 方法覆盖半透明文本框,从而实现局部刷新。
""" BPI-Centi-S3 170x320 ST7789 display """
import st7789
import vga1_bold_16x32
import tft_config
import gc
import time
class TextRect:
def __init__(self):
self.tft = None
self.text_y = None
self.text_x = None
self.rect_height = None
self.rect_width = None
def rect(self, tft, font, text, text_coord,
fg=st7789.WHITE, bg=st7789.BLACK, alpha_text=255, alpha_rect=255):
self.tft = tft
self.rect_width = font.WIDTH * len(text)
self.rect_height = font.HEIGHT
self.text_x = text_coord[0]
self.text_y = text_coord[1]
self.tft.fill_rect(self.text_x, self.text_y,
self.rect_width, self.rect_height, bg, alpha_rect)
self.tft.text(font, text, self.text_x, self.text_y,
fg, st7789.TRANSPARENT, alpha_text)
def erase(self, bg):
buffer, _, _ = self.tft.jpg_decode(bg, self.text_x, self.text_y, self.rect_width, self.rect_height)
self.tft.blit_buffer(buffer, self.text_x, self.text_y, self.rect_width, self.rect_height)
def main():
try:
text_x = 10
text_y = int(170 / 2 - 32)
tft = tft_config.config(rotation=1)
jpg = 'pic_6.jpg'
text_list = [
"This Is Just To Say",
"I have eaten",
"the plums",
"that were in",
"the icebox",
"and which",
"you were probably",
"saving",
"for breakfast",
"Forgive me",
"they were delicious",
"so sweet",
"and so cold"]
tft.init()
gc.collect()
tft.jpg(jpg, 0, 0)
text_rect_1 = TextRect()
while True:
for i in text_list:
text_rect_1.rect(tft, vga1_bold_16x32, i, (text_x, text_y),
fg=st7789.YELLOW, bg=st7789.BLACK, alpha_text=30, alpha_rect=80)
tft.show()
time.sleep(0.5)
text_rect_1.erase(bg=jpg)
gc.collect()
except BaseException as err:
err_type = err.__class__.__name__
print('Err type:', err_type)
from sys import print_exception
print_exception(err)
finally:
tft.deinit()
print("tft deinit")
main()