use of com.google.zxing.qrcode.QRCodeWriter in project Conversations by siacs.
the class BarcodeProvider method create2dBarcodeBitmap.
public static Bitmap create2dBarcodeBitmap(String input, int size) {
try {
final QRCodeWriter barcodeWriter = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final Exception e) {
e.printStackTrace();
return null;
}
}
use of com.google.zxing.qrcode.QRCodeWriter in project cas by apereo.
the class OneTimeTokenQRGeneratorController method generateQRCode.
private static void generateQRCode(final OutputStream stream, final String key) {
try {
final Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
hintMap.put(EncodeHintType.MARGIN, 2);
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
final QRCodeWriter qrCodeWriter = new QRCodeWriter();
final BitMatrix byteMatrix = qrCodeWriter.encode(key, BarcodeFormat.QR_CODE, 250, 250, hintMap);
final int width = byteMatrix.getWidth();
final BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
final Graphics2D graphics = (Graphics2D) image.getGraphics();
try {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, width);
graphics.setColor(Color.BLACK);
IntStream.range(0, width).forEach(i -> {
IntStream.range(0, width).filter(j -> byteMatrix.get(i, j)).forEach(j -> graphics.fillRect(i, j, 1, 1));
});
} finally {
graphics.dispose();
}
ImageIO.write(image, "png", stream);
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
use of com.google.zxing.qrcode.QRCodeWriter in project collect by opendatakit.
the class QRCodeUtils method generateQRBitMap.
public static Bitmap generateQRBitMap(String data, int sideLength) throws IOException, WriterException {
final long time = System.currentTimeMillis();
String compressedData = CompressionUtils.compress(data);
// Maximum capacity for QR Codes is 4,296 characters (Alphanumeric)
if (compressedData.length() > 4000) {
throw new IOException(Collect.getInstance().getString(R.string.encoding_max_limit));
}
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(compressedData, BarcodeFormat.QR_CODE, sideLength, sideLength, hints);
Bitmap bmp = Bitmap.createBitmap(sideLength, sideLength, Bitmap.Config.RGB_565);
for (int x = 0; x < sideLength; x++) {
for (int y = 0; y < sideLength; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
Timber.i("QR Code generation took : %d ms", (System.currentTimeMillis() - time));
return bmp;
}
use of com.google.zxing.qrcode.QRCodeWriter in project pancm_project by xuwujing.
the class QrCodeCreateUtil method createQrCode.
/**
* 生成包含字符串信息的二维码图片
*
* @param outputStream 文件输出流路径
* @param content 二维码携带信息
* @param qrCodeSize 二维码图片大小
* @param imageFormat 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException {
// 设置二维码纠错级别MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
// 矫错级别
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth - 200, matrixWidth - 200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i - 100, j - 100, 1, 1);
}
}
}
return ImageIO.write(image, imageFormat, outputStream);
}
use of com.google.zxing.qrcode.QRCodeWriter in project smartmodule by carozhu.
the class GenerateQrcodePIcHelper method createQRCodeWithLogo.
/**
* 生成带logo的二维码,logo默认为二维码的1/5
*
* @param text 需要生成二维码的文字、网址等
* @param size 需要生成二维码的大小()
* @param mBitmap logo文件
* @return bitmap
*/
public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap mBitmap) {
try {
IMAGE_HALFWIDTH = size / 10;
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
/*
* 设置容错级别,默认为ErrorCorrectionLevel.L
* 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
//矩阵高度
int width = bitMatrix.getWidth();
//矩阵宽度
int height = bitMatrix.getHeight();
int halfW = width / 2;
int halfH = height / 2;
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
m.setScale(sx, sy);
//设置缩放信息
//将logo图片按martix设置的信息缩放
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
//该位置用于存放图片信息
//记录图片每个像素信息
pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
} else {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
//灰色-- 0xEEEEEEEE md_grey_200
pixels[y * size + x] = 0xFFFFFFFF;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
Aggregations