android使用qrcode_swetake.jar產生二維碼

來源:互聯網
上載者:User

標籤:

只有產生二維碼的代碼

優點是佔用方法數比較少 以防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產生二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.