Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. /* "simplest", example of simply enumerating the available devices with ESCAPI */
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <cstdlib>
  6. #include "escapi.h"
  7.  
  8. const int camWidth = 240;
  9. const int camHeight = 240;
  10. int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  11. int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  12.  
  13. void drawRect(HDC__ *context, int x, int y, int width, COLORREF color) {
  14.     for (int i = x; i < x+width; ++i){
  15.         if (i < 0) {
  16.             continue;
  17.         }
  18.         for (int j = y; j < y+width; ++j) {
  19.             if (j < 0) {
  20.                 continue;
  21.             }
  22.             SetPixel(context, i, j, color);
  23.         }
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     printf("Screen: %dx%d\n", screenWidth, screenHeight);
  30.     const auto console = GetConsoleWindow();
  31.     const auto context = GetDC(console);
  32.  
  33.     /* Initialize ESCAPI */
  34.  
  35.     int devices = setupESCAPI();
  36.  
  37.     if (devices == 0)
  38.     {
  39.         printf("ESCAPI initialization failure or no devices found.\n");
  40.         system("pause");
  41.         return -1;
  42.     }
  43.     struct SimpleCapParams capture;
  44.     capture.mWidth = camWidth;
  45.     capture.mHeight = camHeight;
  46.     capture.mTargetBuf = new int[camWidth * camHeight];
  47.  
  48.  
  49.     if (initCapture(0, &capture) == 0)
  50.     {
  51.         printf("Capture failed - device may already be in use.\n");
  52.         system("pause");
  53.         return -2;
  54.     }
  55.     int prevCursorX = 0;
  56.     int prevCursorY = 0;
  57.     int cursorEps = 10;
  58.     int maxRedValueEps = 20;
  59.     while (1) {
  60.         doCapture(0);
  61.         while (isCaptureDone(0) == 0);
  62.         int maxRedValue = 0;
  63.         int maxRedRow = 0;
  64.         int maxRedCol = 0;
  65.         for (int i = 0; i < camHeight; ++i) {
  66.             for (int j = 0; j < camWidth; ++j){
  67.            
  68.                 unsigned char red = (capture.mTargetBuf[i * camWidth + j] >> 16) & 255;
  69.                 unsigned char green = (capture.mTargetBuf[i * camWidth + j] >> 8) & 255;
  70.                 unsigned char blue = (capture.mTargetBuf[i * camWidth + j]) & 255;
  71.    
  72.                 int crRedValue = (red = green)+(red-blue);
  73.                 if (crRedValue > maxRedValue + maxRedValueEps) {
  74.                     maxRedValue = crRedValue;
  75.                     maxRedRow = i;
  76.                     maxRedCol = j;
  77.                 }
  78.                 //SetPixel(context, j, i, RGB(red, green, blue));
  79.             }
  80.         }
  81.         //(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
  82.         int cursorPosX = (maxRedCol - 0) * (float)(screenWidth + 100 + 100) / (float)(camWidth - 0) - 100;
  83.         int cursorPosY = (maxRedRow - 0) * (float)(screenHeight + 100 + 100) / (float)(camHeight - 0) - 100;
  84.         cursorPosX = screenWidth + 100 - cursorPosX;
  85.         if (abs(cursorPosX - prevCursorX) > cursorEps && abs(cursorPosY - prevCursorY) > cursorEps) {
  86.             //drawRect(context, maxRedCol - 10, maxRedRow - 10, 20, RGB(255, 0, 0));
  87.             SetCursorPos(cursorPosX, cursorPosY);
  88.             prevCursorX = cursorPosX;
  89.             prevCursorY = cursorPosY;
  90.         }
  91.     }
  92.     delete[] capture.mTargetBuf;
  93.     deinitCapture(0);
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement