All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.utils.QRCodeUtils Maven / Gradle / Ivy

There is a newer version: 1.189
Show newest version
package com.clickntap.utils;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtils {

  public static String svg(String payload) throws Exception {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    Map hints = new HashMap<>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
    BitMatrix bitMatrix = qrCodeWriter.encode(payload, BarcodeFormat.QR_CODE, 0, 0, hints);
    StringBuilder sbPath = new StringBuilder();
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BitArray row = new BitArray(width);
    for (int y = 0; y < height; y++) {
      row = bitMatrix.getRow(y, row);
      for (int x = 0; x < width; x++) {
        if (row.get(x)) {
          sbPath.append("M").append(x).append(',').append(y).append("h1v1h-1z");
        }
      }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("\n");
    sb.append("\n");
    sb.append("\n");
    sb.append("\n");
    return sb.toString();
  }

  public static void main(String[] args) throws Exception {
    FileUtils.write(new File("qrcode.svg"), QRCodeUtils.svg("https://www.clickntap.com"), ConstUtils.UTF_8);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy