Last active
November 23, 2019 00:53
-
-
Save leadVisionary/d8179c6c6a597505a32e4c300882ca8b to your computer and use it in GitHub Desktop.
QRCode from an Image in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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