Java Code Examples for com.google.zxing.qrcode.encoder.Encoder#encode()

The following examples show how to use com.google.zxing.qrcode.encoder.Encoder#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DouYuBroadcastService.java    From Alice-LiveMan with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public InputStream getBroadcastCaptcha() throws IOException {
    Map<String, String> requestProperties = new HashMap<>();
    requestProperties.put("referer", "https://passport.douyu.com/index/login?passport_reg_callback=PASSPORT_REG_SUCCESS_CALLBACK&passport_login_callback=PASSPORT_LOGIN_SUCCESS_CALLBACK&passport_close_callback=PASSPORT_CLOSE_CALLBACK&passport_dp_callback=PASSPORT_DP_CALLBACK&type=login&client_id=1&state=https%3A%2F%2Fwww.douyu.com%2F&source=click_topnavi_login");
    requestProperties.put("x-requested-with", "XMLHttpRequest");
    String generateCodeJSON = HttpRequestUtil.downloadUrl(URI.create(URL_GENERATE_CODE), null, "client_id=1", requestProperties, StandardCharsets.UTF_8);
    JSONObject generateCode = JSON.parseObject(generateCodeJSON);
    if (generateCode.getInteger("error") == 0) {
        String url = generateCode.getJSONObject("data").getString("url");
        String code = generateCode.getJSONObject("data").getString("code");
        session.setAttribute(SESSION_ATTRIBUTE, code);
        try {
            QRCode qrCode = Encoder.encode(url, ErrorCorrectionLevel.M);
            BufferedImage qrCodeImage = new BufferedImage(qrCode.getMatrix().getWidth(), qrCode.getMatrix().getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            for (int x = 0; x < qrCodeImage.getWidth(); x++) {
                for (int y = 0; y < qrCodeImage.getHeight(); y++) {
                    if (qrCode.getMatrix().get(x, y) == 0) {
                        qrCodeImage.setRGB(x, y, Color.WHITE.getRGB());
                    }
                }
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(qrCodeImage, "bmp", bos);
            return new ByteArrayInputStream(bos.toByteArray());
        } catch (WriterException e) {
            throw new IOException(e);
        }
    } else {
        log.error("获取qrcode失败:" + generateCodeJSON);
    }
    return null;
}
 
Example 2
Source File: QRCodeWriter.java    From MyBox with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
        BarcodeFormat format,
        int width,
        int height,
        Map<EncodeHintType, ?> hints) throws WriterException {

    if (contents.isEmpty()) {
        throw new IllegalArgumentException("Found empty contents");
    }

    if (format != BarcodeFormat.QR_CODE) {
        throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
    }

    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x'
                + height);
    }

    ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
    int quietZone = QUIET_ZONE_SIZE;
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
            errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
        }
        if (hints.containsKey(EncodeHintType.MARGIN)) {
            quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
        }
    }

    code = Encoder.encode(contents, errorCorrectionLevel, hints);
    return renderResult(width, height, quietZone);
}
 
Example 3
Source File: QRCodeWriter.java    From ScreenCapture with MIT License 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 4
Source File: QRCodeWriter.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
public BitMatrix encode(String contents, int width, int height, Map<EncodeHintType, ?> hints)
        throws WriterException {

    if (contents.isEmpty()) {
        throw new IllegalArgumentException("Found empty contents");
    }

    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    }

    ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
    int quietZone = QUIET_ZONE_SIZE;
    if (hints != null) {
        ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
        if (requestedECLevel != null) {
            errorCorrectionLevel = requestedECLevel;
        }
        Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
        if (quietZoneInt != null) {
            quietZone = quietZoneInt;
        }
    }

    QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
    return renderResult(code, width, height, quietZone);
}
 
