標籤:
只有產生二維碼的代碼
優點是佔用方法數比較少 以防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;}}}
android使用qrcode_swetake.jar產生二維碼