如何在 Fragment 上启动 Zxing?
how to start Zxing on a Fragment?
我有一个 activity 包含两个片段,
我想 运行 ZXING 扫描器在其中一个片段上,
目前我在另一个 activity 上这样做 >
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
how do i do that line but to open up the scan on a fragment ?
使用 getActivity() 在 IntentIntegrator
中传递 Context 为:
new IntentIntegrator(getActivity()).initiateScan();
how will i get them on my Fragment that i'm going to run Zxing on ?
在 Fragment 容器 Activity 中使用 super.onActivityResult(requestCode, resultCode, data);
行覆盖 onActivityResult
,在 Fragment 中覆盖 onActivityResult
方法。
如果您确实需要在支持片段中打开它,您可以使用:
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,一切正常。
更多信息 - here you go.
然后您可以将片段的 onActivityResult
称为
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'
}
第二步:在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 步:在父级中 activity
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 步:
在我的 activity
@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 的人
我有一个 activity 包含两个片段, 我想 运行 ZXING 扫描器在其中一个片段上,
目前我在另一个 activity 上这样做 >
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
how do i do that line but to open up the scan on a fragment ?
使用 getActivity() 在 IntentIntegrator
中传递 Context 为:
new IntentIntegrator(getActivity()).initiateScan();
how will i get them on my Fragment that i'm going to run Zxing on ?
在 Fragment 容器 Activity 中使用 super.onActivityResult(requestCode, resultCode, data);
行覆盖 onActivityResult
,在 Fragment 中覆盖 onActivityResult
方法。
如果您确实需要在支持片段中打开它,您可以使用:
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,一切正常。
更多信息 - here you go.
然后您可以将片段的 onActivityResult
称为
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'
}
第二步:在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 步:在父级中 activity
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 步: 在我的 activity
@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 的人