简体   繁体   English

条码扫描器(ZXing)崩溃

[英]Barcode Scanner (ZXing) crashing

First off i want to tell you i have done all i can. 首先,我想告诉你我已经尽力了。 Followed like 10 tutorials on internet and 10 threads on stacked. 遵循了10互联网教程和10线程堆叠。 Still no success. 仍然没有成功。

I know guys that you recommend using scanning via intent,but in my case its not an option and i need to have it native in my app. 我知道您建议您通过意图进行扫描的人,但就我而言,这不是一种选择,我需要在应用程序中使用本机扫描。

I have downloaded the zxing library (2.1) and followed this tutorial 我已经下载了zxing库(2.1),并遵循了本教程

When i run this code on my Galaxy S3 or Galaxy Tab 10.1 as debug, the program crashes, after freezing for like 20 seconds, when i click the button that should start intent for result: com.google.zxing.client.android.SCAN or com.google.zxing.client.android.CaptureActivity . 当我在Galaxy S3或Galaxy Tab 10.1上以调试方式运行此代码时,冻结了大约20秒钟后,当我单击应该开始显示结果的按钮时,程序崩溃: com.google.zxing.client.android.SCANcom.google.zxing.client.android.CaptureActivity

Note that i have copied all the resources from Barcode Scanner app like beep sound, xml files and other. 请注意,我已经从Barcode Scanner应用程序复制了所有资源,例如蜂鸣声,xml文件等。

Crash Log 崩溃日志

My code is below: 我的代码如下:

MainActivity.java MainActivity.java

package com.example.philipscan;

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

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void scanNow(View view)
    {
        Log.e("test", "button works!");

        Intent intent = new Intent("com.google.zxing.client.android.CaptureActivity");
        startActivityForResult(intent, 3);
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
        Log.e("xZing", "Back");
        if (requestCode == 3) 
        {
            if (resultCode == RESULT_OK)
            {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Log.e("xZing", "contents: "+contents+" format: "+format);
                // Handle successful scan
            } 
            else if (resultCode == RESULT_CANCELED)
            {
                // Handle cancel
                Log.e("xZing", "Cancelled");
            }
        }
    }
}

Android Manifest Android清单

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

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

        <uses-feature android:name="android.hardware.camera"/>
          <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
          <uses-feature android:name="android.hardware.camera.flash" android:required="false"/>
          <uses-feature android:name="android.hardware.screen.landscape"/>
          <uses-feature android:name="android.hardware.wifi" android:required="false"/>
          <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>

        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.VIBRATE"/>
        <uses-permission android:name="android.permission.FLASHLIGHT"/>
        <uses-permission android:name="android.permission.READ_CONTACTS"/>
        <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.philipscan.MainActivity"
            android:label="@string/app_name" >
            <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>
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:label="@string/app_name" >
            <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>
    </application>

</manifest>

My SRC folder 我的SRC文件夹

我的档案清单

I'm very thankful for all the help i can get. 我非常感谢我能获得的所有帮助。

Try to create Intent another way 尝试以其他方式创建Intent

Intent scanIntent = new Intent("com.google.zxing.client.android.SCAN");
scanIntent.putExtra("SCAN_MODE", "ONE_D_MODE");

then startActivityForResult() as usual. 然后照常启动startActivityForResult()。 ONE_D_MODE is mode to scan 1D barcodes like Code39. ONE_D_MODE是扫描一维条形码(如Code39)的模式。

Remove all this code you copied from our project. 从我们的项目中删除所有复制的代码。 It's not necessary, you're not understanding it, and it's not supposed to be reused this way under the license: https://code.google.com/p/zxing/wiki/LicenseQuestions 没必要,您也不了解它,并且不应在以下许可下以这种方式重复使用: https : //code.google.com/p/zxing/wiki/LicenseQuestions

In particular you are not supposed to copy the manifest, and not allowed to copy the UI. 特别是,您不应复制清单,也不能复制UI。

Instead, it is much simpler, since you seem to be trying to use Intents anyway: https://code.google.com/p/zxing/wiki/ScanningViaIntent 相反,它要简单得多,因为您似乎仍然尝试使用Intent: https//code.google.com/p/zxing/wiki/ScanningViaIntent

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

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