Example 5
Source File: QRCodeWriter.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 6
Source File: QRCodeWriter.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 7
Source File: QRCodeWriter.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
    if (requestedECLevel != null) {
      errorCorrectionLevel = requestedECLevel;
    }
    Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
    if (quietZoneInt != null) {
      quietZone = quietZoneInt;
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 8
Source File: QRCodeWriter.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 9
Source File: QRCodeWriter.java    From weex with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 10
Source File: QRCodeWriter.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 11
Source File: QRCodeWriter.java    From reacteu-app with MIT License 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.length() == 0) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
    if (requestedECLevel != null) {
      errorCorrectionLevel = requestedECLevel;
    }
    Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
    if (quietZoneInt != null) {
      quietZone = quietZoneInt;
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
Example 12
Source File: SendCodeGenerator.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * This object renders a QR Code as a ByteMatrix 2D array of greyscale
 * values.
 * 
 * @author [email protected] (Daniel Switkin)
 */
public ByteMatrix encode(String contents) throws WriterException {

	if (contents == null || contents.length() == 0) {
		throw new IllegalArgumentException("Found empty contents");
	}

	Encoder.encode(contents, ErrorCorrectionLevel.L);
	return renderResult(code, QR_CODE_ELEMENT_MULTIPLE);
}
 
Example 13
Source File: QRCodeWriter.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints)
		throws WriterException {

	if (contents.isEmpty()) {
		throw new IllegalArgumentException("Found empty contents");
	}

	if (format != BarcodeFormat.QR_CODE) {
		throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
	}

	if (width < 0 || height < 0) {
		throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
	}

	ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
	int quietZone = QUIET_ZONE_SIZE;
	if (hints != null) {
		ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
		if (requestedECLevel != null) {
			errorCorrectionLevel = requestedECLevel;
		}
		Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
		if (quietZoneInt != null) {
			quietZone = quietZoneInt;
		}
	}

	QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
	return renderResult(code, width, height, quietZone);
}
 
Example 14
Source File: PrinterCommands.java    From PrinterThermal-ESCPOS-Android with MIT License 4 votes vote down vote up
/**
 * Convert a string to QR Code byte array compatible with ESC/POS printer.
 *
 * @param data String data to convert in QR Code
 * @return Bytes contain the image in ESC/POS command
 */
public static byte[] QRCodeDataToBytes(String data, int size) {

    ByteMatrix byteMatrix = null;

    try {
        EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCode code = Encoder.encode(data, ErrorCorrectionLevel.L, hints);
        byteMatrix = code.getMatrix();

    } catch (WriterException e) {
        e.printStackTrace();
    }

    if (byteMatrix == null) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    int
            width = byteMatrix.getWidth(),
            height = byteMatrix.getHeight(),
            coefficient = Math.round((float) size / (float) width),
            imageWidth = width * coefficient,
            imageHeight = height * coefficient,
            bytesByLine = (int) Math.ceil(((float) imageWidth) / 8f),
            i = 8;

    if (coefficient < 1) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    byte[] imageBytes = PrinterCommands.initImageCommand(bytesByLine, imageHeight);

    for (int y = 0; y < height; y++) {
        byte[] lineBytes = new byte[bytesByLine];
        int j = 0, multipleX = coefficient;
        boolean isBlack = false;
        for (int x = -1; x < width;) {
            StringBuilder stringBinary = new StringBuilder();
            for (int k = 0; k < 8; k++) {
                if(multipleX == coefficient) {
                    isBlack = ++x < width && byteMatrix.get(x, y) == 1;
                    multipleX = 0;
                }
                stringBinary.append(isBlack ? "1" : "0");
                ++multipleX;
            }
            lineBytes[j++] = (byte) Integer.parseInt(stringBinary.toString(), 2);
        }

        for (int multipleY = 0; multipleY < coefficient; ++multipleY) {
            System.arraycopy(lineBytes, 0, imageBytes, i, lineBytes.length);
            i += lineBytes.length;
        }
    }

    return imageBytes;
}
 
Example 15
Source File: QRCodeGenerator.java    From bither-desktop-java with Apache License 2.0 3 votes vote down vote up
public static ByteMatrix encode(String contents) throws WriterException {

        if (contents == null || contents.length() == 0) {
            throw new IllegalArgumentException("Found empty contents");
        }

        QRCode code = Encoder.encode(contents, ErrorCorrectionLevel.L);

        return renderResult(code, QR_CODE_ELEMENT_MULTIPLE);
    }
 
Example 16
Source File: QRCodeGenerator.java    From bither-desktop-java with Apache License 2.0 3 votes vote down vote up
public static ByteMatrix encode(String contents) throws WriterException {

        if (contents == null || contents.length() == 0) {
            throw new IllegalArgumentException("Found empty contents");
        }

        QRCode code = Encoder.encode(contents, ErrorCorrectionLevel.L);

        return renderResult(code, QR_CODE_ELEMENT_MULTIPLE);
    }