Skip to content

Instantly share code, notes, and snippets.

@leadVisionary
Last active November 23, 2019 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leadVisionary/d8179c6c6a597505a32e4c300882ca8b to your computer and use it in GitHub Desktop.
Save leadVisionary/d8179c6c6a597505a32e4c300882ca8b to your computer and use it in GitHub Desktop.
QRCode from an Image in Java
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.LuminanceSource;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.Result;
public class Demo {
public static void main(final String[] args) {
final byte[] image = new byte[] {}; //get this from somewhere
System.out.println(getDecodedQRCode(image));
}
private static String getDecodedQRCode(final byte[] array)
throws IOException, NotFoundException, ChecksumException, FormatException {
try(InputStream in = new ByteArrayInputStream(array)) {
BufferedImage image = ImageIO.read(in);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = reader.decode(bitmap);
return result.getText();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment