如何在 QR 扫描结果中激活 URL,使用 ZXing fork 编写
How to make active URL in QR Scanner result, writen using ZXing fork
我正在使用 ZXing (https://github.com/journeyapps/zxing-android-embedded) 的分支为 Android 创建 QR 扫描仪。程序扫描 QR 码和条形码,但在 Alertdialog 中,我以纯文本形式获得所有结果,即使它是 link。我希望程序在结果框中将 url 显示为活动的 links,如果它是 link,如果它是条形码则显示纯文本。
我用这个库看了每一个教程https://github.com/journeyapps/zxing-android-embedded(是的,它与 ZXing 不兼容,所以我不能使用与 ZXing 相关的代码,正如我所知),我阅读了可用的文档,用谷歌搜索了它, 在任何地方都找不到对此的回答。
这是代码,负责在扫描后显示结果。
private void scanCode() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptureAct.class);
integrator.setOrientationLocked(false);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Place QR code or Barcode in square");
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result.getContents());
builder.setTitle("Result");
builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scanCode();
}
}).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
我认为您需要将文本结果转换为 link。我想你可以使用 Linkify。为对话框设置自定义布局并添加 TextView oh 布局。
更改您的 onActivityResult 方法,如下所示,以显示 url 处于活动状态 link 并将文本显示为来自扫描结果的 Alertdialog 中的纯文本。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Scan Result :");
builder.setTitle("Result");
builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scanCode();
}
}).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// added few lines
TextView text = new TextView(this);
SpannableString url = SpannableString.valueOf(result.getContents());
Linkify.addLinks(url,Linkify.WEB_URLS);
text.setText(url);
text.setMovementMethod(LinkMovementMethod.getInstance());
AlertDialog dialog = builder.create();
dialog.setView(text);
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
我正在使用 ZXing (https://github.com/journeyapps/zxing-android-embedded) 的分支为 Android 创建 QR 扫描仪。程序扫描 QR 码和条形码,但在 Alertdialog 中,我以纯文本形式获得所有结果,即使它是 link。我希望程序在结果框中将 url 显示为活动的 links,如果它是 link,如果它是条形码则显示纯文本。
我用这个库看了每一个教程https://github.com/journeyapps/zxing-android-embedded(是的,它与 ZXing 不兼容,所以我不能使用与 ZXing 相关的代码,正如我所知),我阅读了可用的文档,用谷歌搜索了它, 在任何地方都找不到对此的回答。
这是代码,负责在扫描后显示结果。
private void scanCode() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptureAct.class);
integrator.setOrientationLocked(false);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Place QR code or Barcode in square");
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result.getContents());
builder.setTitle("Result");
builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scanCode();
}
}).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
我认为您需要将文本结果转换为 link。我想你可以使用 Linkify。为对话框设置自定义布局并添加 TextView oh 布局。
更改您的 onActivityResult 方法,如下所示,以显示 url 处于活动状态 link 并将文本显示为来自扫描结果的 Alertdialog 中的纯文本。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Scan Result :");
builder.setTitle("Result");
builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scanCode();
}
}).setNegativeButton("Quit app", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// added few lines
TextView text = new TextView(this);
SpannableString url = SpannableString.valueOf(result.getContents());
Linkify.addLinks(url,Linkify.WEB_URLS);
text.setText(url);
text.setMovementMethod(LinkMovementMethod.getInstance());
AlertDialog dialog = builder.create();
dialog.setView(text);
dialog.show();
} else {
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}