I have this method to generate a Barcode bitmap:
public static Bitmap encodeToQrCode(String text, int width, int height) {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(text, BarcodeFormat.QR_CODE, 400, 400);
} catch (WriterException ex) {
ex.printStackTrace();
}
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height - 1; y++) {
bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}
Is there any way to find out that the Barcode has been scanned? I'm using zxing.
If I understand you correctly you're looking for a way to
Create a bitmap that contains a QR code
Display that bitmap on your device
Detect when a different device that doesn't use your software scans that code
This is not possible. The QR code bitmap is just that: A bitmap your app displays. And a barcode scanner app simply takes a photo of your display and tries to find patterns in that bitmap.
Related
Im trying to save my bitmap file as to png to my Internal Storage
How can i do it?
My code to generate QR Code :-
public void createQRCode(String qrCodeData, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) {
Bitmap bitmap = null;
try {
//generating qr code in bitmatrix type
BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
//converting bitmatrix to bitmap
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
qrImageView.setImageBitmap(bitmap);
} catch (Exception er) {
Log.e("QrGenerate", er.getMessage());
}
}
I want to save the generated QR code bitmap to my Internal Storage for example say in DCIM folder i want make a new folder "QR CODES" then i want to save my bitmap as "QRCode1.png"
How can i do this?
i have tried many tutorials and none of them seem to work for me or i am doing it wrong maybe
PS
i have already added the write permission to my AndroidManifest file
Try doing this and then use the data to save it in your local device.
saveImageBitmap(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte[] data = baos.toByteArray();
//do whatever you want to save this bitmap file
}
Hope that'll help you out! This will transform the bitmap into .jpg format and then save it in your file.
I'm currently using the ZXing Library to decode QR Codes. Additional I'm displaying the created QR Code on the screen with JavaFx. I'd like to create QR Codes without quiet zone, so I set the margin to zero. However I realized that when I create a QR Code with Error Correction Level L there is still a small quiet zone. Here is a picture displaying the result.
Picture1
Now the strange thing is the same QR Code with Error Correction Level H has no quiet zone.
Here is my code for creating the QR Codes.
private BufferedImage QRCodeGenerator(String content, int height, int width) {
Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hintMap.put(EncodeHintType.MARGIN, 0);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BufferedImage bufferedImage = null;
try {
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hintMap);
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bufferedImage.createGraphics();
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLACK);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
} catch (WriterException e) {
}
return bufferedImage;
}
Can someone explain this behavior?
"Base64Parts" is a string that has been split to equal parts, and I am trying to generate a QR code for each string and place it in a arraylist so that i can retrieve it and generate a GIF. Am I adding the bitmap images to the arraylist in the correct way? Because i can only retrieve "bmp_images.get(0)". Not others (eg-bmp_images.get(1)). My code is given below.
//Declaring QR code generator
QRCodeWriter writer = new QRCodeWriter();
//Declaring Array
ArrayList<Bitmap> bmp_images = new ArrayList<Bitmap>();
for (int i = 0; i < numberOfPartsSplit; i++){
try {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BitMatrix bitMatrix = writer.encode(Base64Parts.get(i), BarcodeFormat.QR_CODE, 512, 512, hintMap);
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);
}
}
bmp_images.add(i,bmp); //the code added for arraylist of images
((ImageView) findViewById(R.id.image_holder)).setImageBitmap(bmp_images.get(0)); //use different values
} catch (WriterException e) {
e.printStackTrace();
}
((ImageView) findViewById(R.id.image_holder)).setImageBitmap(bmp_images.get(0));
paste this line outside for loop. Once all iteration done, set the needed index to get that particular value.
I made a bitmap class with a list of Pixels. The Pixel class has a position and a color. Now I am trying to get a bitmap from a BufferedImage.
This is the code I have tried:
private static Bitmap fromImage(BufferedImage image) {
Bitmap bitmap = new Bitmap();
for (int x = image.getMinX(); x < image.getWidth(); x++) {
for (int y = image.getMinY(); y < image.getHeight(); y++) {
bitmap.setPixel(new Pixel(RGBAColor.fromColor(
new Color(image.getRGB(x, y))), new Vector2(x, y)));
}
}
return bitmap;
}
public static Bitmap fromImage(String path) {
try {
BufferedImage img = ImageIO.read(Galaxy2D.class.getResource(
"/com/mcmastery/galaxy2d/resources/" + path));
BufferedImage conImg = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_ARGB);
conImg.createGraphics().drawImage(img, 0, 0, null);
return Bitmap.fromImage(conImg);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
It works, but when it paints the image to the screen it distorts it.
Original image: http://gyazo.com/0832ebf26f940b6dbf218e43101e63f8.png
Image painted to screen using bitmap methods: http://gyazo.com/c4b776f0e5fa0e54c167c10947582834.png
I have tried DataBufferInt too, and it still produces the same result. With other images, it just changes some pixel's positions to positions close to where they are supposed to be. But with the crosshair above, it turns it black and distorts it.
Not sure what a Bitmap is used for (I can't find it in the JDK7 API).
Maybe you can just use the getSubImage(...) method of the BufferedImage.
I am generating a QR code with ZXing library in an Android app.
Instead of directly saving it into a file, I want it to go into an ImageView.
Here is my code:
String WIFIQRCODE = "";
String SSID = etSSID.getText().toString();
String PASS = etPASS.getText().toString();
String PASSTYPE = sTYPE.getSelectedItem().toString();
WIFIQRCODE = "WIFI:T:"+PASSTYPE+";S:"+SSID+";P:"+PASS;
//Inform the user the button1 has been clicked
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(WIFIQRCODE, BarcodeFormat.QR_CODE, 300, 300);
File file = new File(context.getFilesDir(), "/sdcard/Images/"+SSID+".png");
MatrixToImageWriter.writeToFile(bitMatrix, "png", file);
} catch (WriterException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
How to modify it, so that it puts it into an ImageView instead of a file?
you will need to get Bitmap from BitMatrix to set directly image in ImageView do it as:
int height = bitMatrix.getHeight();
int width = bitMatrix.getWidth();
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);
}
}
ImageView qr_image = (ImageView) findViewById(R.id.qrimage);
qr_image.setImageBitmap(bmp);
for more detail you can see Generating QR Codes with ZXing for getting Bitmap from bitMatrix
simple way
public static Bitmap createQRImage(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height).flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE)).toArray(),
width, height, Bitmap.Config.ARGB_8888);
}
or
public static Bitmap createQRImage2(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height * width).map(s -> bitMatrix.get(s % width, s / width) ? Color.BLACK : Color.WHITE).toArray(),
width, height, Bitmap.Config.ARGB_8888);
}
or, because Stream.toArray() allocate the output array dynamically if size unknown, which may cause memory allocation frequently if the array size too large
public static Bitmap createQRImage3(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height)
.flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE))
.collect(() -> IntBuffer.allocate(width * height), IntBuffer::put, IntBuffer::put)
.array(),
width, height, Bitmap.Config.ARGB_8888);
}
As appose to that,
get the directory to the image file then feed it in to:
Bitmap imageBitmap = BitmapFactory.decodeFile(path);
then its simple as
myImageView.setImageBitmap(imageBitmap);