只有生成二维码的代码
优点是占用方法数比较少 以防65535
public class QRcodeUtil { static Paint paint; static { paint = new Paint(); // 设置一个笔刷大小是3的黄色的画笔 paint.setColor(Color.BLACK); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(3); } private static final int PIC_SIZE = 300; private static final int RECT_SIZE = 6; /** * 生成二维码(QRCode)图片 * * @param content * @param */ public Bitmap encoderQRCode(String content) { Canvas g; try { Qrcode qrcodeHandler = new Qrcode(); qrcodeHandler.setQrcodeErrorCorrect(‘M‘); qrcodeHandler.setQrcodeEncodeMode(‘B‘); qrcodeHandler.setQrcodeVersion(8); byte[] contentBytes = content.getBytes("utf-8"); Bitmap bufferBitMap = Bitmap.createBitmap(PIC_SIZE, PIC_SIZE, Bitmap.Config.ARGB_8888); g = new Canvas(bufferBitMap); g.drawColor(Color.WHITE); // 设置偏移量 不设置可能导致解析出错 int pixoff = 2; // 输出内容 > 二维码 if (contentBytes.length > 0 && contentBytes.length < 512 ) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { float left = j * RECT_SIZE + pixoff; float top = i * RECT_SIZE + pixoff; float right = (j + 1) * RECT_SIZE + pixoff; float bottom = (i + 1) * RECT_SIZE + pixoff; g.drawRect(left, top, right, bottom, paint); } } } } else { Log.e("QRCodeEncoderHandler", "QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. "); } return bufferBitMap; } catch (Exception e) { e.printStackTrace(); return null; } } }
时间: 2024-11-05 07:07:58