Skip to content

Instantly share code, notes, and snippets.

@soldev-42
Created November 26, 2015 18:05
Show Gist options
  • Select an option

  • Save soldev-42/6aca3064e1901e912755 to your computer and use it in GitHub Desktop.

Select an option

Save soldev-42/6aca3064e1901e912755 to your computer and use it in GitHub Desktop.
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* QrCode decoder class
*/
public class QrCodeDecoder
{
/**
* Decodes QrCodeDecoder from image content encoded with Base64
*
* @param qrcodeBase64
* @return String
*/
public static String decode(String qrcodeBase64)
{
String decodedString;
try {
// decodes base64 string
byte[] qrCodeBytes = Base64.getDecoder().decode(qrcodeBase64.getBytes(StandardCharsets.UTF_8));
Map hintMap = new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// decodes QrCodeDecoder
BinaryBitmap binaryBitmap = new BinaryBitmap(
new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(
new ByteArrayInputStream(qrCodeBytes)
)
)
)
);
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
decodedString = qrCodeResult.getText();
} catch (Exception e) {
decodedString = "";
}
return decodedString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment