Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* "simplest", example of simply enumerating the available devices with ESCAPI */
- #include <stdio.h>
- #include <iostream>
- #include <windows.h>
- #include <cstdlib>
- #include "escapi.h"
- const int camWidth = 240;
- const int camHeight = 240;
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
- void drawRect(HDC__ *context, int x, int y, int width, COLORREF color) {
- for (int i = x; i < x+width; ++i){
- if (i < 0) {
- continue;
- }
- for (int j = y; j < y+width; ++j) {
- if (j < 0) {
- continue;
- }
- SetPixel(context, i, j, color);
- }
- }
- }
- int main()
- {
- printf("Screen: %dx%d\n", screenWidth, screenHeight);
- const auto console = GetConsoleWindow();
- const auto context = GetDC(console);
- /* Initialize ESCAPI */
- int devices = setupESCAPI();
- if (devices == 0)
- {
- printf("ESCAPI initialization failure or no devices found.\n");
- system("pause");
- return -1;
- }
- struct SimpleCapParams capture;
- capture.mWidth = camWidth;
- capture.mHeight = camHeight;
- capture.mTargetBuf = new int[camWidth * camHeight];
- if (initCapture(0, &capture) == 0)
- {
- printf("Capture failed - device may already be in use.\n");
- system("pause");
- return -2;
- }
- int prevCursorX = 0;
- int prevCursorY = 0;
- int cursorEps = 10;
- int maxRedValueEps = 20;
- while (1) {
- doCapture(0);
- while (isCaptureDone(0) == 0);
- int maxRedValue = 0;
- int maxRedRow = 0;
- int maxRedCol = 0;
- for (int i = 0; i < camHeight; ++i) {
- for (int j = 0; j < camWidth; ++j){
- unsigned char red = (capture.mTargetBuf[i * camWidth + j] >> 16) & 255;
- unsigned char green = (capture.mTargetBuf[i * camWidth + j] >> 8) & 255;
- unsigned char blue = (capture.mTargetBuf[i * camWidth + j]) & 255;
- int crRedValue = (red-green)+(red-blue);
- if (crRedValue > maxRedValue + maxRedValueEps) {
- maxRedValue = crRedValue;
- maxRedRow = i;
- maxRedCol = j;
- }
- //SetPixel(context, j, i, RGB(red, green, blue));
- }
- }
- //(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
- //drawRect(context, maxRedCol - 10, maxRedRow - 10, 20, RGB(255, 0, 0));
- int cursorPosX = (maxRedCol - 0) * (float)(screenWidth + 100 + 100) / (float)(camWidth - 0) - 100;
- int cursorPosY = (maxRedRow - 0) * (float)(screenHeight + 100 + 100) / (float)(camHeight - 0) - 100;
- cursorPosX = screenWidth + 100 - cursorPosX;
- if (abs(cursorPosX - prevCursorX) > cursorEps && abs(cursorPosY - prevCursorY) > cursorEps) {
- SetCursorPos(cursorPosX, cursorPosY);
- prevCursorX = cursorPosX;
- prevCursorY = cursorPosY;
- }
- }
- delete[] capture.mTargetBuf;
- deinitCapture(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement