当前位置: 首页 > news >正文

《K230 从熟悉到...》屏幕手画板

《K230 从熟悉到...》屏幕手绘板

  • 添加功能
  • 带保存功能的手画板

《庐山派 K230 从熟悉到...》屏幕手绘板

https://wiki.lckfb.com/zh-hans/lushan-pi-k230/media/touch.html
在这里插入图片描述

添加功能

参考下面的格式去添加自己想要的功能
在这里插入图片描述
在这里插入图片描述

带保存功能的手画板

import time, os, gc, sys, urandom
from media.display import *
from media.media import *
from machine import TOUCH

try:
    # LCD
    DISPLAY_MODE = "LCD"

    if DISPLAY_MODE == "LCD":
        # 3.1寸屏幕模式
        DISPLAY_WIDTH = 800
        DISPLAY_HEIGHT = 480

    # 根据模式初始化显示器
    if DISPLAY_MODE == "LCD":
        Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=False)

    width = DISPLAY_WIDTH
    height = DISPLAY_HEIGHT

    # 初始化媒体管理器
    MediaManager.init()

   
    # 创建绘制的图像
    img = image.Image(width, height, image.RGB565)
    img.clear()

    # 设置默认画笔颜色和大小
    current_color = (0, 255, 0)  # 默认绿色
    brush_size = 10  # 默认画笔大小

    # 定义画布中的按钮区域
    clear_button_area = (width - 130, 0, 130, 50)  # 清除按钮区域(右上角)
    color_button_area = (0, 0, 130, 50)  # 颜色选择按钮区域(左上角)
    save_button_area = (0, 60, 130, 50)  # 保存按钮区域(左上角下方)

    # 实例化 TOUCH 设备 0
    tp = TOUCH(0)

    last_point = None  # 记录上一个触摸点

    def draw_button(x, y, w, h, text, bg_color, text_color):
        """绘制圆角矩形按钮"""
        # 绘制圆角矩形
        
        img.draw_rectangle(x, y, w, h, color=bg_color, fill=True)
        # 绘制文本
        img.draw_string_advanced(x + 40, y + 8, 30, text, color=text_color, scale=2)

    def draw_clear_button():
        """绘制清除按钮"""
        draw_button(clear_button_area[0], clear_button_area[1], clear_button_area[2], clear_button_area[3],
                   "清除", (255, 0, 0), (255, 255, 255))

    def draw_color_buttons():
        """绘制颜色选择按钮"""
        draw_button(color_button_area[0], color_button_area[1], color_button_area[2], color_button_area[3],
                   "随机", (255, 255, 0), (0, 0, 0))
        # 显示当前颜色
        img.draw_circle(color_button_area[0] + 170, 25, 20, color=current_color, thickness=3, fill=True)

    def draw_save_button():
        """绘制保存按钮"""
        draw_button(save_button_area[0], save_button_area[1], save_button_area[2], save_button_area[3],
                   "保存", (0, 128, 255), (255, 255, 255))

    def select_color(x, y):
        """选择随机颜色"""
        global current_color
        if (color_button_area[0] <= x <= color_button_area[0] + color_button_area[2] and
                color_button_area[1] <= y <= color_button_area[1] + color_button_area[3]):
            current_color = (urandom.getrandbits(8), urandom.getrandbits(8), urandom.getrandbits(8))
            print(f"select_color to {current_color}")

    def check_clear_button(x, y):
        """检查是否点击了清除按钮"""
        if (clear_button_area[0] <= x <= clear_button_area[0] + clear_button_area[2] and
                clear_button_area[1] <= y <= clear_button_area[1] + clear_button_area[3]):
            img.clear()  # 清除画布

    def check_save_button(x, y):
        """检查是否点击了保存按钮"""
        if (save_button_area[0] <= x <= save_button_area[0] + save_button_area[2] and
                save_button_area[1] <= y <= save_button_area[1] + save_button_area[3]):
            save_image()  # 保存图像

    def save_image():
        """保存当前图像到文件"""
        filename = f'/data/xianyujiang/1.jpg'  # 保存路径
        img.save(filename)
        print(f"Image saved to {filename}")

    def draw_line_between_points(last_point, current_point):
        """在两个触摸点之间绘制连线,插入中间点以平滑移动"""
        if last_point is not None:
            dx = current_point.x - last_point.x
            dy = current_point.y - last_point.y
            distance = (dx ** 2 + dy ** 2) ** 0.5

            if distance > 30:
                return

            min_distance = 10  # 插值的最小距离
            if distance > min_distance:
                steps = int(distance // min_distance)
                for i in range(1, steps + 1):
                    new_x = last_point.x + i * dx / (steps + 1)
                    new_y = last_point.y + i * dy / (steps + 1)
                    img.draw_circle(int(new_x), int(new_y), brush_size, color=current_color, thickness=3, fill=True)

        img.draw_circle(current_point.x, current_point.y, brush_size, color=current_color, thickness=3, fill=True)

    while True:

        os.exitpoint()

        # 只读取 1 个触摸点数据
        p = tp.read(1)

        if p != ():
            for idx, point in enumerate(p, start=1):
                select_color(point.x, point.y)  # 选择颜色
                check_clear_button(point.x, point.y)  # 检查清除按钮
                check_save_button(point.x, point.y)  # 检查保存按钮
                draw_line_between_points(last_point, point)  # 绘制线条
                last_point = point

        # 绘制按钮和其他元素
        draw_clear_button()
        draw_color_buttons()
        draw_save_button()

        # 显示绘制结果
        Display.show_image(img)

        time.sleep_ms(1)
except KeyboardInterrupt as e:
    print(f"user stop")
except BaseException as e:
    print(f"Exception '{e}'")
finally:
    # 销毁 display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # 释放媒体缓冲区
    MediaManager.deinit()

在这里插入图片描述

相关文章:

  • AI 原生 IDE Trae 深度体验:SSHremote 功能如何重新定义远程开发与云原生部署
  • 项目-苍穹外卖(九) 店铺营业状态设置+HttpClient
  • Node.js模块:使用 Bull 打造高效的任务队列系统
  • SQL Server数据库简介及应用
  • 网络空间安全专业发展历程及开设院校
  • 【SpringBatch】03步骤对象 (Step)控制与执行流程设计
  • 学习记录 6 pointnet复现
  • 【深度学习量化交易18】盘前盘后回调机制设计与实现——基于miniQMT的量化交易回测系统开发实记
  • 美团Leaf分布式ID生成器使用教程:号段模式与Snowflake模式详解
  • 《UNIX网络编程卷1:套接字联网API》第2章 传输层:TCP、UDP和SCTP
  • [入门]NUC13配置Ubuntu20.04详细步骤
  • 三分钟掌握视频分辨率修改 | 在 Rust 中优雅地使用 FFmpeg
  • v-on=“$listeners“ 这个写法已经废弃了,如进行代替
  • 指令系统1(数据传输指令)
  • 电子工程师转战汽车OEM主机厂之路
  • (保姆级教程)CAN总线—如何使用CANoe(VN1640)的Scaner功能测量样件的波特率
  • 用于 RGB-D 显著目标检测的点感知交互和 CNN 诱导的细化网络(问题)
  • Metasploit Framework(MSF)使用教程与命令详解
  • bluetooth与hciconfig的区别
  • 使用bat批量获取WORD中包含对应字符的段落,段落使用回车换行
  • 陕西省安康市汉阴县县长陈永乐已任汉阴县委书记
  • 制造四十余年血腥冲突后,库尔德工人党为何自行解散?
  • 秦洪看盘|预期改善,或迎来新的增量资金
  • 四姑娘山一游客疑因高反身亡,镇卫生院:送到时已很严重
  • 重庆三峡学院回应“85万元中标设备,网购价不到300元”:已着手解决
  • 重庆荣昌出圈背后:把网络流量变成经济发展的增量