Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
sanjeevtmimt
Posts: 16
Registered: ‎08-13-2012
My Device: BlackBerry
My Carrier: SE

How to scan and view QRCode ?

:Rockon:

 

yet I did not use it but I have onlu some helpful links which have some api for scanning and viewing the qrcode in cascades development.

 

QrCodeScanner

 

QrCodeView

Please use plain text.
Developer
bskania
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: How to scan and view QRCode ?

You can use zxing library to read QRcode.

 

Please use plain text.
New Developer
sanjeevtmimt
Posts: 16
Registered: ‎08-13-2012
My Device: BlackBerry
My Carrier: SE

Re: How to scan and view QRCode ?

[ Edited ]

can you provide a demo application of zxing library which can use to scan QRCode?

Removed personal information to comply withCommunity GuidelinesandTerms and Conditions of Use.

Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: How to scan and view QRCode ?

I'm working on a demo using ZXing to scan multiple barcode formats right now, but if all you care about is QRCodes the built in QRCode scanner classes work fine.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
Developer
bskania
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: How to scan and view QRCode ?

Hello,

This is the example how you can read a QRcode image.


QFile file(path);
qDebug() << path;
QImage qimage = QImage();
if (file.exists()) {
qimage.load(path, "PNG");
} else {
return "";
}
if (!qimage.isNull())
qDebug() << " file found and loaded " << qimage.height()
<< qimage.width();
else
return "";
Ref<LuminanceSource> imageRef(new CameraImageWrapper(qimage));
Ref<Binarizer> binarizer(new GlobalHistogramBinarizer(imageRef));
Ref<BinaryBitmap> _image(new BinaryBitmap(binarizer));
// Tell the decoder to try as hard as possible.
DecodeHints hints(DecodeHints::smileyvery-happy:EFAULT_HINT);
hints.setTryHarder(true);
// Perform the decoding.
QRCodeReader reader;
Ref<Result> _result(reader.decode(_image, hints));
// Output the result.
qDebug() << _result->getText()->getText().c_str() << endl;


For this you need to add library of ZXing.

Thanks.
Please use plain text.
Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)
My Carrier: Mobile Vikings

Re: How to scan and view QRCode ?

Why make it so complicated when it can be done in pure QML?

 

import bb.cascades 1.0
import bb.cascades.multimedia 1.0
import bb.multimedia 1.0

Page {
    Container {
        Camera {
            id: camera
            horizontalAlignment: HorizontalAlignment.Fill
            verticalAlignment: VerticalAlignment.Fill

            attachedObjects: [
                BarcodeDetector {
                    id: barcodeDetector
                    formats: BarcodeFormat.Any
                    camera: camera

                    onDetected: {
                        console.log("Data&colon; " + data);
                        console.log("Format: " + format);
                        systemSound.play();
                    }
                }
            ]

            onCameraOpened: {
                camera.startViewfinder();
            }
        }
    }

    onCreationCompleted: {
        camera.open();
    }
    attachedObjects: [
	    SystemSound {
	        id: systemSound
	        sound: SystemSound.CameraShutterEvent
	    }
	]
}

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: How to scan and view QRCode ?

At the time this thread was created only QR Codes were readable with the Cascades barcode decoder class. Now anything supported by ZXing is, as you noted.

 

Please don't revive long dead threads, especially since threads from last year were based on incomplete beta tools.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
Developer
pushkaraj9009
Posts: 72
Registered: ‎06-20-2013
My Device: Blackberry 10
My Carrier: idea

Re: How to scan and view QRCode ?

here is the code to decode QR codes using Zxing library

 

#include "BarcodeDecoder.hpp"

#include <bb/cascades/DisplayDirection>
#include <bb/cascades/StackLayout>
#include <bb/cascades/multimedia/CameraTypes.hpp>

#include <QSharedPointer>

#include <zxing/common/GreyscaleLuminanceSource.h>
#include <zxing/common/HybridBinarizer.h>

using namespace bb::cascades;
using namespace bb::cascades::multimedia;
using namespace bb::community::barcode;
using namespace zxing;

