extended the decodeImage function to support image scaling in case of unwanted too big image.

This commit is contained in:
favoritas37 2013-07-30 19:57:45 +03:00
parent 231ef2f2f2
commit 3ae1130138
4 changed files with 57 additions and 61 deletions

View File

@ -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<height; y++)
// {
// unsigned char* tmpRow;
// memcpy(m, tmpRow = getRow(y, NULL), width);
// m += width * sizeof(unsigned char);
// delete tmpRow;
// }
// pMatrix = matrix;
// return matrix;
//}
ArrayRef<char> CameraImageWrapper::getRow(int y, ArrayRef<char> 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);
}

View File

@ -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<char> getMatrix() const;
void setSmoothTransformation(bool enable);
private:
void scale(int maxWidth, int maxHeight);
private:
QImage image;

View File

@ -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

View File

@ -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<Result> res;
emit decodingStarted();
if(image.isNull())
{
emit decodingFinished(false);
processingTime = -1;
return "";
}
if(image.isNull())
{
emit decodingFinished(false);
processingTime = -1;
return "";
}
try{
Ref<LuminanceSource> 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<LuminanceSource> imageRef(ciw);
GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef);
Ref<Binarizer> 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)