diff --git a/source/QZXing.h b/source/QZXing.h index 0700f1f..573de7d 100644 --- a/source/QZXing.h +++ b/source/QZXing.h @@ -87,18 +87,35 @@ public slots: QString decodeImageFromFile(QString imageFilePath, int maxWidth=-1, int maxHeight=-1, bool smoothTransformation = false); /** - * The decoding function accessible from QML - */ + * The decoding function accessible from QML. (Suggested for Qt 4.x) + */ QString decodeImageQML(QObject *item); /** - * The decoding function accessible from QML. Able to set the decoding - * of a portion of the image. - */ + * The decoding function accessible from QML. Able to set the decoding + * of a portion of the image. (Suggested for Qt 4.x) + */ QString decodeSubImageQML(QObject* item, const double offsetX = 0 , const double offsetY = 0, const double width = 0, const double height = 0); + /** + * The decoding function accessible from QML. (Suggested for Qt 5.x) + * Can be used to decode image from the Camera element preview by providing + * the following string: image://camera/preview_1 + */ + QString decodeImageQML(const QUrl &imageUrl); + + /** + * The decoding function accessible from QML. Able to set the decoding + * of a portion of the image. + * Can be used to decode image from the Camera element preview by providing + * the following string: image://camera/preview_1 + * (Suggested for Qt 5.x) + */ + QString decodeSubImageQML(const QUrl &imageUrl, + const double offsetX = 0, const double offsetY = 0, + const double width = 0, const double height = 0); /** * Get the prossecing time in millisecond of the last decode operation. * Added mainly as a statistic measure. diff --git a/source/qzxing.cpp b/source/qzxing.cpp index 74ab634..de79dc8 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -135,17 +135,17 @@ QString QZXing::decodeImage(QImage image, int maxWidth, int maxHeight, bool smoo } catch(zxing::Exception& e) { - emit decodingFinished(false); - processingTime = -1; - return ""; + emit decodingFinished(false); + processingTime = -1; + return ""; } } QString QZXing::decodeImageFromFile(QString imageFilePath, int maxWidth, int maxHeight, bool smoothTransformation) { - //used to have a check if this image exists - //but was removed because if the image file path doesn't point to a valid image - // then the QImage::isNull will return true and the decoding will fail eitherway. + //used to have a check if this image exists + //but was removed because if the image file path doesn't point to a valid image + // then the QImage::isNull will return true and the decoding will fail eitherway. return decodeImage(QImage(imageFilePath), maxWidth, maxHeight, smoothTransformation); } @@ -169,6 +169,29 @@ QString QZXing::decodeSubImageQML(QObject* item, return decodeImage(img); } +QString QZXing::decodeImageQML(const QUrl &imageUrl) +{ + return decodeSubImageQML(imageUrl); +} +QString QZXing::decodeSubImageQML(const QUrl &imageUrl, + const double offsetX, const double offsetY, + const double width, const double height) +{ + QString imagePath = imageUrl.path(); + imagePath = imagePath.trimmed(); + QFile file(imagePath); + if (!file.exists()) { + qDebug() << "[decodeSubImageQML()] The file" << file.fileName() << "does not exist."; + emit decodingFinished(false); + return ""; + } + QImage img(imageUrl.path()); + if(!(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)) { + img = img.copy(offsetX, offsetY, width, height); + } + return decodeImage(img); +} + int QZXing::getProcessTimeOfLastDecoding() { return processingTime;