//! [0]
BarcodeDecoderControl::BarcodeDecoderControl(Container *parent)
    : CustomControl(parent)
    , m_camera(new Camera(parent))
    , m_cameraSettings(new CameraSettings(this))
    , m_landscapePreviewFrames(false)
    , m_nbuffers(2)
{
    setRoot(m_camera);

    connect(m_camera, SIGNAL(cameraOpened()),
            this, SLOT(onCameraOpened()));

    connect(m_camera, SIGNAL(viewfinderStopped()),
            this, SLOT(onViewfinderStopped()));

    //Prepare the camera
    m_camera->open(CameraUnit::Rear);

    //Configure camera settings
    m_camera->getSettings(m_cameraSettings);
    m_cameraSettings->setCameraMode(CameraMode::Photo);
    m_cameraSettings->setFocusMode(CameraFocusMode::ContinuousMacro);

    if (m_camera->applySettings(m_cameraSettings))
        qDebug() << "settings applied successfully";

    //Prepare the decoder
    m_reader = Ref<MultiFormatReader>(new MultiFormatReader());
    DecodeHints *hints = new DecodeHints();
    hints->addFormat(BarcodeFormat_QR_CODE);
    hints->addFormat(BarcodeFormat_EAN_8);
    hints->addFormat(BarcodeFormat_EAN_13);
    hints->addFormat(BarcodeFormat_UPC_A);
    hints->addFormat(BarcodeFormat_UPC_E);
    hints->addFormat(BarcodeFormat_DATA_MATRIX);
    hints->addFormat(BarcodeFormat_CODE_128);
    hints->addFormat(BarcodeFormat_CODE_39);
    hints->addFormat(BarcodeFormat_ITF);
    hints->addFormat(BarcodeFormat_AZTEC);
    m_reader.object_->setHints(*hints);
}
//! [0]

//! [1]
void BarcodeDecoderControl::onPreviewFrameAvailable(
        SharedUCharPointer previewBuffer, quint64 size, unsigned int width,
        unsigned int height, unsigned int stride)
{
    try {
        Ref<LuminanceSource> source(
                new GreyscaleLuminanceSource(previewBuffer.data(), stride,
                        size / stride, 0, 0, width, height));
        Ref<Binarizer> binarizer(new HybridBinarizer(source));
        Ref<BinaryBitmap> bitmap(new BinaryBitmap(binarizer));
        Ref<Result> result;

        // If the preview buffer is in landscape, we can rotate out bitmap to let us scan 1D codes
        if (m_landscapePreviewFrames) {
            Ref<BinaryBitmap> rotated = bitmap->rotateCounterClockwise();
            result = m_reader->decode(rotated);
        } else {
            result = m_reader->decode(bitmap);

        }

        const QString newBarcodeData = QString::fromStdString(result->getText()->getText().data());

        if (newBarcodeData != m_barcodeData) {
            m_barcodeData = newBarcodeData;
            emit newBarcodeDetected(m_barcodeData);
        }

    } catch (std::exception &e) {
        qDebug() << "+++++++ ERROR: " << e.what() << endl;
    }

    m_camera->addPreviewBuffer(previewBuffer, size);
}
//! [1]

//! [2]
void BarcodeDecoderControl::onCameraOpened()
{
    const quint64 bufferSize = m_camera->previewBufferSize();
    //Use two buffers for double buffering goodness.
    for (int i = 0; i < m_nbuffers; i++)
        m_camera->addPreviewBuffer(QSharedPointer<unsigned char>(new unsigned char[bufferSize]), bufferSize);

    connect(m_camera, SIGNAL(previewFrameAvailable(bb::cascades::multimedia::SharedUCharPointer, quint64, unsigned int, unsigned int, unsigned int)),
            this, SLOT(onPreviewFrameAvailable(bb::cascades::multimedia::SharedUCharPointer, quint64, unsigned int, unsigned int, unsigned int)));

    // If the preview frames are oriented East or West, it means we will have to rotate them 90 degrees to reliably detect 1D barcodes
    if (m_camera->devicePreviewFrameDirection() == DisplayDirection::East || m_camera->devicePreviewFrameDirection() == DisplayDirection::West)
        m_landscapePreviewFrames = true;

    m_camera->startViewfinder();
}
//! [2]

//! [3]
void BarcodeDecoderControl::startScanning() const
{
    m_camera->startViewfinder();
}

void BarcodeDecoderControl::stopScanning() const
{
    m_camera->stopViewfinder();
}

void BarcodeDecoderControl::onViewfinderStopped()
{
    m_barcodeData.clear();
}

 

Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: How to scan and view QRCode ?

Yes, you probably don't want to go to all that trouble unless you have special requirements, like inverted codes, or an unsupported format. That sample was created at a time when the Cascades component could only do QR codes, and to show how to process camera preview frames.

 

If you are just scanning a normal barcode, use the BarcodeDetector class. Easy-peasy.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
BlackBerry Development Advisor
tolang
Posts: 47
Registered: ‎04-19-2013
My Device: I love them all!
My Carrier: Carriers rock!

Re: How to scan and view QRCode ?

zezke has the best answer for scanning barcodes. Note that the scanner supports many formats, and by default scans for them all. You may want to set the formats to a smaller set just to be sure. Also, in 10.2 the detector will support scanning inverted barcodes. For creating barcodes, BarcodeGenerator and BarcodeView will create either a bb::ImageData (BarcodeGenerator) or work as a visual image (BarcodeView) in your application.
Please use plain text.