mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2024-11-25 23:14:18 +03:00
Convert screenshot to bitarray
This commit is contained in:
parent
b41cade4fa
commit
2777c0c9d3
@ -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")
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
35
screenshot.h
35
screenshot.h
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -55,6 +55,9 @@ lupdate_only {
|
||||
qml/slot/*.qml
|
||||
}
|
||||
|
||||
HEADERS += screenshot.h
|
||||
|
||||
DISTFILES += \
|
||||
py/yubikey.py \
|
||||
py/* \
|
||||
py/qr/* \
|
||||
resources/icons/yubioath.icns
|
||||
|
Loading…
Reference in New Issue
Block a user