2016-09-18 149 views
0

我正在构建一个集成Zxing条码扫描器的android应用程序。摄像机打开并显示红线,但不扫描数据。 这是我的片段代码:我已经使用了v4.app.fragment。Android/Zxing:Zxing扫描仪不会触发onActivityResult方法

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:[email protected]' 
    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> 
+0

你得到什么错误? –

+0

无。相机不扫描任何东西。当我按下后退按钮时,显示未扫描数据的其他情况 –

回答

0

在你的build.gradle添加这些依赖

compile 'com.journeyapps:zxing-android-embedded:[email protected]' 
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(); 
      } 
    }); 

让我知道,如果它