2016-05-16 13 views
1

私はどのようにそれを行うかフラグメントでZxingを開始するには?

new IntentIntegrator(this).initiateScan(); // opens up Scan intent > ZXING 

>私は、私は断片の一つにスキャナをZXING実行したい 、二つの断片を保持している活性を有する

現在、私はこのような別のアクティビティにこれを行いますフラグメントのスキャンを開くことはできますか?

はまた、私は私が上Zxing実行するつもりです私の断片にそれらを取得する方法

//results when activity enters a callback sent out to another activity 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

>このようなレシーバにZXING結果を得ますか?

のthnx

答えて

3

私はその行をどのように行うのですが、フラグメントにスキャンを開くには?

利用getActivity()としてIntentIntegratorでコンテキストを渡す:

私は上のZxing実行するつもりです私の断片にそれらを取得する方法
new IntentIntegrator(getActivity()).initiateScan(); 

super.onActivityResult(requestCode, resultCode, data);ラインを有する断片コンテナ活性の両方におけるフラグメントで

オーバーライドonActivityResultだけonActivityResultメソッドをオーバーライドします。

+0

フラグメント自体にコールバックを使用する方法 –

1

あなたは本当にあなたが使用できるサポートフラグメントでそれを開くために必要がある場合:あなたのフラグメントで

IntentIntegrator.forSupportFragment(MyFragment.this).initiateScan(); 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
    String barcode = result.getContents(); 
} 

はそれをお試しください!

0

次のコードは正常に動作します。 -

IntentIntegrator integrator = new IntentIntegrator(getActivity()) { 
    @Override 
    protected void startActivityForResult(Intent intent, int code) { 
     EditorFragment.this.startActivityForResult(intent, 312); // REQUEST_CODE override 
    } 
}; 

次に、onActivityResultをオーバーライドするとすべて正常に動作します。

詳細情報 - here you go.

+0

フラグメント内でこのコードをどこで使用しますか。インテグレータ変数はどこで使用しますか? – Rostan

+0

"BarcodeReader"クラス内で使用します。できるだけsetOnClickListener()内で使用することをお勧めします。上記のコードの直後にintegrator変数をitegrator.initiateScan()として使用します。 – Tom

0

ステップ1:build.gradleでの依存関係を含める

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.journeyapps:zxing-android-embedded:3.5.0' 
compile 'com.android.support:appcompat-v7:25.3.1' 
testCompile 'junit:junit:4.12' 

}

ステップ2:OnCreateViewで、ボタンはのスキャンを開始するためにクリックさせqrコード

scan_button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      IntentIntegrator integrator = new IntentIntegrator(getActivity()); 
      integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); 
      integrator.setPrompt("Please focus the camera on the QR Code"); 
      integrator.setCameraId(0); 
      integrator.setBeepEnabled(false); 
      integrator.setBarcodeImageEnabled(false); 
      integrator.initiateScan(); 
     } 
    }); 

ステップ3:親アクティビティ

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
    if(scanResult != null){ 
     Toast.makeText(this, " >>>>"+scanResult.toString(), Toast.LENGTH_LONG).show(); 
     Log.e(">>>>"," "+scanResult.getContents().toString()); 
    } 
} 

ここで、qrコードのデコードされた内容がログファイルに表示され、トーストとして表示されます。

関連する問題