在Android Studio中,我希望二维码在扫描二维码时在屏幕上返回图像



我开发了一个应用程序,二维码扫描仪可以返回二维码保存的两段文本。我是使用android studio进行编码的新手,并且在很大程度上使用了教程。我现在想在屏幕上返回一张图像,扫描时二维码会保存该图像。这可能吗?

以下是从二维码向应用程序返回文本的代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());
//setting values to textviews
textViewName.setText(obj.getString("name"));
textViewAddress.setText(obj.getString("address"));
} catch (JSONException e) {
e.printStackTrace();
//if control comes here
//that means the encoded format not matches
//in this case you can display whatever data is available on the qrcode
//to a toast
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}

您可以创建一个带有URL的二维码来获取图像并显示它。二维码可以存储非常少量的数据来保存图像。

编辑:

要显示实际图像而不是其URL,您只需要在布局中添加一个ImageView(如果需要,可以删除TextView),并在读取二维码后将图像加载到上面。为了以最简单的方式做到这一点,你可以使用毕加索图书馆。这篇文章很好地解释了如何安装和使用它。试试看,如果你需要更多的帮助,请告诉我。

最新更新