Skip to content

Instantly share code, notes, and snippets.

@typelogic
Created July 16, 2020 04:04
Show Gist options
  • Select an option

  • Save typelogic/07d2d0af263567c0fbf960bf5caca77a to your computer and use it in GitHub Desktop.

Select an option

Save typelogic/07d2d0af263567c0fbf960bf5caca77a to your computer and use it in GitHub Desktop.
QR reader snippet to read binary qr encoded
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
private static byte[] decodeQRCode(File qrCodeimage) throws IOException
{
BufferedImage bufferedImage = ImageIO.read(qrCodeimage);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
byte[] content = new byte[0];
try {
Result result = new MultiFormatReader().decode(bitmap);
Map m = result.getResultMetadata();
if (m.containsKey(ResultMetadataType.BYTE_SEGMENTS)) {
List L = (List)m.get(ResultMetadataType.BYTE_SEGMENTS);
content = (byte[])L.get(0);
} else {
content = result.getText().getBytes();
}
} catch (NotFoundException e) {
System.out.println("no QR Code in the image");
}
return content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment