二维码及带logo二维码的生成和解析-Zxing

来源:互联网 发布:今晚eia数据 编辑:程序博客网 时间:2024/05/16 11:00

首先新建一个java project 我创建的是maven的项目可以方便添加包


maven添加依赖包

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.2.1</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.2.1</version></dependency>
在App类中添加如下代码:

private static final int BLACK = 0xff000000;private static final int WHITE = 0xFFFFFFFF;// 设置偏移量,不设置可能导致解析出错(可理解为二维码边框)private static final int pixoff = 2;private static final int width = 140;private static final int height = 140;
这里先定义二维码的背景色、前景色、偏移量、高度及宽度.

生成二维码内容:根据matrix生成二维点阵,并存入image对象.

public static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);}}return image;}

生成二维码图片:根据image对象生成给定格式的图片

public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {BufferedImage image = toBufferedImage(matrix);ImageIO.write(image, format, file);}

contents:存入二维码的字符串,file:存二维码的目标文件,format:编码格式(BarcodeFormat.QR_CODE——二维码/CODE_128——条形码)

public static void encode(String contents, File file, BarcodeFormat format, int width, int height) {try {// 消除乱码contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");// 参数mapMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();// 容错率hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 偏移量hints.put(EncodeHintType.MARGIN, pixoff);// 编码hints.put(EncodeHintType.CHARACTER_SET, "utf-8");MultiFormatWriter multiFormatWriter = new MultiFormatWriter();BitMatrix bitMatrix = multiFormatWriter.encode(contents, format, width, height, hints);writeToFile(bitMatrix, "png", file);} catch (Exception e) {e.printStackTrace();}}

添加logo的方法:qrImage是二维码文件,middleImage是带logo的文件

public static void addMiddleImage(File qrImage, File middleImage) {// 判断文件if (!qrImage.isFile() || !middleImage.isFile()) {System.out.print("file not find !");System.exit(0);}try {BufferedImage image = ImageIO.read(qrImage);Graphics2D gs = image.createGraphics();BufferedImage logo = ImageIO.read(middleImage);// 二维码真实宽度int imgRealW = width - (pixoff * 2);// 中心图片宽度int middleImgW = (int) (imgRealW / 5);// 二维码真实高度int imgRealH = height - (pixoff * 2);// 中心图片高度int middleImgH = (int) (imgRealH / 5);// 中心图片定点X坐标int middleImgL = (imgRealW - middleImgW) / 2;// 中心图片定点Y坐标int middleImgT = (imgRealH - middleImgH) / 2;// 把中心图片绘制到二维码上gs.drawImage(logo, middleImgL, middleImgT, middleImgW, middleImgH, null);gs.dispose();image.flush();// 生成二维码图片ImageIO.write(image, "png", qrImage);// 绘制字符串// gs.drawString("王星禹", middleImgL, middleImgT);} catch (Exception e) {e.printStackTrace();}}

解析二维码的方法:
public static void decode(File file) {if (!file.exists()) {return;}try {MultiFormatReader formatReader = new MultiFormatReader();BufferedImage image;image = ImageIO.read(file);if (image == null) {System.out.println("Could not decode image");}LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Result result = formatReader.decode(binaryBitmap);System.out.println("resultFormat = " + result.getBarcodeFormat());System.out.println("resultText = " + result.getText());} catch (Exception ex) {ex.printStackTrace();}}





0 0
原创粉丝点击