Android生成二维码的工具类

本文介绍了一个快速简单的二维码生成工具类,支持自定义二维码尺寸及添加Logo,并提供了详细的实现代码。

这是个非常快速简单的工具类,直接使用就可以了。还需要Zxing.jar包

package com.maobang.imsdk.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * 生成二维码的工具类
 */
public class QRCodeUtil {
    /**
     * 生成二维码Bitmap
     *
     * @param content   内容
     * @param widthPix  图片宽度
     * @param heightPix 图片高度
     * @param logoBm    二维码中心的Logo图标(可以为null)
     * @param filePath  用于存储二维码图片的文件路径
     * @return 生成二维码及保存文件是否成功
     */
    public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath) {
        Bitmap bitmap = null;
        FileOutputStream stream = null;
        try {
            if (content == null || "".equals(content)) {
                return false;
            }

            //配置参数
            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //容错级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //设置空白边距的宽度
//            hints.put(EncodeHintType.MARGIN, 2); //default is 4

            // 图像数据转换,使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
            int[] pixels = new int[widthPix * heightPix];
            // 下面这里按照二维码的算法,逐个生成二维码的图片,
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < heightPix; y++) {
                for (int x = 0; x < widthPix; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * widthPix + x] = 0xff000000;
                    } else {
                        pixels[y * widthPix + x] = 0xffffffff;
                    }
                }
            }

            // 生成二维码图片的格式,使用ARGB_8888
            bitmap =  Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);

            if (logoBm != null) {
                bitmap = addLogo(bitmap, logoBm);
            }
            stream = new FileOutputStream(filePath);
            boolean compressed = bitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
                    //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!

            return bitmap != null && compressed;
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            bitmap.recycle();
            bitmap = null;
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if(bitmap!=null)
            {
                bitmap.recycle();
            }
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        return false;
    }

    /**
     * 在二维码中间添加Logo图案
     */
    private static Bitmap addLogo(Bitmap src, Bitmap logo) {
        if (src == null) {
            return null;
        }

        if (logo == null) {
            return src;
        }

        //获取图片的宽高
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();

        if (srcWidth == 0 || srcHeight == 0) {
            return null;
        }

        if (logoWidth == 0 || logoHeight == 0) {
            return src;
        }

        //logo大小为二维码整体大小的1/5
        float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(src, 0, 0, null);
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
            canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();
        } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
        }

        return bitmap;
    }
}


1、安装licensemanager,问license时,单击cancel,然后finish,重启计算机;2、安装cadence的product;3、在任务管理器中结束掉cdsNameServer.exe和cdsMsgServer.exe(可能只有一个,也肯能没有,没有就直接关掉任务管理器即可);4、打开开始---Cadence---License Manager---LmTools软件中Start/Stop/Reread标签,单击Stop按钮,关掉LmTOOLs里面的cadence LIC服务(如果Start/Stop/Reread文本框内没有License文件,无需单击Stop按钮)。5、注意运行破解之前要把C:\Cadence\SPB_16.6\tools\pspice目录下的orSimSetup.dll剪切到C:\Cadence目录下, 否则,破解后是不能进行仿真设置的(他人曰);6、把LicenseManager文件夹下pubkey、LicenseManager.cmd、cdslmd.exe和pubkey.exe放到C:\Cadence\LicenseManager目录下,并替换文件夹下同名的文件,运行LicenseManagerPubkey.cmd;7、把破解压缩包中SPB_16.6\tools文件夹下的pubkey、pubkey.exe和Tools.cmd放到C:Cadence\SPB_16.5\tools目录下并运行Tools.cmd。如果Tools.cmd运行的时候提示pubkey命令窗口出现任何can not open …… 或者是 diff pubkey等字样,说明相应的工具就不能破解。8、打开LicGen文件夹,只能把src.lic中第一行中的host改为你的计算机名称,其它一定不能变,双击LicGen.cmd生成新的license.lic,把生成的license.lic拷贝到C:\Cadence\SPB_16.6目录下(位置随便放);9、打开开始---Cadence---License Manager---License Server Configuration Utility指定生成的license.lic位置,一路单击Next,直到结束;10、在电脑开始菜单中的程序里找到Licenseclient configuration Unility,点击运行后,不用填什么直接点next,最后点finish 11、打开开始---Cadence---License Manager---LmTools软件在弹出的窗口里找到Config Services项,在Path to the license file项的那一行里,点Browes指向刚才新生成的license.lic,之后再点Save Service。在Start/Stop/Reread标签,单击Start按钮,启动服务。该步可以不需要执行,直接打开Cadence对应的软件,也能自动运行。12、破解完成。13、别忘了把刚才剪切到别处去的orsimsetup.dll粘贴回安装路径的cadence\SPB_16.6\tools\pspice目录下 到此,破解完成。不必重启电脑就可运行程序了.此修正16.6版本首发 感谢原破解作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值