opencv c 1-9-3

ASCII数字打印机

建立一个500x500大小的白色图像,并在此图像上创建一个ASCII数字打印机。可以在电脑上输人数字或字符,并在一个50×30的方块中显示数字。当输入字符时,字符左

到右逐个显示,每个字符的颜色都是随机的。当字符到达图像边缘时,另起一行从头开始显

示。当输入回车时,另起一行显示。当输入退格键时,删除上次输入的字符。

#include <iostream>
#include <opencv.hpp>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#pragma comment(lib, "opencv_world420d.lib")

using namespace cv;
using namespace std;
Mat m(500, 500, CV_8UC3, Scalar(255, 255, 255));

int main(void) {
    int s1;
    char s2;
    string str;
    int i = 0, j = 0;
    srand((int)time(0));
    while (1) {
        imshow("text3-9-3", m);
        s1 = waitKey(0);
        if ((s1>=48&&s1<=57) || (s1 >= 65 && s1 <= 90) || (s1 >= 97 && s1 <= 122)) {
            s2 = char(s1);
            stringstream stream;
            int b, g, r;
            b = rand() % 100;
            g = rand() % 200;
            r = rand() % 300;
            stream << s2;
            str = stream.str();
            putText(m, str, Point(50 * (i++), 30 * (j+1)), FONT_HERSHEY_COMPLEX_SMALL, 1, (b, g, r), 1);
            if (10 == i) {
                j++;
                i = 0;
            }
        } else if (8 == s1) {//退格
            if (0==i&&0==j) {
                continue;
            }
        if (0==i) {
            j--;
            i = 9;
        } else {
                i--;
            }
            Mat bs1(50, 30, CV_8UC3, Scalar(255, 255, 255));
            Rect bs2(50 * i, 30 * j, bs1.cols, bs1.rows);
            bs1.copyTo(m(bs2));
        } else if (13 == s1) {//回车
            i = 0;
            j++;
        }
    }
    return 0;
}

 

posted on 2022-06-05 20:52  ting-1112  阅读(70)  评论(0编辑  收藏  举报