BarCode QR Code Scanner在我的应用程序中的实现

时间:2014-04-10 08:51:12

标签: barcode zxing google-glass

我正在尝试在我的android应用程序中集成qr代码扫描程序,我正在使用zxing库BarcodeEye。

我已经实现了以下代码

Intent intent = new Intent("com.github.barcodeeye.scan.CaptureActivity");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);

but i am getting a RuntimeException saying that unable to start activity component : android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.github.barcodeeye.scan}

任何人都可以帮助我在哪里出错

谢谢&问候。 Nagendra

2 个答案:

答案 0 :(得分:0)

以下是我使用BarCodeEye的方法:

private void startScanActivityForResult(Bundle extras)
{
    Intent intentScan = new Intent("com.github.barcodeeye.SCAN");
    intentScan.setPackage("com.github.barcodeeye");
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    intentScan.putExtras(extras);
    intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
    intentScan.putExtra("RESULT_DISPLAY_DURATION_MS", 1000L);
    intentScan.putExtra("SAVE_HISTORY", false);
    intentScan.putExtra("PROMPT_MESSAGE", "QR scan http://user:pass@server");
    WtcLog.info(TAG, "startScanActivityForResult: startActivityForResult(intentScan=" + intentScan + ", REQUEST_SCAN)");
    startActivityForResult(intentScan, REQUEST_SCAN);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    WtcLog.info(TAG, "onActivityResult data=" + WtcUtilsPlatform.toString(data));

    Bundle extras = null;
    if (data != null)
    {
        extras = data.getExtras();
    }

    switch (requestCode)
    {
        case REQUEST_SCAN:
        {
            WtcLog.info(TAG, "onActivityResult: REQUEST_SCAN");
            if (resultCode == RESULT_OK)
            {
                WtcLog.info(TAG, "onActivityResult: resultCode == RESULT_OK");

                String result = data.getStringExtra("SCAN_RESULT");

                extras = validateScan(result);

                /// your logic here

            }
            else
            {
                WtcLog.warn(TAG, "onActivityResult: resultCode != RESULT_OK; exiting");
                // your logic here
                finish();
            }
            break;
        }
    }
}

private Bundle validateScan(String result)
{
    Bundle extras = new Bundle();

    String[] userpass;

    try
    {
        URI uri = new URI(result);

        String userinfo = uri.getRawUserInfo();
        if (WtcString.isNullOrEmpty(userinfo))
        {
            throw new URISyntaxException(result, "");
        }

        userpass = userinfo.split(":", 2);
        if (userpass.length != 2)
        {
            throw new URISyntaxException(result, "");
        }

        String server = uri.getHost();
        if (WtcString.isNullOrEmpty(server))
        {
            throw new URISyntaxException(result, "");
        }
        extras.putString("server", server);
    }
    catch (URISyntaxException e)
    {
        WtcLog.error(TAG, "validateScan: EXCEPTION", e);
        extras.putString("error", "uri must be in format \"http://username:password@server\"");
        return extras;
    }

    try
    {
        String username = URLDecoder.decode(userpass[0], "UTF-8");
        extras.putString("username", username);

        String password = URLDecoder.decode(userpass[1], "UTF-8");
        extras.putString("password", password);
    }
    catch (UnsupportedEncodingException e)
    {
        WtcLog.error(TAG, "validateScan: EXCEPTION", e);
        extras.putString("error", "username and password must be UTF-8");
        return extras;
    }

    return extras;
}

答案 1 :(得分:0)

您需要在AndroidManifest.xml中注册CaptureActivity Activity

<activity
        android:name="com.github.barcodeeye.scan.CaptureActivity"
        android:clearTaskOnLaunch="true"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:stateNotNeeded="true"
        android:theme="@style/CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="com.github.barcodeeye.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>