Skip to content

Commit

Permalink
更新JQLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
188080501 committed Jul 25, 2017
1 parent 15473d0 commit 0c293ad
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/JQLibrary/JQQRCodeReader.pri
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INCLUDEPATH *= \
$$PWD/include/JQQRCodeReader/

# 定义JQQRCodeReader的版本
JQQRCODEREADER_VERSIONSTRING = 1.4
JQQRCODEREADER_VERSIONSTRING = 1.5

# 判断Qt版本,小于等于5.6就报错
lessThan( QT_MAJOR_VERSION, 5 ) | lessThan( QT_MINOR_VERSION, 7 ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/JQLibrary/JQQRCodeWriter.pri
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INCLUDEPATH *= \
$$PWD/include/JQQRCodeWriter/

# 定义JQQRCodeWriter的版本
JQQRCODEWRITER_VERSIONSTRING = 1.4
JQQRCODEWRITER_VERSIONSTRING = 1.5

# 判断Qt版本,小于等于5.6就报错
lessThan( QT_MAJOR_VERSION, 5 ) | lessThan( QT_MINOR_VERSION, 7 ) {
Expand Down
22 changes: 22 additions & 0 deletions lib/JQLibrary/include/JQQRCodeReader/JQQRCodeReaderForQml.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <QObject>
#include <QSharedPointer>
#include <QQmlContext>
#include <QQuickPaintedItem>
#include <QMutex>
#include <QPainter>

// JQLibrary import
#include "JQQRCodeReader.h"
Expand All @@ -36,6 +39,17 @@ class QThreadPool;
class QSemaphore;
class QQuickItem;
class QQuickItemGrabResult;
class QImage;

//class TestClass: public QQuickPaintedItem
//{
//public:
// void paint(QPainter *p);

// static TestClass *o;
// static QMutex mutex;
// static QImage image;
//};

class JQQRCodeReaderForQmlManage: public JQQRCodeReader
{
Expand All @@ -51,10 +65,18 @@ class JQQRCodeReaderForQmlManage: public JQQRCodeReader
public slots:
void analysisItem(QQuickItem *item);

private:
int getReference(QImage &image, const int &xStart, const int &yStart, const int &xEnd, const int &yEnd);

qreal avgReference(const qreal &referenceAvg, const qreal &currentReference);

void processImage(QImage &image, const int &xStart, const int &yStart, const int &xEnd, const int &yEnd, const qreal &offset);

private:
QSharedPointer< QThreadPool > threadPool_;
QSharedPointer< QSemaphore > semaphore_;
QSharedPointer< QQuickItemGrabResult > quickItemGrabResult_;
qreal correctionValue_ = 1.42;

// Property code start
private: int decodeQrCodeType_ = JQQRCodeReader::DecodeQrCodeType;
Expand Down
126 changes: 116 additions & 10 deletions lib/JQLibrary/src/JQQRCodeReader/JQQRCodeReaderForQml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ JQQRCodeReaderForQmlManage::JQQRCodeReaderForQmlManage():
threadPool_( new QThreadPool ),
semaphore_( new QSemaphore )
{
threadPool_->setMaxThreadCount( std::max( QThread::idealThreadCount() - 1, 2 ) );
threadPool_->setMaxThreadCount( std::max( QThread::idealThreadCount(), 3 ) );
semaphore_->release( threadPool_->maxThreadCount() );
}

Expand All @@ -41,14 +41,8 @@ JQQRCodeReaderForQmlManage::~JQQRCodeReaderForQmlManage()

void JQQRCodeReaderForQmlManage::analysisItem(QQuickItem *item)
{
static bool lastIsVideoOutput = false;

if ( lastIsVideoOutput && item->objectName().contains( "VideoOutput" ) ) { return; }

if ( !semaphore_->tryAcquire( 1 ) ) { return; }

lastIsVideoOutput = item->objectName().contains( "VideoOutput" );

auto result = item->grabToImage();

QSharedPointer< QMetaObject::Connection > connection( new QMetaObject::Connection );
Expand All @@ -60,7 +54,7 @@ void JQQRCodeReaderForQmlManage::analysisItem(QQuickItem *item)
{
QImage buf;

const auto cut = std::min( image.width(), image.height() ) * 0.1;
const auto cut = std::min( image.width(), image.height() ) * 0.28;

if ( image.width() > image.height() )
{
Expand All @@ -83,11 +77,123 @@ void JQQRCodeReaderForQmlManage::analysisItem(QQuickItem *item)

this->decodeImage( buf, this->decodeQrCodeType_ );

semaphore_->release( 1 );
int reference1 = this->getReference( buf, 0, 0, buf.width() / 2, buf.height() / 2 );
int reference2 = this->getReference( buf, buf.width() / 2, 0, buf.width(), buf.height() / 2 );
int reference3 = this->getReference( buf, 0, buf.height() / 2, buf.width() / 2, buf.height() );
int reference4 = this->getReference( buf, buf.width() / 2, buf.height() / 2, buf.width(), buf.height() );

double referenceAvg = ( reference1 + reference2 + reference3 + reference4 ) / 4;

this->processImage( buf, 0, 0, buf.width() / 2, buf.height() / 2, this->avgReference( referenceAvg, reference1 ) );
this->processImage( buf, buf.width() / 2, 0, buf.width(), buf.height() / 2, this->avgReference( referenceAvg, reference2 ) );
this->processImage( buf, 0, buf.height() / 2, buf.width() / 2, buf.height(), this->avgReference( referenceAvg, reference3 ) );
this->processImage( buf, buf.width() / 2, buf.height() / 2, buf.width(), buf.height(), this->avgReference( referenceAvg, reference4 ) );

buf = QImage();
this->decodeImage( buf, this->decodeQrCodeType_ );

// TestClass::o->mutex.lock();
// TestClass::o->image = buf;
// TestClass::o->mutex.unlock();
// QMetaObject::invokeMethod( TestClass::o, "update", Qt::QueuedConnection );

semaphore_->release( 1 );
} );

disconnect( *connection );
} );
}

int JQQRCodeReaderForQmlManage::getReference(QImage &image, const int &xStart, const int &yStart, const int &xEnd, const int &yEnd)
{
qint64 total = 0;

for ( auto y = yStart; y < yEnd; ++y )
{
for ( auto x = xStart; x < xEnd; ++x )
{
const auto &&color = image.pixelColor( x, y );
const auto &&value = color.red() + color.green() + color.blue();

total += value;
}
}

qint64 avg = total / ( ( xEnd - xStart ) * ( yEnd - yStart ) );
int reference = 0;

for ( auto y = yStart; y < yEnd; ++y )
{
for ( auto x = xStart; x < xEnd; ++x )
{
const auto &&color = image.pixelColor( x, y );
const auto &&value = color.red() + color.green() + color.blue();

if ( value > ( avg / correctionValue_ ) )
{
++reference;
}
}
}

return reference;
}

qreal JQQRCodeReaderForQmlManage::avgReference(const qreal &referenceAvg, const qreal &currentReference)
{
if ( ( currentReference / referenceAvg ) > 1.15 ) { return 0.08; }
if ( ( currentReference / referenceAvg ) > 1.1 ) { return 0.04; }
if ( ( currentReference / referenceAvg ) > 1.05 ) { return 0.02; }
if ( ( currentReference / referenceAvg ) < 0.95 ) { return -0.02; }
if ( ( currentReference / referenceAvg ) < 0.9 ) { return -0.04; }
if ( ( currentReference / referenceAvg ) < 0.85 ) { return -0.08; }
return 0;
}

void JQQRCodeReaderForQmlManage::processImage(QImage &image, const int &xStart, const int &yStart, const int &xEnd, const int &yEnd, const qreal &offset)
{
qint64 total = 0;

for ( auto y = yStart; y < yEnd; ++y )
{
for ( auto x = xStart; x < xEnd; ++x )
{
const auto &&color = image.pixelColor( x, y );
const auto &&value = color.red() + color.green() + color.blue();

total += value;
}
}

qint64 avg = total / ( ( xEnd - xStart ) * ( yEnd - yStart ) );

for ( auto y = yStart; y < yEnd; ++y )
{
for ( auto x = xStart; x < xEnd; ++x )
{
const auto &&color = image.pixelColor( x, y );
const auto &&value = color.red() + color.green() + color.blue();

if ( value > ( avg / ( correctionValue_ + offset ) ) )
{
image.setPixelColor( x, y, QColor( 255, 255, 255 ) );
}
else
{
image.setPixelColor( x, y, QColor( 0, 0, 0 ) );
}
}
}
}

//TestClass *TestClass::o;
//QMutex TestClass::mutex;
//QImage TestClass::image;

//void TestClass::paint(QPainter *p)
//{
// o = this;

// mutex.lock();
// p->drawImage( 0, 0, image );
// mutex.unlock();
//}

0 comments on commit 0c293ad

Please sign in to comment.