启动外部活动以扫描条形码

时间:2017-03-30 15:39:24

标签: android c++ qt zxing barcode-scanner

我想从我的应用程序启动条形码扫描仪并获取标签

这是我到目前为止所做的:

scan.h

class DecodeBarCode : public QObject
{
    Q_OBJECT

    BarCodeReceiver *m_receiver;

public:
    explicit DecodeBarCode(QObject *parent = 0);
    ~DecodeBarCode();

    Q_INVOKABLE void useZXingApp();

signals:
    void tagFound(QString tag);
};

class BarCodeReceiver : public QAndroidActivityResultReceiver
{
    DecodeBarCode *m_decoder;
public:
    BarCodeReceiver(DecodeBarCode *decoder) : m_decoder(decoder) {}

    virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) {
        emit m_decoder->tagFound("Receiver worked");
    }

};

scan.cpp

DecodeBarCode::DecodeBarCode(QObject *parent) : QObject(parent)
{
    m_receiver = new BarCodeReceiver(this);
}

DecodeBarCode::~DecodeBarCode()
{
    delete m_receiver;
}

void DecodeBarCode::useZXingApp()
{
    QAndroidJniObject intent = QAndroidJniObject::fromString("com/google/zxing/client/android/SCAN");
    if (intent.isValid()) {
        QtAndroid::startActivity(intent,0,m_receiver); // CRASH HERE
    } else {
        emit tagFound("Invalid"); // TEMP
    }
}

这是我第一次使用JNI,从未在java中使用它,也没有在c ++ / Qt

中使用它

一定有一些非常错的东西,它是什么?

3 个答案:

答案 0 :(得分:1)

这是我使用意图从应用程序共享文本的示例:

void Sharer::share(const QString &content)
 {
     qDebug() << "sharing text: " << content;
 #ifdef Q_OS_ANDROID
     auto ACTION_SEND = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_SEND", "Ljava/lang/String;");
     auto EXTRA_TEXT = QAndroidJniObject::getStaticObjectField("android/content/Intent", "EXTRA_TEXT", "Ljava/lang/String;");
     auto intent = QAndroidJniObject("android/content/Intent", "(Ljava/lang/String;)V", ACTION_SEND.object());

     // Intent  Intent.putExtra(String name, String value)
     intent.callObjectMethod("putExtra", "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;", EXTRA_TEXT.object(), QAndroidJniObject::fromString(content).object());

     // Intent  Intent.setType(String type)
     intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString(QString("text/plain")).object());
     qDebug() << intent.toString();

     // static Intent Intent.createChooser(Intent target, CharSequence title)
     auto chooserIntent = QAndroidJniObject::callStaticObjectMethod("android/content/Intent", "createChooser", "(Landroid/content/Intent;Ljava/lang/CharSequence;)Landroid/content/Intent;", intent.object(), QAndroidJniObject::fromString(QString("It's Time To Choose...")).object());
     qDebug() << chooserIntent.toString();

     QtAndroid::startActivity(chooserIntent, 0, nullptr);

 #endif
 }

如您所见,您需要自己创建意图,检索所有需要的对象。

答案 1 :(得分:1)

我终于得到了它的工作,这里是完整的代码:

decodebarcode.h

    $.ajax({
            url: '@Url.Action("QuestionBlocks","YourController")',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(model),
            type: 'Get',
            dataType: 'json',
            processData: false
        })
        .success(function (result) {
           // Here do some stuff with your JSON to bind that in Table 
         var trHTML = '';

         $.each(data.Countries, function (i, item) {

          trHTML += 'Your Table Structure';
         });

          $('#YourID').append(trHTML);
        })
        .error(function (xhr, status) {
        alert("error");
     });

decodebarcode.cpp

#ifndef DECODEBARCODE_H
#define DECODEBARCODE_H

#include <QObject>
#ifdef Q_OS_ANDROID
#include <QtAndroidExtras/QtAndroid>
#include <QtAndroidExtras/QAndroidJniObject>
#include <QtAndroidExtras/QAndroidJniEnvironment>
#include <QtAndroidExtras/QAndroidActivityResultReceiver>
#endif

class BarCodeReceiver;

class DecodeBarCode : public QObject
{
    Q_OBJECT

    BarCodeReceiver *m_receiver;

public:
    explicit DecodeBarCode(QObject *parent = 0);
    ~DecodeBarCode();

    Q_INVOKABLE void useZXingApp();

signals:
    void tagFound(QString tag);
};

#ifdef Q_OS_ANDROID
class BarCodeReceiver : public QAndroidActivityResultReceiver
{
    DecodeBarCode *m_decoder;
public:
    BarCodeReceiver(DecodeBarCode *decoder);

    virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);

};
#else
class BarCodeReceiver
{
public:
    BarCodeReceiver(DecodeBarCode *decoder) {Q_UNUSED(decoder)};
};
#endif

#endif // DECODEBARCODE_H

如果您遇到同样的问题,我希望帮助

答案 2 :(得分:0)

使用此库来挽救你的生命:) https://github.com/zxing/zxing