Create a QR Code and show it in ImageView - android

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 !!

Related

How to create a contact with a QR in Android?

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

zxing android qrcode generator

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

How to generate a QR code that redirects to a URL?

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

How to save a run-time-created bitmap in Android?

In my application a user can upload some pictures and for database size limits I would like to merge these images to create only one image, I do that with:
private Bitmap combineImageIntoOne(ArrayList<Bitmap> bitmap) {
int w = 0, h = 0;
for (int i = 0; i < bitmap.size(); i++) {
if (i < bitmap.size() - 1) {
w = bitmap.get(i).getWidth() > bitmap.get(i + 1).getWidth() ? bitmap.get(i).getWidth() : bitmap.get(i + 1).getWidth();
}
h += bitmap.get(i).getHeight();
}
Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
int top = 0;
for (int i = 0; i < bitmap.size(); i++) {
Log.d("HTML", "Combine: "+i+"/"+bitmap.size()+1);
top = (i == 0 ? 0 : top+bitmap.get(i).getHeight());
canvas.drawBitmap(bitmap.get(i), 0f, top, null);
}
return temp;
}
Bitmap.compress is the method you are looking for - it allows you to save Bitmap to any OutputStream you want.
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(someFile));

How to convert 2d int array to bitmap in android

I need to convert a 2d integer array (subSrc) to a bitmap. Any solutions?
private Bitmap decimation(Bitmap src){
Bitmap dest = Bitmap.createBitmap(
src.getWidth(), src.getHeight(), src.getConfig());
int bmWidth = src.getWidth();
int bmHeight = src.getHeight();`enter code here`
int[][] subSrc = new int[bmWidth/2][bmWidth/2];
for(int k = 0; k < bmWidth-2; k++){
for(int l = 0; l < bmHeight-2; l++){
subSrc[k][l] = src.getPixel(2*k, 2*l); <---- ??
I looked for a method that received an 2d array (int[][]) and created a Bitmap, and found none, so I wrote one myself:
public static Bitmap bitmapFromArray(int[][] pixels2d){
int width = pixels2d.length;
int height = pixels2d[0].length;
int[] pixels = new int[width * height];
int pixelsIndex = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
pixels[pixelsIndex] = pixels2d[i][j];
pixelsIndex ++;
}
}
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}
I also wrote a reverse method:
public static int[][] arrayFromBitmap(Bitmap source){
int width = source.getWidth();
int height = source.getHeight();
int[][] result = new int[width][height];
int[] pixels = new int[width*height];
source.getPixels(pixels, 0, width, 0, 0, width, height);
int pixelsIndex = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i][j] = pixels[pixelsIndex];
pixelsIndex++;
}
}
return result;
}
I hope you find it useful!
you can use
setPixel(int, int, int) or setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height) methos od bitmap class.
Bitmap dest = Bitmap.createBitmap(
src.getWidth()/2, src.getHeight()/2, src.getConfig());
int bmWidth = src.getWidth();
int bmHeight = src.getHeight();
for(int k = 0; k < bmWidth/2; k++){
for(int l = 0; l < bmHeight/2; l++){
dest.setPixel(k,l,src.getPixel(2*k, 2*l));
But this will be slower i think.
for the 2nd method you uhave to do something like this
int subSrc = new int[(bmWidth/2*)(bmHeight/2)];
for(int k = 0; k < bmWidth-2; k++){
subSrc[k] = src.getPixel(2*(k/bmWidth), 2*(k%bmHeight)); <---- ??
So, you are essentially trying to pull pixels out, do something to them, then make a Bitmap as a result?
The routines expect the pixels to be in a single-dimensional array, so you'll want to put them into the array more like this:
int data[] = new int[size];
data[x + width*y] = pixel(x,y);
...
Then use Bitmap.createBitmap() that accepts the single-dimensional array. You'll want to use the Bitmap.Config for ARGB in your example, since you're using b.getPixel(x,y) which always returns a color in ARGB format.
Bitmap result = Bitmap.createBitmap(data, width, height, Bitmap.Config.ARGB_8888);

Categories

Resources