安卓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())));
}
相关推荐
怦怦蓝5 分钟前
IDEA 开发邮件发送功能:全流程报错解决方案汇总
java·ide·intellij-idea·发邮件
Cx330❀6 分钟前
【优选算法必刷100题】第43题(模拟):数青蛙
c++·算法·leetcode·面试
杜子不疼.6 分钟前
【LeetCode30_滑动窗口 + 哈希表】:三招搞定“串联所有单词的子串”
数据结构·算法·哈希算法
闻缺陷则喜何志丹6 分钟前
【C++动态规划 状压dp】1879. 两个数组最小的异或值之和|2145
c++·算法·动态规划·力扣·数组·最小·动态规范
艾莉丝努力练剑11 分钟前
【优选算法必刷100题:专题五】(位运算算法)第033~38题:判断字符是否唯一、丢失的数字、两整数之和、只出现一次的数字 II、消失的两个数字
java·大数据·运维·c++·人工智能·算法·位运算
光羽隹衡11 分钟前
机器学习——DBSCAN算法
人工智能·算法·机器学习
vyuvyucd11 分钟前
Java数组与Arrays类实战指南
数据结构·算法
csuzhucong11 分钟前
七彩鹦鹉螺魔方
算法
逝川长叹12 分钟前
利用 SSI-COV 算法自动识别线状结构在环境振动下的模态参数研究(Matlab代码实现)
前端·算法·支持向量机·matlab
山上三树12 分钟前
详细介绍 C 语言中的匿名结构体
c语言·开发语言·算法