安卓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())));
}
相关推荐
py有趣1 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗1 小时前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
工业甲酰苯胺3 小时前
实现 json path 来评估函数式解析器的损耗
java·前端·json
老前端的功夫3 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
@forever@3 小时前
【JAVA】LinkedList与链表
java·python·链表
LilySesy3 小时前
ABAP+WHERE字段长度不一致报错解决
java·前端·javascript·bug·sap·abap·alv
六件套是我3 小时前
redission实现延时队列
android·java·servlet
吃着火锅x唱着歌3 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
王元_SmallA4 小时前
Redis Desktop Manager(Redis可视化工具)安装
java·后端
ᐇ9594 小时前
Java HashMap深度解析:数据结构、原理与实战指南
java·开发语言·数据结构