This commit is contained in:
Alexis Delhaie
2020-12-02 19:17:30 +01:00
commit 0f3aeb0b3f
7 changed files with 657 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.pro.user

38
Cam2Ascii.pro Normal file
View File

@@ -0,0 +1,38 @@
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# remove possible other optimization flags
QMAKE_CXXFLAGS_RELEASE -= -O
QMAKE_CXXFLAGS_RELEASE -= -O1
QMAKE_CXXFLAGS_RELEASE *= -O2
RC_ICONS = icon.ico
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

BIN
icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

11
main.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

162
mainwindow.cpp Normal file
View File

@@ -0,0 +1,162 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
cameras_availables = QCameraInfo::availableCameras();
for (const QCameraInfo &cameraInfo : cameras_availables)
{
ui->devicesBox->addItem(cameraInfo.description());
}
ui->devicesBox->setEditText(QCameraInfo::defaultCamera().description());
revert();
init(QCameraInfo::defaultCamera());
QObject::connect(ui->startAndStopButton, &QPushButton::clicked, this, &MainWindow::start_or_stop);
QObject::connect(ui->devicesBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::config_changed);
QObject::connect(ui->sourceWidth, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::config_changed);
QObject::connect(ui->sourceHeight, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::config_changed);
QObject::connect(ui->asciiWidth, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::config_changed);
QObject::connect(ui->asciiHeight, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::config_changed);
QObject::connect(ui->charEdit, &QLineEdit::textEdited, this, &MainWindow::config_changed);
QObject::connect(ui->disableProcess, &QCheckBox::clicked, this, &MainWindow::config_changed);
QObject::connect(ui->applyButton, &QPushButton::clicked, this, &MainWindow::apply);
QObject::connect(ui->revertButton, &QPushButton::clicked, this, &MainWindow::revert);
QObject::connect(this, &MainWindow::image_processed, [&, this] (QPixmap img) {
img = img.scaled(ascii_width, ascii_height);
ui->screen->setPixmap(img);
fps += 1;
});
QObject::connect(this, &MainWindow::tick_framerate, [&, this] () {
ui->framerate->display(fps);
fps = 0;
});
}
void MainWindow::init(QCameraInfo cam)
{
camera = new QCamera(cam);
image_capture = new QCameraImageCapture(camera);
image_capture->setCaptureDestination(QCameraImageCapture::CaptureDestination::CaptureToBuffer);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->start();
QObject::connect(image_capture, &QCameraImageCapture::imageCaptured, [&, this] (int id, QImage img) {
img = img.scaled(source_width, source_height);
if (!disable_process) {
convert(img);
} else {
ui->screen->setPixmap(QPixmap::fromImage(img));
fps += 1;
}
});
}
MainWindow::~MainWindow()
{
delete_all();
delete ui;
}
void MainWindow::delete_all() {
if (thread_running) {
start_or_stop();
}
camera->unlock();
camera->stop();
delete camera;
delete image_capture;
}
void MainWindow::convert(QImage in) {
int length = chars.length();
QString res = "";
for (int y = 0; y < in.height(); y+=4) {
for (int x = 1; x < in.width(); x+=2) {
QColor pix1(in.pixel(x, y));
int avg1 = (pix1.red() + pix1.green() + pix1.blue()) / 3;
QColor pix2(in.pixel(x-1, y));
int avg2 = (pix2.red() + pix2.green() + pix2.blue()) / 3;
double avg = floor(avg1 + avg2 / 2);
int char_index = floor(((256.0 - avg) / 256.0) * (length - 1));
if (char_index >= length) char_index = length - 1;
if (char_index < 0) char_index = 0;
res += chars[char_index];
}
res += '\n';
}
QBitmap bitmap(1920,1080);
bitmap.fill(Qt::white);
QPainter painter(&bitmap);
QFont serifFont("Consolas", 12);
painter.setFont(serifFont);
painter.setPen(Qt::black);
const QRect rectangle = QRect(0, 0, 1920, 1080);
QRect boundingRect;
painter.drawText(rectangle, 0, res, &boundingRect);
painter.save();
QPixmap pix(bitmap);
emit image_processed(pix);
}
void MainWindow::start_or_stop() {
if (thread_running) {
thread_running = false;
capture_thread->wait();
framerate_count->wait();
delete capture_thread;
delete framerate_count;
ui->startAndStopButton->setText("Start");
} else {
ui->startAndStopButton->setText("Stop");
thread_running = true;
capture_thread = QThread::create([&, this]() {
while(thread_running) {
image_capture->capture();
QThread::msleep(refresh_timer);
}
});
framerate_count = QThread::create([&, this]() {
while(thread_running) {
QThread::sleep(1);
emit tick_framerate();
}
});
capture_thread->start();
framerate_count->start();
}
}
void MainWindow::apply() {
ui->applyButton->setEnabled(false);
ui->revertButton->setEnabled(false);
delete_all();
QCameraInfo cam = cameras_availables[ui->devicesBox->currentIndex()];
init(cam);
source_width = ui->sourceWidth->value();
source_height = ui->sourceHeight->value();
ascii_width = ui->asciiWidth->value();
ascii_height = ui->asciiHeight->value();
chars = ui->charEdit->text();
disable_process = ui->disableProcess->checkState();
refresh_timer = ui->refreshTimer->value();
}
void MainWindow::revert() {
ui->sourceWidth->setValue(source_width);
ui->sourceHeight->setValue(source_height);
ui->asciiWidth->setValue(ascii_width);
ui->asciiHeight->setValue(ascii_height);
ui->charEdit->setText(chars);
ui->refreshTimer->setValue(refresh_timer);
ui->applyButton->setEnabled(false);
ui->revertButton->setEnabled(false);
}
void MainWindow::config_changed() {
ui->applyButton->setEnabled(true);
ui->revertButton->setEnabled(true);
}

