mirror of https://github.com/status-im/qzxing.git
Refactoring on CameraImageWrapper on the conversion of image to grayscale
This commit is contained in:
parent
7c24420fdc
commit
07cc5c9c6b
|
@ -3,81 +3,78 @@
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QDesktopWidget>
|
#include <QDesktopWidget>
|
||||||
|
|
||||||
CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0)
|
CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0), image(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraImageWrapper::CameraImageWrapper(const QImage &sourceImage) : LuminanceSource(sourceImage.width(), sourceImage.height())
|
CameraImageWrapper::CameraImageWrapper(const QImage &sourceImage) : LuminanceSource(sourceImage.width(), sourceImage.height())
|
||||||
{
|
{
|
||||||
image = sourceImage.copy();
|
image = grayScaleImage( &sourceImage );
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : LuminanceSource(otherInstance.getWidth(), otherInstance.getHeight())
|
CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : LuminanceSource(otherInstance.getWidth(), otherInstance.getHeight())
|
||||||
{
|
{
|
||||||
image = otherInstance.getOriginalImage().copy();
|
image = new QImage(otherInstance.getOriginalImage());
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraImageWrapper::~CameraImageWrapper()
|
CameraImageWrapper::~CameraImageWrapper()
|
||||||
{
|
{
|
||||||
|
if(image)
|
||||||
|
delete image;
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraImageWrapper *CameraImageWrapper::Factory(const QImage &sourceImage, int maxWidth, int maxHeight, bool smoothTransformation)
|
CameraImageWrapper *CameraImageWrapper::Factory(const QImage &sourceImage, int maxWidth, int maxHeight, bool smoothTransformation)
|
||||||
{
|
{
|
||||||
QImage image;
|
|
||||||
if((maxWidth != 1 || maxHeight != 1) && (sourceImage.width() > maxWidth || sourceImage.height() > maxHeight))
|
if((maxWidth != 1 || maxHeight != 1) && (sourceImage.width() > maxWidth || sourceImage.height() > maxHeight))
|
||||||
|
{
|
||||||
|
QImage image;
|
||||||
image = sourceImage.scaled(
|
image = sourceImage.scaled(
|
||||||
maxWidth != -1 ? maxWidth : sourceImage.width(),
|
maxWidth != -1 ? maxWidth : sourceImage.width(),
|
||||||
maxHeight != -1 ? maxHeight : sourceImage.height(),
|
maxHeight != -1 ? maxHeight : sourceImage.height(),
|
||||||
Qt::KeepAspectRatio,
|
Qt::KeepAspectRatio,
|
||||||
smoothTransformation ? Qt::SmoothTransformation : Qt::FastTransformation);
|
smoothTransformation ? Qt::SmoothTransformation : Qt::FastTransformation);
|
||||||
else
|
|
||||||
image = sourceImage;
|
|
||||||
|
|
||||||
return new CameraImageWrapper(image);
|
return new CameraImageWrapper(image);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return new CameraImageWrapper(sourceImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CameraImageWrapper::getWidth() const
|
int CameraImageWrapper::getWidth() const
|
||||||
{
|
{
|
||||||
return image.width();
|
return image->width();
|
||||||
}
|
}
|
||||||
|
|
||||||
int CameraImageWrapper::getHeight() const
|
int CameraImageWrapper::getHeight() const
|
||||||
{
|
{
|
||||||
return image.height();
|
return image->height();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char CameraImageWrapper::getPixel(int x, int y) const
|
unsigned char CameraImageWrapper::getPixel(int x, int y) const
|
||||||
{
|
{
|
||||||
QRgb pixel = image.pixel(x,y);
|
return image->pixel(x,y);
|
||||||
|
|
||||||
return qGray(pixel);//((qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* CameraImageWrapper::copyMatrix() const
|
unsigned char* CameraImageWrapper::copyMatrix() const
|
||||||
{
|
{
|
||||||
unsigned char* newMatrix = (unsigned char*)malloc(image.width()*image.height()*sizeof(unsigned char));
|
unsigned char* newMatrix = (unsigned char*)malloc(image->width() * image->height() * sizeof(unsigned char));
|
||||||
|
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
for(int i=0; i<image.width(); i++)
|
for(int i=0; i<image->width(); i++)
|
||||||
{
|
for(int j=0; j<image->height(); j++)
|
||||||
for(int j=0; j<image.height(); j++)
|
|
||||||
{
|
|
||||||
newMatrix[cnt++] = getPixel(i,j);
|
newMatrix[cnt++] = getPixel(i,j);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newMatrix;
|
return newMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage CameraImageWrapper::grayScaleImage(QImage::Format f)
|
QImage* CameraImageWrapper::grayScaleImage(const QImage *origin)
|
||||||
{
|
{
|
||||||
QImage tmp(image.width(), image.height(), f);
|
QImage *tmp = new QImage(origin->width(), origin->height(), QImage::Format_Grayscale8);
|
||||||
for(int i=0; i<image.width(); i++)
|
for(int i=0; i<origin->width(); i++)
|
||||||
{
|
{
|
||||||
for(int j=0; j<image.height(); j++)
|
for(int j=0; j<origin->height(); j++)
|
||||||
{
|
{
|
||||||
int pix = (int)getPixel(i,j);
|
int pix = qGray(origin->pixel(QPoint(i,j)));
|
||||||
tmp.setPixel(i,j, qRgb(pix ,pix,pix));
|
tmp->setPixel(i,j, qRgb(pix ,pix,pix));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +83,7 @@ QImage CameraImageWrapper::grayScaleImage(QImage::Format f)
|
||||||
|
|
||||||
QImage CameraImageWrapper::getOriginalImage()
|
QImage CameraImageWrapper::getOriginalImage()
|
||||||
{
|
{
|
||||||
return image;
|
return *image;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayRef<char> CameraImageWrapper::getRow(int y, ArrayRef<char> row) const
|
ArrayRef<char> CameraImageWrapper::getRow(int y, ArrayRef<char> row) const
|
||||||
|
@ -109,14 +106,15 @@ ArrayRef<char> CameraImageWrapper::getMatrix() const
|
||||||
char* matrix = new char[width*height];
|
char* matrix = new char[width*height];
|
||||||
char* m = matrix;
|
char* m = matrix;
|
||||||
|
|
||||||
|
ArrayRef<char> tmpRow(0);
|
||||||
|
|
||||||
for(int y=0; y<height; y++)
|
for(int y=0; y<height; y++)
|
||||||
{
|
{
|
||||||
ArrayRef<char> tmpRow;
|
tmpRow = getRow(y, tmpRow);
|
||||||
tmpRow = getRow(y, ArrayRef<char>(width));
|
|
||||||
#if __cplusplus > 199711L
|
#if __cplusplus > 199711L
|
||||||
memcpy(m, tmpRow->values().data(), width);
|
memcpy(m, tmpRow->values().data(), width);
|
||||||
#else
|
#else
|
||||||
memcpy(m, &tmpRow->values()[0], width);
|
memcpy(m, &tmpRow[0], width);
|
||||||
#endif
|
#endif
|
||||||
m += width * sizeof(unsigned char);
|
m += width * sizeof(unsigned char);
|
||||||
}
|
}
|
||||||
|
@ -129,3 +127,42 @@ ArrayRef<char> CameraImageWrapper::getMatrix() const
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QImage *CameraImageWrapper::sharpen(const QImage *origin)
|
||||||
|
{
|
||||||
|
QImage * newImage = new QImage(* origin);
|
||||||
|
|
||||||
|
int kernel [3][3]= {{0,-1,0},
|
||||||
|
{-1,5,-1},
|
||||||
|
{0,-1,0}};
|
||||||
|
int kernelSize = 3;
|
||||||
|
int sumKernel = 1;
|
||||||
|
int r,g,b;
|
||||||
|
QColor color;
|
||||||
|
|
||||||
|
for(int x=kernelSize/2; x<newImage->width()-(kernelSize/2); x++){
|
||||||
|
for(int y=kernelSize/2; y<newImage->height()-(kernelSize/2); y++){
|
||||||
|
|
||||||
|
r = 0;
|
||||||
|
g = 0;
|
||||||
|
b = 0;
|
||||||
|
|
||||||
|
for(int i = -kernelSize/2; i<= kernelSize/2; i++){
|
||||||
|
for(int j = -kernelSize/2; j<= kernelSize/2; j++){
|
||||||
|
color = QColor(origin->pixel(x+i, y+j));
|
||||||
|
r += color.red()*kernel[kernelSize/2+i][kernelSize/2+j];
|
||||||
|
g += color.green()*kernel[kernelSize/2+i][kernelSize/2+j];
|
||||||
|
b += color.blue()*kernel[kernelSize/2+i][kernelSize/2+j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r = qBound(0, r/sumKernel, 255);
|
||||||
|
g = qBound(0, g/sumKernel, 255);
|
||||||
|
b = qBound(0, b/sumKernel, 255);
|
||||||
|
|
||||||
|
newImage->setPixel(x,y, qRgb(r,g,b));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newImage;
|
||||||
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
unsigned char getPixel(int x, int y) const;
|
unsigned char getPixel(int x, int y) const;
|
||||||
unsigned char* copyMatrix() const;
|
unsigned char* copyMatrix() const;
|
||||||
|
|
||||||
QImage grayScaleImage(QImage::Format f);
|
QImage* grayScaleImage(const QImage *origin);
|
||||||
QImage getOriginalImage();
|
QImage getOriginalImage();
|
||||||
|
|
||||||
// Callers take ownership of the returned memory and must call delete [] on it themselves.
|
// Callers take ownership of the returned memory and must call delete [] on it themselves.
|
||||||
|
@ -31,7 +31,9 @@ public:
|
||||||
ArrayRef<char> getMatrix() const;
|
ArrayRef<char> getMatrix() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QImage image;
|
QImage* sharpen(const QImage *origin);
|
||||||
|
|
||||||
|
QImage* image;
|
||||||
unsigned char* pRow;
|
unsigned char* pRow;
|
||||||
unsigned char* pMatrix;
|
unsigned char* pMatrix;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue