From 51346e19d620e8033e37451ae01268e8d75fe892 Mon Sep 17 00:00:00 2001 From: favoritas37 Date: Mon, 20 Mar 2017 15:11:40 +0200 Subject: [PATCH] Fixed processing of RGB32 images. Fix inverted y directly at the construction of the image. Fixes issue #27. --- examples/QZXingLive/native.cpp | 7 ++++--- src/QZXingFilter.cpp | 22 +++++++++++++++++----- src/QZXingFilter.h | 1 + 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/QZXingLive/native.cpp b/examples/QZXingLive/native.cpp index cea7ee0..60ff2d8 100644 --- a/examples/QZXingLive/native.cpp +++ b/examples/QZXingLive/native.cpp @@ -1,8 +1,9 @@ -#if defined(Q_OS_ANDROID) - #include -#endif //Q_OS_ANDROID #include "native.h" #include +#if defined(Q_OS_ANDROID) + #include +#endif // Q_OS_ANDROID + QObject *NativeHelpers::application_p_ = 0; diff --git a/src/QZXingFilter.cpp b/src/QZXingFilter.cpp index 9f1024a..cc8bf11 100644 --- a/src/QZXingFilter.cpp +++ b/src/QZXingFilter.cpp @@ -28,7 +28,7 @@ QZXingFilter::QZXingFilter(QObject *parent) : QAbstractVideoFilter(parent) , decoding(false) { - /// Conecting signals to handlers that will send signals to QML + /// Connecting signals to handlers that will send signals to QML connect(&decoder, &QZXing::decodingStarted, this, &QZXingFilter::handleDecodingStarted); connect(&decoder, &QZXing::decodingFinished, @@ -130,12 +130,13 @@ static QImage rgbDataToGrayscale(const uchar* data, const int width, const int h const int targetWidth = isRectValid(captureRect) ? captureRect.width() : width; const int targetHeight = isRectValid(captureRect) ? captureRect.height() : height; const int endX = width - startX - targetWidth; - const int skipX = (endX + startX) * stride; + const int skipX = (endX + startX ) * stride; QImage image(targetWidth, targetHeight, QImage::Format_Grayscale8); - uchar* pixel = image.bits(); + uchar* pixelInit = image.bits(); data += (startY * width + startX) * stride; - for (int y = 0; y < targetHeight; ++y) { + for (int y = 1; y <= targetHeight; ++y) { + uchar* pixel = pixelInit + (targetHeight - y) * targetWidth; for (int x = 0; x < targetWidth; ++x) { uchar r = data[red]; uchar g = data[green]; @@ -152,6 +153,17 @@ static QImage rgbDataToGrayscale(const uchar* data, const int width, const int h } data += skipX; } + +// QTransform myTransform; +// myTransform.rotate(180); +// myTransform.scale(-1, 1); +// return image.transformed(myTransform); + + // static int cnt = 0; + // QImage image_pre(width, height, QImage::Format_RGB32); + // memcpy(image_pre.bits(), data, width*height*stride); + // image_pre.save("D:\\tmp\\" + QString::number(cnt++) + ".png"); + return image; } @@ -168,7 +180,7 @@ void QZXingFilterRunnable::processVideoFrameProbed(SimpleVideoFrame & videoFrame /// Let's try to convert it from RGB formats if (videoFrame.pixelFormat == QVideoFrame::Format_RGB32) - image = rgbDataToGrayscale(data, width, height, -1, 1, 2, 3, captureRect); + image = rgbDataToGrayscale(data, width, height, 0, 1, 2, 3, captureRect); else if (videoFrame.pixelFormat == QVideoFrame::Format_ARGB32) image = rgbDataToGrayscale(data, width, height, 0, 1, 2, 3, captureRect); else if (videoFrame.pixelFormat == QVideoFrame::Format_ARGB32_Premultiplied) diff --git a/src/QZXingFilter.h b/src/QZXingFilter.h index e16f2aa..0c19285 100644 --- a/src/QZXingFilter.h +++ b/src/QZXingFilter.h @@ -60,6 +60,7 @@ struct SimpleVideoFrame if(data.size() != frame.mappedBytes()) { qDebug() << "needed to resize"; + qDebug() << "size: " << data.size() << ", new size: " << frame.mappedBytes(); data.resize(frame.mappedBytes()); } memcpy(data.data(), frame.bits(), frame.mappedBytes());