diff --git a/README.md b/README.md index a1eccf7..544d931 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,7 @@ Or use the encoding function with the optional custom settings that are passed l | border | true, false | image has border (white 1px) | | correctionLevel | L, M, Q, H | the error correction level | | format | qrcode | the encode formatter. Currently only QR Code. | +| transparent | true, false | whether the black pixels are transparent | the size of the image can be adjusted by using the Image.sourceWidth and Image.sourceHeight properties of Image QML element. diff --git a/examples/BarcodeEncoder/main.qml b/examples/BarcodeEncoder/main.qml index d674e00..ef1b613 100644 --- a/examples/BarcodeEncoder/main.qml +++ b/examples/BarcodeEncoder/main.qml @@ -1,6 +1,7 @@ import QtQuick 2.0 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 +import QtQuick.Dialogs 1.3 ApplicationWindow { id: mainWindow @@ -11,10 +12,19 @@ ApplicationWindow { minimumWidth: formatGroupBox.width + errorCorrectionlevelGroupBox.width + borderStatus.width + + transparentStatus.width + 5 * advancedOptions.spacing property bool isAdvancedOptionsEnabled: advancedSwitch.position; + property string advancedUrl: "image://QZXing/encode/" + inputField.text + + "?correctionLevel=" + errorCorrectionlevelCombo.currentText + + "&format=" + formatCombo.currentText + + "&border=" + (borderStatus.checkState !== Qt.Unchecked) + + "&transparent=" + (transparentStatus.checkState !== Qt.Unchecked) + + property string normalUrl: "image://QZXing/encode/" + inputField.text + ColumnLayout { id: mainLayout anchors { @@ -77,11 +87,53 @@ ApplicationWindow { } } - Switch { - id: borderStatus - text: "Border" - anchors.verticalCenter: parent.verticalCenter + Rectangle{ + height: parent.height + width: borderStatus.width + colorPickerButton.width + 10 + + Column { + Row { + CheckBox { + id: borderStatus + text: "Border" + } + + CheckBox { + id: colorPickerButton + text: "Color" + + background: Rectangle { + color: barcodeRectangle.color + } + + onCheckStateChanged: colorDialog.visible = true + } + } + + CheckBox { + id: transparentStatus + text: "Transparent" + } + } } + +// Rectangle { +// height: colorPick.height + 6 +// width: colorPick.width + 6 +// border.width: 1 +// border.color: "#bdbebf" +// Button { +// id: colorPick +// anchors.centerIn: parent +// width: borderStatus.height /2 +// height: borderStatus.height /2 +// background: Rectangle { +// color: barcodeRectangle.color +// } + +// onClicked: colorDialog.visible = true +// } +// } } Rectangle { @@ -91,6 +143,7 @@ ApplicationWindow { border.width: 1 border.color: "#bdbebf" clip: true + color: "white" property int imageWidth: Math.min(height, width) * 0.7; @@ -101,19 +154,23 @@ ApplicationWindow { sourceSize.height: barcodeRectangle.imageWidth source: mainLayout.getImageRequestString() - //cache: false; + cache: false; + } + } + + ColorDialog{ + id: colorDialog + title: "Please choose a color" + onAccepted: { + barcodeRectangle.color = colorDialog.color } } function getImageRequestString() { - if(mainWindow.isAdvancedOptionsEnabled) { - return "image://QZXing/encode/" + inputField.text + - "?correctionLevel=" + errorCorrectionlevelCombo.currentText + - "&format=" + formatCombo.currentText + - "&border=" + borderStatus.position; - } + if(mainWindow.isAdvancedOptionsEnabled) + return advancedUrl; else - return "image://QZXing/encode/" + inputField.text; + return normalUrl; } } } diff --git a/src/QZXing.cpp b/src/QZXing.cpp index 2b30b6e..1798906 100644 --- a/src/QZXing.cpp +++ b/src/QZXing.cpp @@ -580,13 +580,15 @@ QImage QZXing::encodeData(const QString& data, const EncoderFormat encoderFormat, const QSize encoderImageSize, const EncodeErrorCorrectionLevel errorCorrectionLevel, - const bool border) + const bool border, + const bool transparent) { return encodeData(data, QZXingEncoderConfig(encoderFormat, encoderImageSize, errorCorrectionLevel, - border)); + border, + transparent)); } QImage QZXing::encodeData(const QString &data, const QZXingEncoderConfig &encoderConfig) @@ -612,8 +614,8 @@ QImage QZXing::encodeData(const QString &data, const QZXingEncoderConfig &encode const std::vector< std::vector >& bytes = bytesRef->getArray(); const int width = int(bytesRef->getWidth()) + (encoderConfig.border ? 2 : 0); const int height = int(bytesRef->getHeight()) + (encoderConfig.border ? 2 : 0); - const QRgb black = qRgb(0, 0, 0); - const QRgb white = qRgb(255, 255, 255); + const QRgb black = qRgba(0, 0, 0, encoderConfig.transparent ? 0 : 255); + const QRgb white = qRgba(255, 255, 255, 255); image = QImage(width, height, QImage::Format_ARGB32); image.fill(white); diff --git a/src/QZXing.h b/src/QZXing.h index 72bf0a5..880571b 100644 --- a/src/QZXing.h +++ b/src/QZXing.h @@ -194,7 +194,8 @@ public slots: const EncoderFormat encoderFormat = EncoderFormat_QR_CODE, const QSize encoderImageSize = QSize(240, 240), const EncodeErrorCorrectionLevel errorCorrectionLevel = EncodeErrorCorrectionLevel_L, - const bool border = false); + const bool border = false, + const bool transparent = false); #endif // ENABLE_ENCODER_GENERIC /** @@ -250,13 +251,15 @@ typedef struct QZXingEncoderConfig QSize imageSize; QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel; bool border; + bool transparent; QZXingEncoderConfig(const QZXing::EncoderFormat encoderFormat_ = QZXing::EncoderFormat_QR_CODE, const QSize encoderImageSize_ = QSize(240, 240), const QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel_ = QZXing::EncodeErrorCorrectionLevel_L, - const bool border_ = false) : + const bool border_ = false, + const bool transparent_ = false) : format(encoderFormat_), imageSize(encoderImageSize_), - errorCorrectionLevel(errorCorrectionLevel_), border(border_) {} + errorCorrectionLevel(errorCorrectionLevel_), border(border_), transparent(transparent_) {} } QZXingEncoderConfig; #endif // ENABLE_ENCODER_GENERIC diff --git a/src/QZXingImageProvider.cpp b/src/QZXingImageProvider.cpp index b721839..35130f2 100644 --- a/src/QZXingImageProvider.cpp +++ b/src/QZXingImageProvider.cpp @@ -29,8 +29,9 @@ QImage QZXingImageProvider::requestImage(const QString &id, QSize *size, const Q QZXing::EncoderFormat format = QZXing::EncoderFormat_QR_CODE; QZXing::EncodeErrorCorrectionLevel correctionLevel = QZXing::EncodeErrorCorrectionLevel_L; bool border = false; + bool transparent = false; - int customSettingsIndex = id.lastIndexOf(QRegularExpression("\\?(correctionLevel|format|border)=")); + int customSettingsIndex = id.lastIndexOf(QRegularExpression("\\?(correctionLevel|format|border|transparent)=")); if(customSettingsIndex >= 0) { int startOfDataIndex = slashIndex + 1; @@ -58,15 +59,20 @@ QImage QZXingImageProvider::requestImage(const QString &id, QSize *size, const Q else if(correctionLevelString == "L") correctionLevel = QZXing::EncodeErrorCorrectionLevel_L; - if (optionQuery.hasQueryItem("border")) { - border = QVariant(optionQuery.queryItemValue("border")).toBool(); - } - } else + if (optionQuery.hasQueryItem("border")) + border = optionQuery.queryItemValue("border") == "true"; + + if (optionQuery.hasQueryItem("transparent")) + transparent = optionQuery.queryItemValue("transparent") == "true"; + } + else { data = id.mid(slashIndex + 1); } - QImage result = QZXing::encodeData(data, format, requestedSize, correctionLevel, border); + QZXingEncoderConfig encoderConfig(format, requestedSize, correctionLevel, border, transparent); + + QImage result = QZXing::encodeData(data, encoderConfig); *size = result.size(); return result; }