Android / Zxing:Zxing Scanner不会触发onActivityResult方法

时间:2016-09-18 09:09:22

标签: android android-fragments zxing

我正在构建一个整合Zxing条码扫描器的Android应用程序。相机打开并显示红线但不扫描数据。 这是片段的代码:我使用了v4.app.fragment。

public class AddItem extends Fragment{

    public AddItem() {
        // Required empty public constructor
    }
    String Idno;
    String Name;
    String Brand;
    String Cost;
    String Storeid;
    String Date;
    String Type;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_item, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageButton button=(ImageButton)getView().findViewById(R.id.img);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             IntentIntegrator.forSupportFragment(AddItem.this).setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES ).initiateScan();

            }
        });
    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve scan result
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        final EditText idno=(EditText)getView().findViewById(R.id.IdNum);
        final EditText title=(EditText)getView().findViewById(R.id.title);
        final EditText brand=(EditText)getView().findViewById(R.id.brand);
        final EditText cost=(EditText)getView().findViewById(R.id.price);
        final EditText store=(EditText)getView().findViewById(R.id.storeid);
        final EditText date=(EditText)getView().findViewById(R.id.date);


        if (scanningResult != null) {
            //we have a result
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            Toast toast = Toast.makeText(getActivity(),scanContent, Toast.LENGTH_SHORT);
            toast.show();
            // display it on screenformatTxt.setText("FORMAT: " + scanFormat);
            idno.setText(scanContent);


        }else{
            Toast toast = Toast.makeText(getActivity(),"No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }

这是我的应用程序gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.strokx.user.stockmanager"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:design:24.2.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.android.support:support-v4:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    compile 'com.google.firebase:firebase-database:9.4.0'
    compile 'com.google.firebase:firebase-auth:9.4.0'
    compile 'com.google.firebase:firebase-crash:9.4.0'
    compile 'com.firebase:firebase-client-android:2.4.0'
    compile 'com.journeyapps:zxing-android-embedded:3.3.0@aar'
    compile 'com.google.zxing:core:3.2.1'
    compile 'com.firebaseui:firebase-ui:0.4.3'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'  

这是我的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.strokx.user.stockmanager">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk tools:overrideLibrary="com.firebase.ui,com.firebase.ui.auth,com.facebook,android.support.customtabs"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/purchase_icon"
        android:label="@string/app_name"
        android:name=".StockManager"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:1)

在build.gradle中添加这些依赖项

compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'
compile 'com.google.zxing:core:3.2.0'

并删除

 compile 'com.google.zxing:core:3.2.1'

现在OnClickListener内部就像这样整合了扫描仪。

  button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

    IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
                scanIntegrator.setPrompt("Scan a Barcode");
                scanIntegrator.setBeepEnabled(true);
                scanIntegrator.setOrientationLocked(true);
                scanIntegrator.setBarcodeImageEnabled(true);
                scanIntegrator.initiateScan();
            }
  });

让我知道它是否有效