-
Notifications
You must be signed in to change notification settings - Fork 1
/
zxingimagesource.cpp
99 lines (78 loc) · 2.2 KB
/
zxingimagesource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "zxingimagesource.h"
#include <QColor>
#include "zxing/core/src/zxing/ZXing.h"
#include "zxing/core/src/zxing/LuminanceSource.h"
#include "zxing/core/src/zxing/common/Array.h"
ZXingImageSource::ZXingImageSource(const QImage &sourceImage) : zxing::LuminanceSource(sourceImage.width(), sourceImage.height())
{
m_image = sourceImage.copy();
}
int ZXingImageSource::getWidth() const
{
return m_image.width();
}
int ZXingImageSource::getHeight() const
{
return m_image.height();
}
unsigned char ZXingImageSource::getPixel(int x, int y) const
{
QRgb pixel = m_image.pixel(x,y);
return qGray(pixel);
}
unsigned char* ZXingImageSource::copyMatrix() const
{
unsigned char* newMatrix = (unsigned char*)malloc(m_image.width()*m_image.height()*sizeof(char));
int cnt = 0;
for(int i=0; i<m_image.width(); i++)
{
for(int j=0; j<m_image.height(); j++)
{
newMatrix[cnt++] = getPixel(i,j);
}
}
return newMatrix;
}
QImage ZXingImageSource::grayScaleImage(QImage::Format f)
{
QImage tmp(m_image.width(), m_image.height(), f);
for(int i=0; i<m_image.width(); i++)
{
for(int j=0; j<m_image.height(); j++)
{
int pix = (int)getPixel(i,j);
tmp.setPixel(i,j, qRgb(pix ,pix,pix));
}
}
return tmp;
}
QImage ZXingImageSource::getOriginalImage()
{
return m_image;
}
zxing::ArrayRef<char> ZXingImageSource::getRow(int y, zxing::ArrayRef<char> row) const
{
int width = getWidth();
if (row->size() != width)
row.reset(zxing::ArrayRef<char>(width));
for (int x = 0; x < width; x++)
row[x] = getPixel(x,y);
return row;
}
zxing::ArrayRef<char> ZXingImageSource::getMatrix() const
{
int width = getWidth();
int height = getHeight();
char* matrix = new char[width * height];
char* m = matrix;
for(int y=0; y<height; ++y) {
zxing::ArrayRef<char> tmpRow;
tmpRow = getRow(y, zxing::ArrayRef<char>(width));
memcpy(m, &tmpRow->values()[0], width);
m += width * sizeof(unsigned char);
}
zxing::ArrayRef<char> arr = zxing::ArrayRef<char>(matrix, width * height);
if(matrix)
delete matrix;
return arr;
}