From 3ae113013849e28ffd702b840670b4948a4d5d34 Mon Sep 17 00:00:00 2001 From: favoritas37 Date: Tue, 30 Jul 2013 19:57:45 +0300 Subject: [PATCH] extended the decodeImage function to support image scaling in case of unwanted too big image. --- source/CameraImageWrapper.cpp | 62 +++++++++-------------------------- source/CameraImageWrapper.h | 7 ++-- source/QZXing.h | 18 ++++++++-- source/qzxing.cpp | 31 ++++++++++++------ 4 files changed, 57 insertions(+), 61 deletions(-) diff --git a/source/CameraImageWrapper.cpp b/source/CameraImageWrapper.cpp index 90783b8..a415f2b 100644 --- a/source/CameraImageWrapper.cpp +++ b/source/CameraImageWrapper.cpp @@ -54,30 +54,27 @@ unsigned char* CameraImageWrapper::copyMatrix() const return newMatrix; } -bool CameraImageWrapper::setImage(QString fileName) +bool CameraImageWrapper::setImage(QString fileName, int maxWidth, int maxHeight) { bool isLoaded = image.load(fileName); if(!isLoaded) return false; - if(image.width() > QApplication::desktop()->width()) - image = image.scaled(QApplication::desktop()->width(), image.height(), Qt::IgnoreAspectRatio); + scale(maxWidth, maxHeight); - if(image.height() > QApplication::desktop()->height()) - image = image.scaled(image.width(), QApplication::desktop()->height(), Qt::IgnoreAspectRatio); return true; } -bool CameraImageWrapper::setImage(QImage newImage) +bool CameraImageWrapper::setImage(QImage newImage, int maxWidth, int maxHeight) { if(newImage.isNull()) return false; image = newImage.copy(); - if(image.width() > 640) - image = image.scaled(640, image.height(), Qt::KeepAspectRatio, isSmoothTransformationEnabled ? Qt::SmoothTransformation : Qt::FastTransformation); + scale(maxWidth, maxHeight); + return true; } @@ -94,8 +91,6 @@ QImage CameraImageWrapper::grayScaleImage(QImage::Format f) } return tmp; - - //return image.convertToFormat(f); } QImage CameraImageWrapper::getOriginalImage() @@ -103,43 +98,6 @@ QImage CameraImageWrapper::getOriginalImage() return image; } - -//unsigned char* CameraImageWrapper::getRow(int y, unsigned char* row) -//{ -// int width = getWidth(); - -// if (row == NULL) -// { -// row = new unsigned char[width]; -// pRow = row; -// } - -// for (int x = 0; x < width; x++) -// row[x] = getPixel(x,y); - -// return row; -//} - -//unsigned char* CameraImageWrapper::getMatrix() -//{ -// int width = getWidth(); -// int height = getHeight(); -// unsigned char* matrix = new unsigned char[width*height]; -// unsigned char* m = matrix; - -// for(int y=0; y CameraImageWrapper::getRow(int y, ArrayRef row) const { int width = getWidth(); @@ -182,3 +140,13 @@ void CameraImageWrapper::setSmoothTransformation(bool enable) { isSmoothTransformationEnabled = enable; } + +void CameraImageWrapper::scale(int maxWidth, int maxHeight) +{ + if((maxWidth != 1 || maxHeight != 1) && (image.width() > maxWidth || image.height() > maxHeight)) + image = image.scaled( + maxWidth != -1 ? maxWidth : image.width(), + maxHeight != -1 ? maxHeight : image.height(), + Qt::KeepAspectRatio, + isSmoothTransformationEnabled ? Qt::SmoothTransformation : Qt::FastTransformation); +} diff --git a/source/CameraImageWrapper.h b/source/CameraImageWrapper.h index 832628f..b5b5c54 100644 --- a/source/CameraImageWrapper.h +++ b/source/CameraImageWrapper.h @@ -24,8 +24,8 @@ public: /** * Set the source of the image. If it fails, returns false. */ - bool setImage(QString fileName); - bool setImage(QImage newImage); + bool setImage(QString fileName, int maxWidth=-1, int maxHeight=-1); + bool setImage(QImage newImage, int maxWidth=-1, int maxHeight=-1); QImage grayScaleImage(QImage::Format f); QImage getOriginalImage(); @@ -39,6 +39,9 @@ public: ArrayRef getMatrix() const; void setSmoothTransformation(bool enable); + +private: + void scale(int maxWidth, int maxHeight); private: QImage image; diff --git a/source/QZXing.h b/source/QZXing.h index af5a0ff..6b17035 100644 --- a/source/QZXing.h +++ b/source/QZXing.h @@ -69,15 +69,20 @@ public: public slots: /** * The decoding function. Will try to decode the given image based on the enabled decoders. + * If the image width is larger than maxWidth or image height is larger + * than maxHeight then the image will be scaled down. Either way, in case of scaling, the aspect + * ratio of the image will be kept. * + * The smoothTransformation flag determines whether the transformation will be smooth or fast. + * Smooth transformation provides better results but fast transformation is...faster. */ - QString decodeImage(QImage image); + QString decodeImage(QImage image, int maxWidth=-1, int maxHeight=-1, bool smoothTransformation = false); /** * The decoding function. Will try to decode the given image based on the enabled decoders. * The input image is read from a local image file. */ - QString decodeImageFromFile(QString imageFilePath); + QString decodeImageFromFile(QString imageFilePath, int maxWidth=-1, int maxHeight=-1, bool smoothTransformation = false); /** * The decoding function accessible from QML @@ -99,6 +104,10 @@ public slots: */ int getProcessTimeOfLastDecoding(); + /** + * Get the decoders that are enabled at the moment. + * Returns a uint which is a bitwise OR of DecoderFormat enumeration values. + */ uint getEnabledFormats() const; /** * Set the enabled decoders. @@ -118,6 +127,11 @@ private: DecoderFormatType enabledDecoders; QObject* imageHandler; int processingTime; + + /** + * If true, the decoding operation will take place at a different thread. + */ + bool isThreaded; }; #endif // QZXING_H diff --git a/source/qzxing.cpp b/source/qzxing.cpp index 5a74583..0cbfa45 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -95,22 +95,33 @@ void QZXing::setDecoder(const uint &hint) emit enabledFormatsChanged(); } -QString QZXing::decodeImage(QImage image) +QString QZXing::decodeImage(QImage image, int maxWidth, int maxHeight, bool smoothTransformation) { QTime t; t.start(); Ref res; emit decodingStarted(); - if(image.isNull()) - { - emit decodingFinished(false); - processingTime = -1; - return ""; - } + if(image.isNull()) + { + emit decodingFinished(false); + processingTime = -1; + return ""; + } try{ - Ref imageRef(new CameraImageWrapper(image)); + CameraImageWrapper* ciw; + + if(maxWidth > 0 || maxHeight > 0) + { + ciw = new CameraImageWrapper(); + ciw->setSmoothTransformation(smoothTransformation); + ciw->setImage(image, maxWidth, maxHeight); + } + else + ciw = new CameraImageWrapper(image); + + Ref imageRef(ciw); GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef); Ref bz (binz); @@ -134,12 +145,12 @@ QString QZXing::decodeImage(QImage image) } } -QString QZXing::decodeImageFromFile(QString imageFilePath) +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. - return decodeImage(QImage(imageFilePath)); + return decodeImage(QImage(imageFilePath), maxWidth, maxHeight, smoothTransformation); } QString QZXing::decodeImageQML(QObject *item)