粗浅的设计一个登录平台

代码

/**
 *
 *   file name:系统登陆以及主界面
 *   author   :eon4051@163.com
 *   date     :2024/05/14
 *   brief    :登录系统(无密码)
 *   note     :None
 *
 *   CopyRight (c) 2024    eon4051@163.com    All Right Reseverd
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "jpeglib.h"
#include <linux/input.h>
#include <string.h>

int x1, y1;
int x2, y2;
int x, y;
int state = 0;
int *lcd_mp;
struct input_event lcd_event; // 定义结构体变量

void *get_xy()
{

    // 打开触摸屏文件
    int lcd_fd2 = open("/dev/input/event0", O_RDWR);
    if (lcd_fd2 == -1)
        perror("lcd open error!!\n");
    while (1)
    {
        read(lcd_fd2, &lcd_event, sizeof(lcd_event));
        // printf("lcd_event.type = %d,lcd_event.code = %d,lcd_event.value = %d\n",lcd_event.type,lcd_event.code,lcd_event.value);
        if (lcd_event.type == 3)
        {
            if (lcd_event.code == 0)
                x = lcd_event.value * 800 / 1024;
            if (lcd_event.code == 1)
                y = lcd_event.value * 480 / 600;
        }
        if (lcd_event.type == 1 && lcd_event.code == BTN_TOUCH && lcd_event.value == 1) // 按下输出坐标
        {
            x1 = x;
            y1 = y;
            printf("x1: %d, y1: %d\n", x1, y1);
        }
        if (lcd_event.type == 1 && lcd_event.code == BTN_TOUCH && lcd_event.value == 0) // 松开输出坐标
        {
            x2 = x;
            y2 = y;
            printf("x2: %d, y2: %d\n\n", x2, y2);
            state = 1;
            break;
        }
    }
    close(lcd_fd2);
}

// 成功返回1 失败返回0
int show_jpg(char *filename, int start_x, int start_y)
{

    // 为JPEG对象分配空间并初始化
    struct jpeg_decompress_struct cinfo; // JPEG对象
    struct jpeg_error_mgr jerr;          // 错误处理对象

    /* More stuff */
    FILE *infile;          // 文件标识符
    unsigned char *buffer; // 缓冲区
    int row_stride;        // 单行字节大小

    cinfo.err = jpeg_std_error(&jerr); // 将错误处理对象绑定到JPEG对象上

    jpeg_create_decompress(&cinfo); // 初始化cinfo结构

    // 指定解压缩数据源
    if ((infile = fopen(filename, "rb")) == NULL)
    {
        fprintf(stderr, "can't open %s\n", filename);
        return 0;
    }

    jpeg_stdio_src(&cinfo, infile); // 指定读取

    // 获取文件信息
    (void)jpeg_read_header(&cinfo, TRUE);

    // 开始解压缩图像
    (void)jpeg_start_decompress(&cinfo);

    // 取出数据
    row_stride = cinfo.output_width * cinfo.output_components; // 计算一行的大小(宽度*色深)

    buffer = calloc(1, row_stride);

    int data = 0;

    while (cinfo.output_scanline < cinfo.output_height)
    {

        (void)jpeg_read_scanlines(&cinfo, &buffer, 1); // 从上到下,从左到右  RGB RGB RGB RGB

        for (int i = 0; i < cinfo.output_width; ++i) // 012 345
        {
            data |= buffer[3 * i] << 16;    // R
            data |= buffer[3 * i + 1] << 8; // G
            data |= buffer[3 * i + 2];      // B

            // 把像素点写入到LCD的指定位置
            lcd_mp[800 * start_y + start_x + 800 * (cinfo.output_scanline - 1) + i] = data;

            data = 0;
        }
    }

    // 解压缩完毕
    (void)jpeg_finish_decompress(&cinfo);

    // 释放资源
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);

    return 1;
}

int main()
{
    // 1.打开LCD   open
    int lcd_fd = open("/dev/fb0", O_RDWR);

    // 2.对LCD进行内存映射  mmap
    lcd_mp = (int *)mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);

    int i = 0;
    int flag = 0;
    // 开机动画
    char gif_path[128] = {0};
    for (int i = 0; i < 82; ++i)//替换成你的gif图片总数
    {
        sprintf(gif_path, "./gif/Frame%d.jpg", i); //将对应的路径写入gif_path中
        show_jpg(gif_path, 0, 0);                  //输出gif图片
        usleep(1000 * 10);                         // FPS = 100HZ
    }

    show_jpg("./pic/background.jpg", 0, 0); // 背景 图片大小800*480
    show_jpg("./pic/login.jpg", 230, 150);  // 登录 图片大小100*100
    show_jpg("./pic/exit.jpg", 530, 150);   // 退出 图片大小100*100

    while (1)
    {
        get_xy();

        if (flag == 0) // 处在登陆界面下
        {
            if (x2 > 230 && x2 < 330 && y2 < 250 && y2 > 150) // 登录 图片大小100*100
            {
                show_jpg("./pic/main.jpg", 0, 0);
                show_jpg("./pic/back.jpg", 700, 0);
                flag = 1;
            }

            if (x2 > 530 && x2 < 630 && y2 < 250 && y2 > 150) // 退出 图片大小100*100
            {
                show_jpg("./pic/exit2.jpg", 0, 0);//将界面刷为纯黑退出程序
                return 0;
            }
        }

        if (flag == 1) // 处在主界面下
        {
            if (x2 > 700 && x2 < 800 && y2 < 100 && y2 > 0) // 返回 图片大小100*100
            {
                show_jpg("./pic/background.jpg", 0, 0); // 背景 图片大小800*480
                show_jpg("./pic/login.jpg", 230, 150);  // 登录 图片大小100*100
                show_jpg("./pic/exit.jpg", 530, 150);   // 退出 图片大小100*100
                flag = 0;
            }
        }
    }

    return 0;
}
posted @ 2024-05-14 18:43  藍桉未遇釋槐鳥  阅读(45)  评论(0)    收藏  举报