Convert screenshot to bitarray

This commit is contained in:
Dag Heyman 2017-02-14 13:12:24 +01:00
parent b41cade4fa
commit 2777c0c9d3
No known key found for this signature in database
GPG Key ID: 06FC004369E7D338
4 changed files with 39 additions and 9 deletions

View File

@ -16,7 +16,7 @@ Dialog {
Layout.columnSpan: 2
text: qsTr("Scan a QR code")
Layout.fillWidth: true
onClicked: console.log(ScreenShot.capture());
onClicked: device.parseQr(ScreenShot.capture());
}
Label {
text: qsTr("Name")

View File

@ -194,4 +194,10 @@ Python {
function deleteCredential(credential) {
do_call('yubikey.controller.delete_credential', [credential, passwordKey])
}
function parseQr(image){
do_call('yubikey.controller.parse_qr', [image], function(res){
console.log(res)
})
}
}

View File

@ -2,21 +2,42 @@
#define SCREENSHOT_H
#include <QObject>
#include <QtWidgets>
#include <QVariant>
class ScreenShot: public QObject
{
Q_OBJECT
public:
explicit ScreenShot () : QObject() {}
// Take a screenshot and return a base64 encoded string
Q_INVOKABLE QString capture(){
// Take a screenshot, convert it to a bitarray and return it with some metadata
Q_INVOKABLE QVariantMap capture() {
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap screenShotPixMap = screen->grabWindow(0);
QByteArray byteArray;
QBuffer buffer(&byteArray);
screenShotPixMap.save(&buffer, "PNG");
return QString(byteArray.toBase64());
QImage image = screenShotPixMap.toImage();
// Monochrome, no dither
image = image.convertToFormat(QImage::Format_Mono, Qt::ThresholdDither);
// Iterate over all pixels
QByteArray imageArray(4 + 4 + (image.width() * image.height()), 0);
for (int row = 0; row < image.height(); ++row) {
for (int col = 0; col < image.width(); ++col) {
QRgb px = image.pixel(col, row);
// If black 1 else 0
if (px == 0xFF000000) {
imageArray.append((char) 1);
} else {
imageArray.append((char) 0);
}
}
}
QVariantMap map;
map.insert("width", image.width());
map.insert("height", image.height());
map.insert("data", QString(imageArray.toBase64()));
return map;
}
};

View File

@ -55,6 +55,9 @@ lupdate_only {
qml/slot/*.qml
}
HEADERS += screenshot.h
DISTFILES += \
py/yubikey.py \
py/* \
py/qr/* \
resources/icons/yubioath.icns