use of com.google.zxing.qrcode.QRCodeWriter in project cas by apereo.
the class QRUtils method generateQRCode.
/**
* Generate qr code.
*
* @param stream the stream
* @param key the key
* @param width the width
* @param height the height
*/
@SneakyThrows
public static void generateQRCode(final OutputStream stream, final String key, final int width, final int height) {
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, width, height, hintMap);
final int byteMatrixWidth = byteMatrix.getWidth();
final BufferedImage image = new BufferedImage(byteMatrixWidth, byteMatrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
@Cleanup("dispose") final Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, byteMatrixWidth, byteMatrixWidth);
graphics.setColor(Color.BLACK);
IntStream.range(0, byteMatrixWidth).forEach(i -> IntStream.range(0, byteMatrixWidth).filter(j -> byteMatrix.get(i, j)).forEach(j -> graphics.fillRect(i, j, 1, 1)));
ImageIO.write(image, "png", stream);
}
use of com.google.zxing.qrcode.QRCodeWriter in project run-wallet-android by runplay.
the class QR method generateImage.
public static void generateImage(final String text, final ImageView showView, Activity activity) {
if (text.trim().isEmpty()) {
// alert("Ketik dulu data yang ingin dibuat QR Code");
return;
}
// endEditing();
// showLoadingVisible(true);
new Thread(new Runnable() {
@Override
public void run() {
int size = showView.getMeasuredWidth();
if (size > 1) {
// Log.e("QR", "size is set manually");
size = 260;
}
Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
try {
BitMatrix byteMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);
int height = byteMatrix.getHeight();
int width = byteMatrix.getWidth();
final Bitmap qrImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
qrImage.setPixel(x, y, byteMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
showView.setImageBitmap(qrImage);
// activity.showImage(self.qrImage);
// self.showLoadingVisible(false);
// self.snackbar("QRCode telah dibuat");
}
});
} catch (WriterException e) {
Log.e("QR.ex", "" + e.getMessage());
}
}
}).start();
}
use of com.google.zxing.qrcode.QRCodeWriter in project EnableHands by LeviWGG.
the class QRCodeUtil method createQRCodeBitmap.
/**
* 创建二维码位图 (支持自定义配置和自定义样式)
*
* @param content 字符串内容
* @param width 位图宽度,要求>=0(单位:px)
* @param height 位图高度,要求>=0(单位:px)
* @param character_set 字符集/字符转码格式 (支持格式:{@link Character SetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
* @param error_correction 容错级别 (支持级别:{@link Error CorrectionLevel })。传null时,zxing源码默认使用 "L"
* @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
* @param color_black 黑色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height, @Nullable String character_set, @Nullable String error_correction, @Nullable String margin, @ColorInt int color_black, @ColorInt int color_white) {
/**
* 1.参数合法性判断
*/
if (TextUtils.isEmpty(content)) {
// 字符串内容判空
return null;
}
if (width < 0 || height < 0) {
// 宽和高都需要>=0
return null;
}
try {
/**
* 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象
*/
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
if (!TextUtils.isEmpty(character_set)) {
// 字符转码格式设置
hints.put(EncodeHintType.CHARACTER_SET, character_set);
}
if (!TextUtils.isEmpty(error_correction)) {
// 容错级别设置
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction);
}
if (!TextUtils.isEmpty(margin)) {
// 空白边距设置
hints.put(EncodeHintType.MARGIN, margin);
}
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
/**
* 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值
*/
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y)) {
// 黑色色块像素设置
pixels[y * width + x] = color_black;
} else {
// 白色色块像素设置
pixels[y * width + x] = color_white;
}
}
}
/**
* 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象
*/
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
use of com.google.zxing.qrcode.QRCodeWriter in project smartmodule by carozhu.
the class GenerateQrcodePIcHelper method createQRCode.
/**
* 生成二维码
*
* @param text 需要生成二维码的文字、网址等
* @param size 需要生成二维码的大小()
* @return bitmap
*/
public static Bitmap createQRCode(String text, int size) {
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
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;
}
}
use of com.google.zxing.qrcode.QRCodeWriter in project ring-client-android by savoirfairelinux.
the class QRCodeUtils method encodeStringAsQRCodeData.
/**
* @param input uri to be displayed
* @return the resulting data
*/
public static QRCodeData encodeStringAsQRCodeData(String input) {
if (input == null || input.isEmpty()) {
return null;
}
QRCodeWriter qrWriter = new QRCodeWriter();
BitMatrix qrImageMatrix;
try {
HashMap<EncodeHintType, Integer> hints = new HashMap<>();
hints.put(EncodeHintType.MARGIN, QRCODE_IMAGE_PADDING);
qrImageMatrix = qrWriter.encode(input, BarcodeFormat.QR_CODE, QRCODE_IMAGE_SIZE, QRCODE_IMAGE_SIZE, hints);
} catch (WriterException e) {
Log.e(TAG, "Error while encoding QR", e);
return null;
}
int qrImageWidth = qrImageMatrix.getWidth();
int qrImageHeight = qrImageMatrix.getHeight();
int[] pixels = new int[qrImageWidth * qrImageHeight];
final int BLACK = 0xFF000000;
final int WHITE = 0xFFFFFFFF;
for (int row = 0; row < qrImageHeight; row++) {
int offset = row * qrImageWidth;
for (int column = 0; column < qrImageWidth; column++) {
pixels[offset + column] = qrImageMatrix.get(column, row) ? BLACK : WHITE;
}
}
return new QRCodeData(pixels, qrImageWidth, qrImageHeight);
}
Aggregations