安卓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())));
}
相关推荐
快乐就好ya1 小时前
Java多线程
java·开发语言
IT学长编程1 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
CS_GaoMing2 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
Indigo_code2 小时前
【数据结构】【顺序表算法】 删除特定值
数据结构·算法
艾伦~耶格尔2 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
man20172 小时前
基于spring boot的篮球论坛系统
java·spring boot·后端
阿史大杯茶2 小时前
Codeforces Round 976 (Div. 2 ABCDE题)视频讲解
数据结构·c++·算法
2401_858120533 小时前
Spring Boot框架下的大学生就业招聘平台
java·开发语言
S hh3 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习
小雨cc5566ru3 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app