66
mainwindow.h Normal file
View File

@@ -0,0 +1,66 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QBuffer>
#include <QThread>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QMap>
#include <QBitmap>
#include <QPainter>
#include <QStaticText>
#include <QCameraInfo>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void start_or_stop();
void apply();
void revert();
void config_changed();
signals:
void image_processed(QPixmap img);
void tick_framerate();
private:
Ui::MainWindow *ui;
QCamera *camera;
QCameraImageCapture *image_capture;
QThread *capture_thread;
QThread *framerate_count;
QCameraInfo selected_camera;
bool thread_running = false;
QString chars = " .~#☻░▒";
int source_width = 480;
int source_height = 360;
int ascii_width = 771;
int ascii_height = 531;
int refresh_timer = 25;
bool disable_process = false;
int fps = 0;
QList<QCameraInfo> cameras_availables;
void init(QCameraInfo cam);
void convert(QImage in);
void delete_all();
};
#endif // MAINWINDOW_H

379
mainwindow.ui Normal file
View File

@@ -0,0 +1,379 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1007</width>
<height>561</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>800</width>
<height>561</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1007</width>
<height>561</height>
</size>
</property>
<property name="windowTitle">
<string>Cam2Ascii (Demo)</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="screen">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>771</width>
<height>531</height>
</rect>
</property>
<property name="text">
<string>Currently stopped</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>800</x>
<y>10</y>
<width>20</width>
<height>541</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QComboBox" name="devicesBox">
<property name="geometry">
<rect>
<x>820</x>
<y>30</y>
<width>181</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>820</x>
<y>10</y>
<width>47</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Device</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>820</x>
<y>70</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Source scale</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>879</x>
<y>94</y>
<width>47</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Width</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>880</x>
<y>120</y>
<width>47</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Height</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QSpinBox" name="sourceWidth">
<property name="geometry">
<rect>
<x>931</x>
<y>90</y>
<width>61</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>3840</number>
</property>
</widget>
<widget class="QSpinBox" name="sourceHeight">
<property name="geometry">
<rect>
<x>931</x>
<y>116</y>
<width>61</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>2160</number>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>820</x>
<y>180</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>ASCII Video output</string>
</property>
</widget>
<widget class="QSpinBox" name="asciiHeight">
<property name="geometry">
<rect>
<x>932</x>
<y>232</y>
<width>61</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>2160</number>
</property>
</widget>
<widget class="QSpinBox" name="asciiWidth">
<property name="geometry">
<rect>
<x>932</x>
<y>206</y>
<width>61</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>3840</number>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>881</x>
<y>236</y>
<width>47</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Height</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_8">
<property name="geometry">
<rect>
<x>880</x>
<y>210</y>
<width>47</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Width</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="charEdit">
<property name="geometry">
<rect>
<x>827</x>
<y>300</y>
<width>161</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>830</x>
<y>280</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Characters</string>
</property>
</widget>
<widget class="QPushButton" name="applyButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>915</x>
<y>520</y>
<width>80</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Apply</string>
</property>
</widget>
<widget class="QPushButton" name="revertButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>827</x>
<y>520</y>
<width>80</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Revert</string>
</property>
</widget>
<widget class="QPushButton" name="startAndStopButton">
<property name="geometry">
<rect>
<x>830</x>
<y>488</y>
<width>161</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Start</string>
</property>
</widget>
<widget class="Line" name="line_2">
<property name="geometry">
<rect>
<x>820</x>
<y>58</y>
<width>181</width>
<height>16</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QCheckBox" name="disableProcess">
<property name="geometry">
<rect>
<x>830</x>
<y>330</y>
<width>161</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Disable process</string>
</property>
</widget>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>815</x>
<y>151</y>
<width>111</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Refresh timer (ms)</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QSpinBox" name="refreshTimer">
<property name="geometry">
<rect>
<x>930</x>
<y>150</y>
<width>61</width>
<height>22</height>
</rect>
</property>
<property name="minimum">
<number>25</number>
</property>
<property name="maximum">
<number>5000</number>
</property>
</widget>
<widget class="QLCDNumber" name="framerate">
<property name="geometry">
<rect>
<x>923</x>
<y>440</y>
<width>71</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_11">
<property name="geometry">
<rect>
<x>880</x>
<y>420</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Framerate</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>