zoukankan      html  css  js  c++  java
  • 使用ZXing库生成二维码

    1. 首先在库依赖中添加 com.google.zxing:core:3.2.1
    2. /** 
    3.      * 生成二维码 
    4.      * @param string 二维码中包含的文本信息 
    5.      * @param mBitmap logo图片 
    6.      * @param format  编码格式 
    7.      * @return Bitmap 位图 
    8.      * @throws WriterException 
    9.      */  
    10.     public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)  
    11.             throws WriterException {  
    12.         Matrix m = new Matrix();  
    13.         float sx = (float2 * IMAGE_HALFWIDTH / mBitmap.getWidth();  
    14.         float sy = (float2 * IMAGE_HALFWIDTH  
    15.                 / mBitmap.getHeight();  
    16.         m.setScale(sx, sy);//设置缩放信息  
    17.         //将logo图片按martix设置的信息缩放  
    18.         mBitmap = Bitmap.createBitmap(mBitmap, 00,  
    19.                 mBitmap.getWidth(), mBitmap.getHeight(), m, false);  
    20.         MultiFormatWriter writer = new MultiFormatWriter();  
    21.         Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();  
    22.         hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码  
    23.         BitMatrix matrix = writer.encode(string, format, 400400, hst);//生成二维码矩阵信息  
    24.         int width = matrix.getWidth();//矩阵高度  
    25.         int height = matrix.getHeight();//矩阵宽度  
    26.         int halfW = width / 2;  
    27.         int halfH = height / 2;  
    28.         int[] pixels = new int[width * height];//定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息  
    29.         for (int y = 0; y < height; y++) {//从行开始迭代矩阵  
    30.             for (int x = 0; x < width; x++) {//迭代列  
    31.                 if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH  
    32.                         && y > halfH - IMAGE_HALFWIDTH  
    33.                         && y < halfH + IMAGE_HALFWIDTH) {//该位置用于存放图片信息  
    34. //记录图片每个像素信息  
    35.                     pixels[y * width + x] = mBitmap.getPixel(x - halfW  
    36.                             + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);                } else {  
    37.                     if (matrix.get(x, y)) {//如果有黑块点,记录信息  
    38.                         pixels[y * width + x] = 0xff000000;//记录黑块信息  
    39.                     }  
    40.                 }  
    41.   
    42.             }  
    43.         }  
    44.         Bitmap bitmap = Bitmap.createBitmap(width, height,  
    45.                 Bitmap.Config.ARGB_8888);  
    46.         // 通过像素数组生成bitmap  
    47.         bitmap.setPixels(pixels, 0, width, 00, width, height);  
    48.         return bitmap;  
    49.     }  
  • 相关阅读:
    time zone list
    docker build doris-0.11.20-release source code
    Apache Flink 开发环境搭建和应用的配置、部署及运行
    locate home of running java application
    Android wpa_supplicant 四次握手 流程分析
    Wifi 开放系统认证和共享密钥身份认证
    Android WiFi 获取国家码
    Android WiFi 扫描流程分析(wpa_supplicant选择网络)
    Android WiFi 日志记录(ASSOC_REJECT)
    Android WiFi 扫描流程分析(wpa_supplicant)
  • 原文地址:https://www.cnblogs.com/vegetate/p/9997336.html
Copyright © 2011-2022 走看看