mirror of https://github.com/status-im/qzxing.git
extended the decodeImage function to support image scaling in case of unwanted too big image.
This commit is contained in:
parent
231ef2f2f2
commit
3ae1130138
|
@ -54,30 +54,27 @@ unsigned char* CameraImageWrapper::copyMatrix() const
|
||||||
return newMatrix;
|
return newMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CameraImageWrapper::setImage(QString fileName)
|
bool CameraImageWrapper::setImage(QString fileName, int maxWidth, int maxHeight)
|
||||||
{
|
{
|
||||||
bool isLoaded = image.load(fileName);
|
bool isLoaded = image.load(fileName);
|
||||||
|
|
||||||
if(!isLoaded)
|
if(!isLoaded)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if(image.width() > QApplication::desktop()->width())
|
scale(maxWidth, maxHeight);
|
||||||
image = image.scaled(QApplication::desktop()->width(), image.height(), Qt::IgnoreAspectRatio);
|
|
||||||
|
|
||||||
if(image.height() > QApplication::desktop()->height())
|
|
||||||
image = image.scaled(image.width(), QApplication::desktop()->height(), Qt::IgnoreAspectRatio);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CameraImageWrapper::setImage(QImage newImage)
|
bool CameraImageWrapper::setImage(QImage newImage, int maxWidth, int maxHeight)
|
||||||
{
|
{
|
||||||
if(newImage.isNull())
|
if(newImage.isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
image = newImage.copy();
|
image = newImage.copy();
|
||||||
|
|
||||||
if(image.width() > 640)
|
scale(maxWidth, maxHeight);
|
||||||
image = image.scaled(640, image.height(), Qt::KeepAspectRatio, isSmoothTransformationEnabled ? Qt::SmoothTransformation : Qt::FastTransformation);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,8 +91,6 @@ QImage CameraImageWrapper::grayScaleImage(QImage::Format f)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
|
|
||||||
//return image.convertToFormat(f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage CameraImageWrapper::getOriginalImage()
|
QImage CameraImageWrapper::getOriginalImage()
|
||||||
|
@ -103,43 +98,6 @@ QImage CameraImageWrapper::getOriginalImage()
|
||||||
return image;
|
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
|
ArrayRef<char> CameraImageWrapper::getRow(int y, ArrayRef<char> row) const
|
||||||
{
|
{
|
||||||
int width = getWidth();
|
int width = getWidth();
|
||||||
|
@ -182,3 +140,13 @@ void CameraImageWrapper::setSmoothTransformation(bool enable)
|
||||||
{
|
{
|
||||||
isSmoothTransformationEnabled = 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);
|
||||||
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ public:
|
||||||
/**
|
/**
|
||||||
* Set the source of the image. If it fails, returns false.
|
* Set the source of the image. If it fails, returns false.
|
||||||
*/
|
*/
|
||||||
bool setImage(QString fileName);
|
bool setImage(QString fileName, int maxWidth=-1, int maxHeight=-1);
|
||||||
bool setImage(QImage newImage);
|
bool setImage(QImage newImage, int maxWidth=-1, int maxHeight=-1);
|
||||||
|
|
||||||
QImage grayScaleImage(QImage::Format f);
|
QImage grayScaleImage(QImage::Format f);
|
||||||
QImage getOriginalImage();
|
QImage getOriginalImage();
|
||||||
|
@ -40,6 +40,9 @@ public:
|
||||||
|
|
||||||
void setSmoothTransformation(bool enable);
|
void setSmoothTransformation(bool enable);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void scale(int maxWidth, int maxHeight);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QImage image;
|
QImage image;
|
||||||
unsigned char* pRow;
|
unsigned char* pRow;
|
||||||
|
|
|
@ -69,15 +69,20 @@ public:
|
||||||
public slots:
|
public slots:
|
||||||
/**
|
/**
|
||||||
* The decoding function. Will try to decode the given image based on the enabled decoders.
|
* 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 decoding function. Will try to decode the given image based on the enabled decoders.
|
||||||
* The input image is read from a local image file.
|
* 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
|
* The decoding function accessible from QML
|
||||||
|
@ -99,6 +104,10 @@ public slots:
|
||||||
*/
|
*/
|
||||||
int getProcessTimeOfLastDecoding();
|
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;
|
uint getEnabledFormats() const;
|
||||||
/**
|
/**
|
||||||
* Set the enabled decoders.
|
* Set the enabled decoders.
|
||||||
|
@ -118,6 +127,11 @@ private:
|
||||||
DecoderFormatType enabledDecoders;
|
DecoderFormatType enabledDecoders;
|
||||||
QObject* imageHandler;
|
QObject* imageHandler;
|
||||||
int processingTime;
|
int processingTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If true, the decoding operation will take place at a different thread.
|
||||||
|
*/
|
||||||
|
bool isThreaded;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QZXING_H
|
#endif // QZXING_H
|
||||||
|
|
|
@ -95,22 +95,33 @@ void QZXing::setDecoder(const uint &hint)
|
||||||
emit enabledFormatsChanged();
|
emit enabledFormatsChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QZXing::decodeImage(QImage image)
|
QString QZXing::decodeImage(QImage image, int maxWidth, int maxHeight, bool smoothTransformation)
|
||||||
{
|
{
|
||||||
QTime t;
|
QTime t;
|
||||||
t.start();
|
t.start();
|
||||||
Ref<Result> res;
|
Ref<Result> res;
|
||||||
emit decodingStarted();
|
emit decodingStarted();
|
||||||
|
|
||||||
if(image.isNull())
|
if(image.isNull())
|
||||||
{
|
{
|
||||||
emit decodingFinished(false);
|
emit decodingFinished(false);
|
||||||
processingTime = -1;
|
processingTime = -1;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
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);
|
GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef);
|
||||||
|
|
||||||
Ref<Binarizer> bz (binz);
|
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
|
//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
|
//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.
|
// 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)
|
QString QZXing::decodeImageQML(QObject *item)
|
||||||
|
|
Loading…
Reference in New Issue