首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android/ Zxing : Zxing Scanner不会触发onActivityResult方法

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

Stack Overflow用户
提问于 2016-09-18 17:09:22
回答 1查看 467关注 0票数 0

我正在构建一个集成Zxing条形码扫描器的android应用程序。摄像机打开并显示红线,但不扫描数据。下面是我的代码片段:我使用了v4.app.fragment。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-18 17:35:01

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

代码语言:javascript
复制
compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'
compile 'com.google.zxing:core:3.2.0'

并删除

代码语言:javascript
复制
 compile 'com.google.zxing:core:3.2.1'

现在在OnClickListener内部集成扫描器,就像这样。

代码语言:javascript
复制
  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();
            }
  });

如果有效,请让我知道

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39555929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档