use of com.google.zxing.qrcode.QRCodeWriter in project jfinal by jfinal.
the class QrCodeRender method render.
public void render() {
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//去掉白色边框,极度重要,否则二维码周围的白边会很宽
hints.put(EncodeHintType.MARGIN, 0);
if (errorCorrectionLevel != null) {
hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
}
try {
// MultiFormatWriter 可支持多种格式的条形码,在此直接使用 QRCodeWriter,通过查看源码可知少创建一个对象
// BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 经测试 200 X 200 大小的二维码使用 "png" 格式只有 412B,而 "jpg" 却达到 15KB
// format: "jpg"、"png"
MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
} catch (Exception e) {
throw new RenderException(e);
}
}
use of com.google.zxing.qrcode.QRCodeWriter in project Signal-Android by WhisperSystems.
the class QrCode method create.
@NonNull
public static Bitmap create(String data) {
try {
BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
if (result.get(x, y)) {
bitmap.setPixel(x, y, Color.BLACK);
}
}
}
return bitmap;
} catch (WriterException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
}
}
use of com.google.zxing.qrcode.QRCodeWriter in project KeyBox by skavanagh.
the class OTPAction method qrImage.
@Action(value = "/admin/qrImage")
public String qrImage() {
String username = UserDB.getUser(AuthUtil.getUserId(servletRequest.getSession())).getUsername();
String secret = AuthUtil.getOTPSecret(servletRequest.getSession());
AuthUtil.setOTPSecret(servletRequest.getSession(), null);
try {
String qrCodeText = "otpauth://totp/KeyBox%20%28" + URLEncoder.encode(servletRequest.getHeader("host").replaceAll("\\:.*$", ""), "utf-8") + "%29:" + username + "?secret=" + secret;
QRCodeWriter qrWriter = new QRCodeWriter();
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix matrix = qrWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, hints);
servletResponse.setContentType("image/png");
BufferedImage image = new BufferedImage(QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT);
graphics.setColor(Color.BLACK);
for (int x = 0; x < QR_IMAGE_WIDTH; x++) {
for (int y = 0; y < QR_IMAGE_HEIGHT; y++) {
if (matrix.get(x, y)) {
graphics.fillRect(x, y, 1, 1);
}
}
}
ImageIO.write(image, "png", servletResponse.getOutputStream());
servletResponse.getOutputStream().flush();
servletResponse.getOutputStream().close();
} catch (Exception ex) {
log.error(ex.toString(), ex);
}
return null;
}
use of com.google.zxing.qrcode.QRCodeWriter in project zxing by zxing.
the class ChartServlet method doEncode.
private static void doEncode(ServletRequest request, HttpServletResponse response, boolean isPost) throws IOException {
ChartServletRequestParameters parameters;
try {
parameters = doParseParameters(request, isPost);
} catch (IllegalArgumentException | NullPointerException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.toString());
return;
}
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.MARGIN, parameters.getMargin());
if (!StandardCharsets.ISO_8859_1.equals(parameters.getOutputEncoding())) {
// Only set if not QR code default
hints.put(EncodeHintType.CHARACTER_SET, parameters.getOutputEncoding().name());
}
hints.put(EncodeHintType.ERROR_CORRECTION, parameters.getEcLevel());
BitMatrix matrix;
try {
matrix = new QRCodeWriter().encode(parameters.getText(), BarcodeFormat.QR_CODE, parameters.getWidth(), parameters.getHeight(), hints);
} catch (WriterException we) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, we.toString());
return;
}
ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(matrix, "PNG", pngOut);
byte[] pngData = pngOut.toByteArray();
response.setContentType("image/png");
response.setContentLength(pngData.length);
response.setHeader("Cache-Control", "public");
response.getOutputStream().write(pngData);
}
use of com.google.zxing.qrcode.QRCodeWriter in project JustAndroid by chinaltz.
the class CodeUtils method createImage.
/**
* 生成二维码图片
* @param text
* @param w
* @param h
* @param logo
* @return
*/
public static Bitmap createImage(String text, int w, int h, Bitmap logo) {
if (TextUtils.isEmpty(text)) {
return null;
}
try {
Bitmap scaleLogo = getScaleLogo(logo, w, h);
int offsetX = w / 2;
int offsetY = h / 2;
int scaleWidth = 0;
int scaleHeight = 0;
if (scaleLogo != null) {
scaleWidth = scaleLogo.getWidth();
scaleHeight = scaleLogo.getHeight();
offsetX = (w - scaleWidth) / 2;
offsetY = (h - scaleHeight) / 2;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容错级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置空白边距的宽度
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, w, h, hints);
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (x >= offsetX && x < offsetX + scaleWidth && y >= offsetY && y < offsetY + scaleHeight) {
int pixel = scaleLogo.getPixel(x - offsetX, y - offsetY);
if (pixel == 0) {
if (bitMatrix.get(x, y)) {
pixel = 0xff000000;
} else {
pixel = 0xffffffff;
}
}
pixels[y * w + x] = pixel;
} else {
if (bitMatrix.get(x, y)) {
pixels[y * w + x] = 0xff000000;
} else {
pixels[y * w + x] = 0xffffffff;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
Aggregations