【问题标题】:how to start Zxing on a Fragment?如何在 Fragment 上启动 Zxing?
【发布时间】:2021-01-29 16:03:22
【问题描述】:

我有一个包含两个片段的活动, 我想在其中一个片段上运行 ZXING 扫描仪,

目前我在这样的另一个活动中这样做>

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

除了在片段上打开扫描之外,我该怎么做?

我也能在这样的接收器上得到 ZXING 结果 >

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

如何将它们放在我要运行 Zxing 的 Fragment 上?

THNX

【问题讨论】:

    标签: android android-fragments zxing


    【解决方案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();
    }
    

    试试吧!

    【讨论】:

    • 兄弟,你救了我的命。谢谢sss
    • 它给了我error: cannot find symbol IntentIntegrator.forSupportFragment(this).initiateScan();
    【解决方案2】:

    除了打开片段扫描之外,我该怎么做?

    使用 getActivity() 将IntentIntegrator 中的 Context 传递为:

    new IntentIntegrator(getActivity()).initiateScan(); 
    

    如何将它们放在我要运行 Zxing 的 Fragment 上?

    在 Fragment 容器 Activity 中使用 super.onActivityResult(requestCode, resultCode, data); 行覆盖 onActivityResult 并在 Fragment 中覆盖 onActivityResult 方法。

    【讨论】:

    • 如何在片段本身中使用回调
    【解决方案3】:

    第 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中,让一个按钮被点击开始扫描二维码

    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 码的解码内容出现在日志文件中并作为敬酒!

    【讨论】:

      【解决方案4】:

      以上所有答案都是正确的,我想补充一下我是如何做到的。

      第 1 步:

      implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
      implementation 'com.google.zxing:core:3.2.1'
      

      第 2 步: 在我的活动中

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // This is important, otherwise the result will not be passed to the fragment
          super.onActivityResult(requestCode, resultCode, data);
      }
      

      第 3 步: 在我的片段中

      requestCamera.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //Request camera
                  IntentIntegrator integrator = IntentIntegrator.forSupportFragment(ScanFrag.this);
                  integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                  integrator.setPrompt("Scan QR code");
                  integrator.setCameraId(0);
                  integrator.setBeepEnabled(true);
                  integrator.setBarcodeImageEnabled(false);
                  integrator.initiateScan();
              }
          });
      

      我重写

       @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
          if(requestCode == IntentIntegrator.REQUEST_CODE) {
              if (result != null) {
                  if (result.getContents() == null) {
                      Log.d("ScanFrag", "Cancelled scan");
                      Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
                  } else {
                      Log.d("ScanFrag", "Scanned | " + result.getContents());
                      Toast.makeText(getContext(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                  }
              }
          }
      }
      

      希望它会帮助人们寻找如何从片段中使用 xzing

      【讨论】:

        【解决方案5】:

        以下代码效果很好。 -

        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.

        然后您可以将片段的onActivityResult 称为

        Fragment fragment = getSupportFragmentManager().findFragmentById(fragmentId);
            if(fragment instanceof ConsDetailUpdateFragment)
                ((ConsDetailUpdateFragment) fragment).onActivityResult(requestCode, resultCode, data);
        

        【讨论】:

        • 你在片段中哪里使用这个代码!?你在哪里使用积分器变量?
        • 在“BarcodeReader”类中使用它,最好在 setOnClickListener() 中使用。在上述代码之后使用积分器变量作为 itegrator.initiateScan()。
        猜你喜欢
        • 2015-07-09
        • 1970-01-01
        • 2016-12-19
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多