Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在碎片上启动Zxing?_Android_Android Fragments_Zxing - Fatal编程技术网

Android 如何在碎片上启动Zxing?

Android 如何在碎片上启动Zxing?,android,android-fragments,zxing,Android,Android Fragments,Zxing,我有一个活动包含两个片段, 我想在其中一个碎片上运行ZXING扫描仪 目前我在另一个类似这样的活动中执行此操作> new IntentIntegrator(this).initiateScan(); // opens up Scan intent > ZXING //results when activity enters a callback sent out to another activity public void onActivityResult(int reques

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

目前我在另一个类似这样的活动中执行此操作>

new IntentIntegrator(this).initiateScan(); // opens up Scan intent > ZXING
//results when activity enters a callback sent out to another activity
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
除了打开碎片的扫描,我该怎么做

我还可以在这样的接收器上获得ZXING结果>

new IntentIntegrator(this).initiateScan(); // opens up Scan intent > ZXING
//results when activity enters a callback sent out to another activity
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
我将如何在我要运行Zxing的片段上获得它们

THNX

除了打开碎片的扫描,我该怎么做

使用getActivity()将
IntentIntegrator
中的上下文传递为:

new IntentIntegrator(getActivity()).initiateScan(); 
我将如何在我要运行Zxing的片段上获得它们


使用
super.onActivityResult(请求代码、结果代码、数据)覆盖两个片段容器活动中的
onActivityResult
行和片段只需重写activityresult方法

如果您确实需要在支持片段中打开它,您可以使用:

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();
}

试试看

以下代码运行良好-

IntentIntegrator integrator = new IntentIntegrator(getActivity()) {
    @Override
    protected void startActivityForResult(Intent intent, int code) {
        EditorFragment.this.startActivityForResult(intent, 312); // REQUEST_CODE override
    }
};
然后您可以覆盖onActivityResult,一切正常

更多信息-

然后您可以将片段的
调用为activityresult

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

步骤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());
    }
}

现在二维码的解码内容出现在日志文件中,并作为祝酒词

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

步骤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的人

如何在片段中使用回调在片段中您在哪里使用此代码!?在哪里使用integrator变量?在“BarcodeReader”类中使用它,最好在setOnClickListener()中使用。使用integrator变量itegrator.initiateScan()就在上面的代码之后。兄弟,你救了我的命。感谢SSSIT给我的
错误:找不到符号IntentIntegrator.forSupportFragment(this.initiateScan()