• EVE111


    Beep.h

    #pragma once
    #include 
    #include 
    #define qdo 262 
    #define qre 294
    #define qmi 330     //q前缀为低音,1后缀为高音,s前缀为半音阶 
    #define qfa 349
    #define qso 392
    #define qla 440
    #define qsi 494
    #define do 523
    #define re 578
    #define mi 659
    #define fa 698
    #define so 784
    #define la 880
    #define si 988
    #define do1 1046
    #define re1 1175
    #define mi1 1318
    #define fa1 1480
    #define so1 1568
    #define la1 1760
    #define si1 1976
    #define sqdo 277
    #define sqre 311
    #define sqfa 370
    #define sqso 415
    #define sqla 466
    #define sdo 554
    #define sre 622
    #define sfa 740
    #define sso 831
    #define sla 932
    #define sdo1 1046
    #define sre1 1245
    #define sfa1 1480
    #define sso1 1661
    #define sla1 1865
    
    
    #define pai  400
    #define ban  200
    #define ting  128
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    Screenshot.h

    #pragma once
    #include 
    #include 
    
    class Screenshot
    {
    public:
        Screenshot();
        double static getZoom();
        cv::Mat getScreenshot();
        cv::Mat getScreenshot(int x, int y, int width, int height);
    
    private:
        int m_width;
        int m_height;
        HDC m_screenDC;
        HDC m_compatibleDC;
        HBITMAP m_hBitmap;
        LPVOID m_screenshotData = nullptr;
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Sreenshot.cpp

    #include "Screenshot.h"
    using cv::Mat;
    
    Screenshot::Screenshot()
    {
        double zoom = getZoom();
        m_width = GetSystemMetrics(SM_CXSCREEN) * zoom;
        m_height = GetSystemMetrics(SM_CYSCREEN) * zoom;
        m_screenshotData = new char[m_width * m_height * 4];
        memset(m_screenshotData, 0, m_width);
    
        // 获取屏幕 DC
        m_screenDC = GetDC(NULL);
        m_compatibleDC = CreateCompatibleDC(m_screenDC);
    
        // 创建位图
        m_hBitmap = CreateCompatibleBitmap(m_screenDC, m_width, m_height);
        SelectObject(m_compatibleDC, m_hBitmap);
    }
    
    /* 获取整个屏幕的截图 */
    Mat Screenshot::getScreenshot()
    {
        // 得到位图的数据
        BitBlt(m_compatibleDC, 0, 0, m_width, m_height, m_screenDC, 0, 0, SRCCOPY);
        GetBitmapBits(m_hBitmap, m_width * m_height * 4, m_screenshotData);
    
        // 创建图像
        Mat screenshot(m_height, m_width, CV_8UC4, m_screenshotData);
    
        return screenshot;
    }
    
    /** @brief 获取指定范围的屏幕截图
     * @param x 图像左上角的 X 坐标
     * @param y 图像左上角的 Y 坐标
     * @param width 图像宽度
     * @param height 图像高度
     */
    Mat Screenshot::getScreenshot(int x, int y, int width, int height)
    {
        Mat screenshot = getScreenshot();
        return screenshot(cv::Rect(x, y, width, height));
    }
    
    /* 获取屏幕缩放值 */
    double Screenshot::getZoom()
    {
        // 获取窗口当前显示的监视器
        HWND hWnd = GetDesktopWindow();
        HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
    
        // 获取监视器逻辑宽度
        MONITORINFOEX monitorInfo;
        monitorInfo.cbSize = sizeof(monitorInfo);
        GetMonitorInfo(hMonitor, &monitorInfo);
        int cxLogical = (monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left);
    
        // 获取监视器物理宽度
        DEVMODE dm;
        dm.dmSize = sizeof(dm);
        dm.dmDriverExtra = 0;
        EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &dm);
        int cxPhysical = dm.dmPelsWidth;
    
        return cxPhysical * 1.0 / cxLogical;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    main.cpp

    #include "Screenshot.h"
    #include "Beep.h"
    #include
    using namespace std;
    using namespace cv;
    
    
    // 发出警报
    void warning()
    {
        while (true)
        {
            Beep(la, pai);
        }
    }
    
    // 显示截取的图像
    void show(Mat img_wite,Mat img_red)
    {
    
        imshow("src", img_wite);
        waitKey();
        system("pause");
    
        imshow("src", img_red);
        waitKey();
        system("pause");
    }
    
    // 图像锐化
    void alter(Mat img_wite)
    {
    
        for (int j = 0; j < img_wite.rows; j++)
        {
            for (int i = 0; i < img_wite.cols; i++)
            {
    
                //printf("%d %d %d \n", img_wite.at(j, i)[0], img_wite.at(j, i)[1], img_wite.at(j, i)[2]);
    
    
                // 如果rgb值中其中一个值大于255,则将其值重新赋值为其他俩值的平均数,因为本图像只有灰度
                if (img_wite.at<cv::Vec3b>(j, i)[0] == 255)
                {
                    img_wite.at<cv::Vec3b>(j, i)[0] = (img_wite.at<cv::Vec3b>(j, i)[1] + img_wite.at<cv::Vec3b>(j, i)[2]) / 2;
                }
                if (img_wite.at<cv::Vec3b>(j, i)[1] == 255)
                {
                    img_wite.at<cv::Vec3b>(j, i)[1] = (img_wite.at<cv::Vec3b>(j, i)[0] + img_wite.at<cv::Vec3b>(j, i)[2]) / 2;
                }
                if (img_wite.at<cv::Vec3b>(j, i)[2] == 255)
                {
                    img_wite.at<cv::Vec3b>(j, i)[2] = (img_wite.at<cv::Vec3b>(j, i)[1] + img_wite.at<cv::Vec3b>(j, i)[0]) / 2;
                }
    
    
    
                // 然后来判断他是深色背景还是浅色字体
    
                if (img_wite.at<cv::Vec3b>(j, i)[0] +
                    img_wite.at<cv::Vec3b>(j, i)[1] + img_wite.at<cv::Vec3b>(j, i)[2] > 250
                    )
                {
                    img_wite.at<cv::Vec3b>(j, i)[0] = 255;
                    img_wite.at<cv::Vec3b>(j, i)[1] = 255;
                    img_wite.at<cv::Vec3b>(j, i)[2] = 255;
                }
                else
                {
                    img_wite.at<cv::Vec3b>(j, i)[0] = 0;
                    img_wite.at<cv::Vec3b>(j, i)[1] = 0;
                    img_wite.at<cv::Vec3b>(j, i)[2] = 0;
                }
                //printf("%d", img_wite.at(j, i)[2] == 255 ? 1 : 0);
            }
            //printf("\n");
        }
    }
    
    
    // 俩Mat进行比较,判断是否相同
    bool comp(Mat mat1, Mat mat2)
    {
    
        int diff = 0;
        for (int j = 0; j < mat1.rows; j++)
        {
            for (int i = 0; i < mat1.cols; i++)
            {
                if (!((int)mat1.at<cv::Vec3b>(j, i)[0] == (int)mat2.at<cv::Vec3b>(j, i)[0] &&
                    (int)mat1.at<cv::Vec3b>(j, i)[1] == (int)mat2.at<cv::Vec3b>(j, i)[1] &&
                    (int)mat1.at<cv::Vec3b>(j, i)[2] == (int)mat2.at<cv::Vec3b>(j, i)[2]))
                {
                    diff++;
                  /*  cout << (int)mat1.at(j, i)[0] << (int)mat2.at(j, i)[0] << endl;
                    cout << (int)mat1.at(j, i)[1] << (int)mat2.at(j, i)[1] << endl;
                    cout << (int)mat1.at(j, i)[2] << (int)mat2.at(j, i)[2] << endl;*/
                    //return false;
                }
            }
        }
        if (diff>100)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    
    int main()
    {
        system("pause");
        Screenshot screenshot1;
        Screenshot screenshot;
        // 截取全屏
        //Mat img = screenshot.getScreenshot();
    
        // 截取红白都没有的状态
        Mat src_wite;
        Mat src_red;
        src_wite = screenshot1.getScreenshot(194, 740, 30, 40);
        src_red = screenshot1.getScreenshot(338, 740, 30, 40);
        alter(src_red);
        alter(src_wite);
    
        Mat img_wite;
        Mat img_red;
        while (true)
        {
            Sleep(1000);
            img_wite = screenshot.getScreenshot(194, 740, 30, 40);
            img_red = screenshot.getScreenshot(338, 740, 30, 40);
            // 图像锐化
            alter(img_red);
            alter(img_wite);
    
            // 如果俩都没发生变化,返回的都是true
            if (comp(img_red, src_red)==false || comp(img_wite, src_wite)==false)
            {
                cout << "开始警报" << endl;
                warning();
            }
            else
            {
                cout << "安全" << endl;
            }
        }
    
    
    
    
        // 临时显示截取的图片
        //show(img_wite, img_red);
    
        // 释放内存
        img_wite.release();
        img_red.release();
    
    
        src_wite.release();
        src_red.release();
    
        // 将mat保存
        // imwrite("screenshot_part.jpg", img_);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
  • 相关阅读:
    【元宇宙欧米说】探讨Web3领域的文化交汇点
    关于冒泡排序算法的实验
    java毕业设计设计师品牌服装租赁网站源码+lw文档+mybatis+系统+mysql数据库+调试
    Zen of Python(python之禅)
    每日三题 7.4
    图像处理笔记3-Canny边缘检测算法与原理
    开关、电机、断路器、电热偶、电表接线图大全
    面试题:分布式锁用了 Redis 的什么数据结构
    搭建一个干净整洁的vue前端开发环境
    wps阶梯表格怎么做?wps阶梯表格制作教程
  • 原文地址:https://blog.csdn.net/qq_46664420/article/details/134027795