Open zxing barcode scanner via intent



  • Hey guys,

    I'd like to start the zxing barcode/qr scanner via an Intent on Android and then receive and parse the result via C++ Qt code.

    https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

    I've made some attempts but for some reason I always just received a whitescreen.

    Could anyone give it a try and share his results?

    The Java code without Qt would be:

    @public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.setPackage("com.google.zxing.client.android");
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    startActivityForResult(intent, 0);
    }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
    if (resultCode == RESULT_OK) {
    String contents = intent.getStringExtra("SCAN_RESULT");
    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    // Handle successful scan
    } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    }
    }
    }@

    Thanks in advance!



  • Ok, I got the QR Scanner to start and can also receive results. Problem now is, I have to pass the results from the Java class to Qt...



  • Current code without java file:

    @ QAndroidJniObject Scanner;
    QAndroidJniObject Package;
    QAndroidJniObject Subject;
    QAndroidJniObject Text;
    QAndroidJniObject intent("android/content/Intent");

    Scanner = QAndroidJniObject::fromString("com.google.zxing.client.android.SCAN");
    Package = QAndroidJniObject::fromString("com.google.zxing.client.android");
    Subject = QAndroidJniObject::fromString("SCAN_MODE");
    Text = QAndroidJniObject::fromString("QR_CODE_MODE");
    intent.callObjectMethod("addAction","(Ljava/lang/String;)Landroid/content/Intent;",Scanner.object<jstring>());
    intent.callObjectMethod("setPackage","(Ljava/lang/String;)Landroid/content/Intent;",Package.object<jstring>());
    intent.callObjectMethod("putExtra","(Ljava/lang/String;Ljava/lang/string;)Landroid/content/Intent;",Subject.object<jstring>(), Text.object<jstring>());
    QtAndroid::startActivity(intent,123456);@


  • me also facing same problem ,but iam trying like this result code got -1 ,but scanner result showing null.
    class OnActivityResult : public QAndroidActivityResultReceiver
    {
    void handleActivityResult(int requestCode, int resultCode, const QAndroidJniObject & intent)
    {
    qDebug() << "**** " << requestCode << " : " << resultCode << "data" << intent.toString();

            if (requestCode == 12345)
            {
                if (resultCode != 0 && intent.isValid())
                {
    
                    QAndroidJniObject jPackage;
                    QAndroidJniObject jClass;
                    QAndroidJniObject jresult;
                    QAndroidJniObject jdata;
    
                    jPackage = QAndroidJniObject::fromString("com.google.zxing.client.android");
                    jClass =  QAndroidJniObject::fromString("com.google.zxing.client.android.SCAN");
                    jresult = QAndroidJniObject::fromString("RESULT_OK");
    
    
                    intent.callObjectMethod("setPackage"
                    , "(Ljava/lang/String;)Landroid/content/Intent;"
                    , jPackage.object<jstring>());
    
                    intent.callObjectMethod("setAction"
                    , "(Ljava/lang/String;)Landroid/content/Intent;"
                    , jClass.object<jstring>());
    
    
                    jdata=intent.callObjectMethod("getExtra"
                    , "(Ljava/lang/String;)Landroid/content/Intent;"
                    , jresult.object<jstring>());
                    qDebug() << "DATA:" << jdata.toString() ;
    

    // QAndroidJniObject result =
    // intent.getObjectField<jstring>("SCAN_RESULT");
    // QAndroidJniObject format =
    // intent.getObjectField<jstring>("SCAN_RESULT_FORMAT");

    // qDebug() << result.toString() << " : " << format.toString();
    }
    }
    }
    };


  • Banned

    This post is deleted!


  • Hi,

    I've successfully implemented zxing barcode scanning via intent from Qt. I don't think it's the proper way to do it, but it works.

    I copied QtActivity.java and added some code in onActivityResult to get hold of the return values. I'm quite sure you can do this a cleaner way.

    <code>
    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data)    {
        //Barcode results
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null)
        {
           barcodeData = result.getContents();
           Log.i(QtApplication.QtTAG, "Barcode activity result: data=" + barcodeData + ", formatName=" + result.getFormatName() + ", errorCorrectionLevel=" + result.getErrorCorrectionLevel());
           if (barcodeData == null) barcodeData = "#SC$SCAN_ABORT$SC#"; // Signal scan is finished, but with no data.
        }
       //Barcode results finished
        if (QtApplication.m_delegateObject != null && QtApplication.onActivityResult != null) {
            QtApplication.invokeDelegateMethod(QtApplication.onActivityResult, requestCode, resultCode, data);
            return;
        }
        if (requestCode == MINISTRO_INSTALL_REQUEST_CODE)
            startApp(false);
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    </code>


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.