1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.sankuai.cx.etcp.code.exception.BizException; import com.sankuai.cx.etcp.code.exception.ErrorEnum; import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.HashMap; import java.util.Map;
@Slf4j public class ZxingQrCodeUtil {
private static String FORMAT = "png";
public static String createZxingqrCode(String content,int width,int height,String errorCorrection){ Map hints=new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "iso-8859-1"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.valueOf(errorCorrection)); hints.put(EncodeHintType.MARGIN, 0); try { BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BitMatrix resbitMatrix = deleteWhite(bitMatrix); ByteArrayOutputStream baos = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(resbitMatrix,FORMAT,baos); byte[] b = baos.toByteArray();
return Base64.getEncoder().encodeToString(b); } catch (Exception e) { throw new BizException(e,ErrorEnum.ZXING_ERROR.getCode()); } } public static void readZxingQrCode(){ MultiFormatReader reader = new MultiFormatReader(); File file = new File("/Users/charlie/Desktop/yitongxing.jpg"); try { BufferedImage image = ImageIO.read(file); BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); Map hints=new HashMap(); hints.put(DecodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = reader.decode(binaryBitmap,hints); byte[] b = result.getRawBytes(); System.out.println("解析结果:"+result.toString()); String s = HexUtil.byteToHex(result.toString().getBytes(StandardCharsets.ISO_8859_1)); System.out.println(s); System.out.println(s.length()); System.out.println("二维码格式:"+result.getBarcodeFormat()); System.out.println("二维码文本内容:"+result.getText()); } catch (Exception e) { log.error("read zxing pic error",e); } }
public static BitMatrix deleteWhite(BitMatrix matrix){ int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) { resMatrix.set(i, j); } } } return resMatrix; }
public static void main(String[] args) {
readZxingQrCode(); }
}
|