I am trying to create a QR that, when read by another device, appears as creating a new Contact.
To create the QR, I have tried the class
QRCodeWriter and BitMatrix.
I leave you the code:
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
image.setImageBitmap(bmp);
} catch (WriterException e) {
e.printStackTrace();
}
}
And I would like that when reading the QR, something similar to this would come out
Related
i included image of my stacktrace
MultiFormatWriter multiFormatWriter = new
MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(uid,
BarcodeFormat.QR_CODE,200,200);----55
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
imageView.setImageBitmap(Bitmap.createBitmap(bitmap));
} catch (WriterException e) {
e.printStackTrace();
}
im getting a null pointer exeption error at this point 55 .I used zxing library but im unable to perform required operation please help me
Try this
public static Bitmap encodeStringToBitmap(String contents) throws WriterException {
//Null check, just b/c
if (contents == null) {
return null;
}
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, BarcodeFormat.PDF_417, 700, 900, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
For more trial
visit Java Code Examples for com.google.zxing.MultiFormatWriter
I've been working with a QR generator app, and it works fine with me.
My question is, how to generate a QR code that will redirect to a website after being scanned by a QR reader?
This is my QR generator code which generates only simple text:
Bitmap getQRCode(Context context, String type, String value) throws WriterException {
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(value, BarcodeFormat.DATA_MATRIX.QR_CODE, QR_DIM, QR_DIM, null);
} catch (IllegalArgumentException e) {
return null;
}
int bitMatrixHeight = bitMatrix.getHeight();
int bitMatrixWidth = bitMatrix.getWidth();
int pixels[] = new int[bitMatrixHeight * bitMatrixWidth];
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ?
context.getResources().getColor(R.color.QRCodeBlackColor):context.getResources().getColor(R.color.QRCodeWhiteColor);
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
return bitmap;
}
Thanks you guys.
Use
https://github.com/kenglxn/QRGen then
you can read the QR code and open a web view using the result.
I haven't tried it yet but I hope this helps
I'm using Zxing library to generate a barcode bitmap from a String numeric code with this code:
public Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
private String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
It works well but it only generates the barcode in the image, no numbers within it. The problem comes when I want to paint it in an ImageView with the numbers below as we usually see them in barcodes. I tried with a textview but they are too small or too big, depending on the screen and the ImageView size.
The question is, is there a way of including code numbers below barcode in the Bitmap with the Zxing library? Or is there any other easy solution as resizable text in TextView or sth?
Thanks in advance!
I am creating an app which will be able to scan QR code and create QR code. The scanning part is done and its working fine. But when I try to create the QR code and show it in an ImageView the QR code created is not containing correct text. I am using ZXING library.
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeEncoder = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeEncoder.encode(myText, BarcodeFormat.QR_CODE,
200, 200, hintMap);
height = bitMatrix.getHeight();
width = bitMatrix.getWidth();
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (x = 0; x < width; x++){
bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
}
ImageView myImage = (ImageView) findViewById(R.id.qr_code);
myImage.setImageBitmap(bmp);
The error is in the for loop. You missed a inner for loop. But how come you were getting a blank image!
Use below snippet.
for (x = 0; x < width; x++){
for (y = 0; y < height; y++){
bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
}
}
This should work.
Try out full code:
com.google.zxing.Writer writer = new QRCodeWriter();
// String finaldata = Uri.encode(data, "utf-8");
int width = 250;
int height = 250;
BitMatrix bm = writer
.encode(data, BarcodeFormat.QR_CODE, width, height);
Bitmap ImageBitmap = Bitmap.createBitmap(width, height,
Config.ARGB_8888);
for (int i = 0; i < width; i++) {// width
for (int j = 0; j < height; j++) {// height
ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK
: Color.WHITE);
}
}
Works !!
I'm modifying a two-dimensional code project which is based on an open source project zxing, but When I input Chinese characters, I found the results which get from the other two-dimensional code software always with "] Q2 \ 000026" prefix. I cann't understand what it means. Used zxing-2.1 version. Here is the project link http://code.google.com/p/zxing/.
My project code is:
#Override
public void run() {
// TODO Auto-generated method stub
try {
mTextContent = chinese test 中文测试";
Bitmap bitmap = Create2DCode(mTextContent);
return;
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Bitmap Create2DCode(String str) throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
hints.put(EncodeHintType.CHARACTER_SET, "GBK");//UTF-8
// hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//It's the same result.
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 500, 500, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 0xffffffff;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
Can anybody give me some help?
I think the QR software you used may not support it. because I ever used zxing lib and wrote the code as below. Chinese can be recognized by the app named "wochacha", but not by "wechat"
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 400, 400, hints);