【问题标题】:Getting started with ZXing on Android在 Android 上开始使用 ZXing
【发布时间】:2010-06-13 15:56:42
【问题描述】:

我正在尝试将 ZXing 添加到我的项目中(添加一个在按下时调用扫描仪的按钮)。我找到了这个:http://groups.google.com/group/android-developers/browse_thread/thread/788eb52a765c28b5,当然还有 ZXing 主页:http://code.google.com/p/zxing/,但仍然不知道要在项目类路径中包含什么才能使其正常工作!

就目前而言,我将第一个链接中的类复制到我的项目中(更改了一些包名称),它运行但在按下按钮并尝试安装条形码扫描仪后崩溃。

一些代码:

private void setScanButton(){
    Button scan = (Button) findViewById(R.id.MainPageScanButton);
    scan.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            IntentIntegrator.initiateScan(MyActivity.this);
        }
    });
}

产生的错误(来自 logcat):

06-13 15:26:01.540: ERROR/AndroidRuntime(1423): Uncaught handler: thread main exiting due to uncaught exception
06-13 15:26:01.560: ERROR/AndroidRuntime(1423): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pname:com.google.zxing.client.android }

想法?

【问题讨论】:

  • 顺便说一句,我在安卓模拟器 v1.6 上运行
  • 我按照 Google Groups 线程中的说明进行操作,当我将它部署到我的三星 Galaxy S 时,它第一次工作。我什至不需要重命名任何东西。在我的项目中,我让它创建一个新的类路径“com.google.zxing.integration.andoid”。
  • 试试我的博客,可能会有所帮助mcondev.wordpress.com/2011/06/24/…

标签: android barcode-scanner zxing


【解决方案1】:

前往here 获取链接。

在要触发条形码扫描的活动中包含

IntentIntegrator.initiateScan(YourActivity.this); 

然后还包括:

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
            TextView 
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
};

条形码扫描仪应用将处理实际扫描。如果 Barcode Scanner 应用程序未安装,集成商将提示他们 安装它。

-----------来自nEx.Software ---------------

【讨论】:

    【解决方案2】:

    首先,ZXing 在模拟器上无法自动提示用户从 Market 下载,因为模拟器上没有 Market。您需要在模拟器上手动安装 Barcode Scanner APK。

    其次,由于模拟器不模拟相机,条形码扫描仪可能对您没有太大帮助。您很可能需要在设备上对此进行测试。

    【讨论】:

    • @Sean Owen:对不起,有人不知道他们在说什么,编辑了我的答案。我需要找出 SO 中有什么样的编辑警报...
    • 呃,是的,我在发表评论后才知道发生了什么。明白了。
    【解决方案3】:

    只需将此代码添加到您的清单文件中,在 application 标记内:

     <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.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    

    然后在文件顶部添加以下权限,如果尚未添加,则在文件顶部:

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

    【讨论】:

      【解决方案4】:

      检查您的 AndroidManifest 是否为新添加的活动正确地赋予了“android:name”属性。您收到“ActivityNotFoundException”,这主要是因为您可能使用了不同的包名,而 ZXing 使用的是“com.google.zxing.client.android”包名。当你加载 ZXing 的第一个 Activity 时,给它绝对类路径而不是相对路径。然后你的错误就会消失。

      【讨论】:

        【解决方案5】:

        条形码扫描仪应用程序未安装在您的模拟器上,这会导致此异常。以下链接提供了如何在模拟器上安装第 3 方应用程序的分步指南:

        Install application on emulator

        【讨论】:

          【解决方案6】:

          如果你是第一次使用zxing,我推荐这个项目*1*,它是zxing的一部分,你只需要导入项目并运行它。这个项目是一个尝试使在 Android 中使用 QR 码更容易一些。 强烈推荐给bigenner。祝你好运。 最后,感谢肖恩·欧文;

          【讨论】:

            【解决方案7】:
            1. 为模拟器开启 SD 卡。
            2. 将链接复制到在ZXing download page 上找到的 BarcodeScaner
            3. 在模拟器中打开浏览器并指向链接编辑框
            4. 输入终端和命令 adb shell input text 'https://code.google.com/p/zxing/downloads/detail?name=BarcodeScanner-4.5.1.apk&amp;can=2&amp;q=' - 链接是ZXing下载页面的链接
            5. 链接已复制到浏览器,您可以下载并安装它。这解决了所描述的问题。

            【讨论】:

              【解决方案8】:

              我在选项卡(片段)中使用 Zxing 并使用支持库(用于 Material Design 组件),所以我不得不这样称呼它:

              IntentIntegrator 积分器 = new IntentIntegrator(getActivity()); integrator.forSupportFragment(this).initiateScan();

              然后在onActivityResult()中

                  if (resultCode == Activity.RESULT_OK) {
                      if (requestCode == IntentIntegrator.REQUEST_CODE) {
              
                          String contents = data.getStringExtra("SCAN_RESULT");
                          String format = data.getStringExtra("SCAN_RESULT_FORMAT");
                          Log.i(TAG, "Barcode Result: " + contents);
                          etc...
                      }
                  }
              

              在我的 Manifest.xml 中

                  <activity
                      android:name="com.google.zxing.client.android.CaptureActivity"
                      android:configChanges="orientation|keyboardHidden"
                      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>
              

              现在一切都很好。仅使用意图和 startActivityForResult() 并不成功。扫描仪将启动并修复二维码,但没有返回。

              在我的 build.grade 中,我有:

              存储库 { mavenCentral() 行家{网址“https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/”} }

              compile 'com.google.zxing:core:3.2.1'
              compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'
              compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-04-01
                • 1970-01-01
                • 1970-01-01
                • 2014-01-04
                • 2011-04-10
                • 2019-01-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多