/** * 字符串转二维码 * * @param str 渠道号 */ private void StringToBarcode(String str) { BitMatrix byteMatrix = null; try { // 如果目录不存在-创建目录 if (!mConfig.getOutPath().exists()) { mConfig.getOutPath().mkdir(); } String change = new String(str.getBytes(mConfig.getCharCode()), mConfig.getCharCode()); // 二维码内容 String content = mConfig.getContentRegex().replace("*", change); // 二维码宽度 int width = mConfig.getWidth(); QRCoder bc = new QRCoder(); // 输出文件 String outPutFile = mConfig.getOutPath().getPath() + File.separator + str + "." + mConfig.getFormate(); bc.createQrCode( content, width, width, outPutFile, mConfig.getLogo(), mConfig.getFormate(), 0xFF00000F, 0xFFFFFFFF, mConfig.getLogoCent()); } catch (Exception e) { e.printStackTrace(); } finally { if (byteMatrix != null) { byteMatrix.clear(); } } }
/* * 去白边 */ public static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; }
/** * Convert the ByteMatrix to BitMatrix. * * @param matrix The input matrix. * @return The output matrix. */ private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { int matrixWidgth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight); output.clear(); for (int i = 0; i < matrixWidgth; i++) { for (int j = 0; j < matrixHeight; j++) { // Zero is white in the bytematrix if (matrix.get(i, j) == 1) { output.set(i, j); } } } return output; }
private static BitMatrix updateBit(BitMatrix matrix, int margin) { int tempM = margin * 2; int[] rec = matrix.getEnclosingRectangle(); // 获取二维码图案的属性 int resWidth = rec[2] + tempM; int resHeight = rec[3] + tempM; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix resMatrix.clear(); for (int i = margin; i < resWidth - margin; i++) { // 循环,将二维码图案绘制到新的bitMatrix中 for (int j = margin; j < resHeight - margin; j++) { if (matrix.get(i - margin + rec[0], j - margin + rec[1])) { resMatrix.set(i, j); } } } return resMatrix; }
public void encode(String str, String path, int width, int height) { try { Map hints = new HashMap(); hints.put(EncodeHintType.MARGIN, 0); // hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix byteMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, width, height, hints); /** tianboalei 2015-1-22 去掉生成二维码时周围的白圈 */ // 1 int[] rec = byteMatrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (byteMatrix.get(i + rec[0], j + rec[1])) { resMatrix.set(i, j); } } } // 2 int widthT = resMatrix.getWidth(); int heightT = resMatrix.getHeight(); BufferedImage image = new BufferedImage(widthT, heightT, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < widthT; x++) { for (int y = 0; y < heightT; y++) { image.setRGB( x, y, resMatrix.get(x, y) == true ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } /** 去掉二维码周围白圈结束 */ File file = new File(path); writeToFile(resMatrix, "png", file); } catch (Exception e) { e.printStackTrace(); } }