生活随笔
收集整理的這篇文章主要介紹了
关于Zxing生成DM二维码变形问题总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Zxing生成DM碼不是正方形的問題
在hints中增加
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);//設置樣式:不設置,正方形,矩形
SymbolShapeHint.FORCE_NONE 默認
SymbolShapeHint.FORCE_SQUARE 正方形
SymbolShapeHint.FORCE_RECTANGLE 矩形
/*** @param content 二維碼內容* @param width 二維碼寬度* @param height 二維碼高度* @param charset 字符編碼* @param barcodeFormat 二維碼類型QR、DM、條形碼* @param errorCorrectionLevel 容錯級別* @param margin 邊距* @param color 二維碼顏色 16進制 例如:#ff000000* @param colorBg 背景色 16進制 例如:#ffffffff* @return*/public static Bitmap createImage(String content, int width, int height, String charset,BarcodeFormat barcodeFormat,ErrorCorrectionLevel errorCorrectionLevel,int margin, int color, int colorBg) {try {//判斷URL合法性if (content == null || "".equals(content) || content.length() < 1) {return null;}Hashtable<EncodeHintType, Object> hints = new Hashtable<>();hints.put(EncodeHintType.CHARACTER_SET, charset);//編碼格式hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);//容錯類型hints.put(EncodeHintType.MARGIN, margin);//邊距hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);//設置樣式:不設置,正方形,矩形//圖像數據轉換,使用了矩陣轉換BitMatrix bitMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);int[] pixels = new int[width * height];//下面這里按照二維碼的算法,逐個生成二維碼的圖片,//兩個for循環是圖片橫列掃描的結果for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (bitMatrix.get(x, y)) {pixels[y * width + x] = color;} else {pixels[y * height + x] = colorBg;}}}//生成二維碼圖片的格式,使用ARGB_8888Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;} catch (WriterException e) {e.printStackTrace();return null;}}
調用方法
createImage(content, width, height, "utf-8",BarcodeFormat.DATA_MATRIX, errorCorrectionLevel, margin, 0XFF000000, 0XFFFFFFFF);
總結
以上是生活随笔為你收集整理的关于Zxing生成DM二维码变形问题总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。