安卓uir转二维码保存本地

第一步:集成zxing库

//集成二维码

api 'com.google.zxing:core:3.3.3'

//添加读写权限

第二步:uir转bitmap

复制代码
/**
 * Created by KeithXiaoY on 2016/7/27.
 * 生成一个二维码图像
 *
 * @param url
 * 传入的字符串,通常是一个URL
 * @param widthAndHeight
 * 图像的宽高
 * @return
 */
private final int BLACK = 0xff000000;
private final int WHITE = 0xffffffff;

public Bitmap createQRCode(String str, int widthAndHeight)
        throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = BLACK;
            } else {
                pixels[y * width + x] = WHITE;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

第三步:保存到本地

方法1:调系统的

复制代码
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "shareImage", "description");

方法2:

复制代码
/**
 * API29 中的最新保存图片到相册的方法
 */
public boolean saveBitmap(Bitmap toBitmap, Context context) {
    boolean state = false;
    //开始一个新的进程执行保存图片的操作
    Uri insertUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
    //使用use可以自动关闭流
    try {
        OutputStream outputStream = context.getContentResolver().openOutputStream(insertUri, "rw");
        if (toBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)) {
            state = true;
        } else {
            state = false;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return state;
}


方法3:

public static void saveImageToGallery(Context context, Bitmap bmp) {
    // 首先保存图片
    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // 其次把文件插入到系统图库
    try {
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                file.getAbsolutePath(), fileName, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // 最后通知图库更新
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
}
相关推荐
Geoking.几秒前
【设计模式】享元模式(Flyweight)详解:用共享对象对抗内存爆炸
java·设计模式·享元模式
callJJ2 分钟前
Spring设计模式与依赖注入详解
java·spring·设计模式·idea·工厂模式
码农三叔3 分钟前
(7-3)自动驾驶中的动态环境路径重规划:实战案例:探险家的行进路线
人工智能·算法·机器学习·机器人·自动驾驶
ExiFengs5 分钟前
Java使用策略模式实现多实体通用操作的优雅设计
java·开发语言·设计模式·策略模式
茶本无香6 分钟前
设计模式之三—工厂模式:灵活对象创建的艺术
java·开发语言·设计模式·工厂模式
游戏开发爱好者815 分钟前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
飞Link17 分钟前
【Water】数据增强中的数据标注、数据重构和协同标注
算法·重构·数据挖掘
a31582380622 分钟前
Android CardView修改背景阴影
android·cardview·修改背景
week_泽22 分钟前
第7课:管理长期记忆的关键架构决策 - 学习笔记_7
java·笔记·学习·ai agent
爱装代码的小瓶子25 分钟前
【c++进阶】c++11下类的新变化以及Lambda函数和封装器
java·开发语言·c++