You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Java中使用ZXING扫描二维码如何获取已扫描的二维码图像?

问题根因

你遇到的报错是依赖版本过低导致的,com.journeyapps:zxing-android-embedded从3.2.0版本才开始在IntentResult类中提供getBitmap()方法,你当前使用的3.0.3版本还没有这个API,所以会提示无法找到方法。

解决步骤

  1. 升级依赖版本
    把build.gradle中的依赖替换为更高的稳定版本:
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.google.zxing:core:3.5.1'

注意:4.x版本要求项目最低适配API 16(Android 4.1)及以上,如果你的项目minSdk低于16,可选择3.6.0版本,同样支持getBitmap()方法

  1. 验证Bitmap获取逻辑
    你现有代码中已经调用了integrator.setBarcodeImageEnabled(true),这个配置是必须的,不开启的话即使升级版本也拿不到扫描的二维码图像。升级依赖后,原有调用result.getBitmap()的代码就可以正常返回扫描时的原图Bitmap。

  2. 可选:Bitmap本地保存实现
    拿到Bitmap后可参考以下逻辑保存到公共相册,不需要申请存储权限(适配Android 10及以上分区存储特性):

private void saveQrBitmapToAlbum(Bitmap bitmap) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DISPLAY_NAME, "QRCode_" + System.currentTimeMillis() + ".png");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    try {
        OutputStream os = getContentResolver().openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.close();
        Toast.makeText(this, "二维码已保存到相册", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
    }
}

内容的提问来源于stack exchange,提问作者B_Stromberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 23:54:07