繁体   English   中英

将QR码扫描仪与Zxing集成时出错

[英]Error in integrating QR code scanner with Zxing

我正在尝试使用Zxing将QR码扫描仪集成到我的android应用中。 我已按照以下步骤操作:

  1. 我已经下载了ZXing.zip文件并将其解压缩。

  2. 将ZXing项目作为android现有项目打开,然后转到android文件夹并打开android文件夹,还将core.jar文件包含到名为CaptureActivity的ZXing项目中。

  3. 我已经将CaptureActivity项目用作名为“ QRCodeSample”的项目中的库。

这是我的MainActivity.java文件:

package com.charith.qrcodesample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button b1;
    TextView scanResult;
    String contents;
    public static final int REQUEST_CODDE = 1;
    protected static final String QR_CODE_MODE = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.bScan);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE",QR_CODE_MODE);
                startActivityForResult(intent, 0);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        scanResult = (TextView) findViewById(R.id.tvContent);
        if(requestCode == 0) {
            if(resultCode == RESULT_OK) {
                contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                scanResult.setText(contents);
            }else if(resultCode == RESULT_CANCELED){
                scanResult.setText("Error");
            }
        }
    }

}

这是我的AndroidManifest.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.charith.qrcodesample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.charith.qrcodesample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这是我的main_activity.xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="Scan" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bScan"
        android:layout_below="@+id/bScan"
        android:layout_marginTop="44dp"
        android:text="" />

</RelativeLayout>

我已经在Eclipse中使用模拟器检查了我的应用。 然后我有以下错误:

The application has stopped unexpectedly. Please try again

如果有人能尽快澄清这个问题,将不胜感激。

首先,您已经复制并粘贴了我们的项目。 我假设您也复制了UI。 正如您在此处的许多问题中所看到的那样,以及在https://code.google.com/p/zxing/wiki/LicenseQuestions中所讨论的,开源许可证不允许这样做。

其次,您已经复制并粘贴了AndroidManifest.xml声明。 您在我们的名称空间中声明一个Activity并拦截我们的Intent 这将干扰我们的应用程序,并且不行。 删除它并创建自己的清单。

但是第三,您似乎正在尝试通过Intent进行集成。 这比这容易得多,并且与错误地复制和粘贴所有这些东西无关。 参见https://code.google.com/p/zxing/wiki/ScanningViaIntent

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM