Java zxing生成条形码和二维吗代码实例

在如今的生活中,二维码随处可见,二维码的出现既减少了宣传纸张的浪费,又方便了人们的生活。这一篇我来说说 Java 利用第三方 Jar 包 zxing 生成二维码。

依赖

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>3.3.3</version>
</dependency>
<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>3.3.3</version>
</dependency>

生成二维码

package code;

import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64OutputStream;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QRCodeKit {

  public static final String QRCODE_DEFAULT_CHARSET = "UTF-8";

  public static final int QRCODE_DEFAULT_HEIGHT = 300;

  public static final int QRCODE_DEFAULT_WIDTH = 300;

  private static final int BLACK = 0xFF000000;
  private static final int WHITE = 0xFFFFFFFF;

  public static void main(String[] args) throws IOException, NotFoundException {
    String data = "https://www.jianshu.com/p/748aa03cc1e8?ddd=dsdsdsdsddsdsdsdsdsdsdsdsd";
    File logoFile = new File("C:/1.png");
    BufferedImage image = QRCodeKit.createQRCodeWithLogo(data, logoFile);
    ImageIO.write(image, "png", new File("D:/result7.png"));
    System.out.println("done");
  }

  /**
   * Create qrcode with default settings
   *
   * @param data
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCode(String data) {
    return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT);
  }

  /**
   * Create qrcode with default charset
   *
   * @param data
   * @param width
   * @param height
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCode(String data, int width, int height) {
    return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height);
  }

  /**
   * Create qrcode with specified charset
   *
   * @param data
   * @param charset
   * @param width
   * @param height
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCode(String data, String charset, int width, int height) {
    Map hint = new HashMap();
    hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hint.put(EncodeHintType.CHARACTER_SET, charset);

    return createQRCode(data, charset, hint, width, height);
  }

  /**
   * Create qrcode with specified hint
   *
   * @param data
   * @param charset
   * @param hint
   * @param width
   * @param height
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCode(String data, String charset, Map<EncodeHintType, ?> hint, int width,
                       int height) {
    BitMatrix matrix;
    try {
      matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE,
          width, height, hint);
      return toBufferedImage(matrix);
    } catch (WriterException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }

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

  /**
   * Create qrcode with default settings and logo
   *
   * @param data
   * @param logoFile
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCodeWithLogo(String data, File logoFile) {
    return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile);
  }

  /**
   * Create qrcode with default charset and logo
   *
   * @param data
   * @param width
   * @param height
   * @param logoFile
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) {
    return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile);
  }

  /**
   * Create qrcode with specified charset and logo
   *
   * @param data
   * @param charset
   * @param width
   * @param height
   * @param logoFile
   * @return
   * @author stefli
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) {
    Map hint = new HashMap();
    hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hint.put(EncodeHintType.CHARACTER_SET, charset);
    hint.put(EncodeHintType.MARGIN, 2);

    return createQRCodeWithLogo(data, charset, hint, width, height, logoFile);
  }

  /**
   * Create qrcode with specified hint and logo
   *
   * @param data
   * @param charset
   * @param hint
   * @param width
   * @param height
   * @param logoFile
   * @return
   * @author stefli
   */
  public static BufferedImage createQRCodeWithLogo(String data, String charset, Map<EncodeHintType, ?> hint,
                           int width, int height, File logoFile) {
    try {
      BufferedImage qrcode = createQRCode(data, charset, hint, width, height);
      BufferedImage logo = ImageIO.read(logoFile);

      BufferedImage combined = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) combined.getGraphics();

      //设置二维码大小,太大,会覆盖二维码,此处20%
      int logoWidth = logo.getWidth() > qrcode.getWidth() * 2 / 10 ? (qrcode.getWidth() * 2 / 10) : logo.getWidth();
      int logoHeight = logo.getHeight() > qrcode.getHeight() * 2 / 10 ? (qrcode.getHeight() * 2 / 10) : logo.getHeight();

      //设置logo图片放置位置--中心
      int x = Math.round((qrcode.getWidth() - logoWidth) / 2);
      int y = Math.round((qrcode.getHeight() - logoHeight) / 2);

      g.drawImage(qrcode, 0, 0, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));

      //开始合并绘制图片
      g.drawImage(logo, x, y, logoWidth, logoHeight, null);
      g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);

      //logo边框大小
      g.setStroke(new BasicStroke(3));
      //logo边框颜色
      g.setColor(Color.white);
      g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
      g.dispose();

      logo.flush();
      qrcode.flush();

      return combined;
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }

  /**
   * Return base64 for image
   *
   * @param image
   * @return
   * @author stefli
   */
  public static String getImageBase64String(BufferedImage image) {
    String result = null;
    try {
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      OutputStream b64 = new Base64OutputStream(os);
      ImageIO.write(image, "png", b64);
      result = os.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    return result;
  }

  /**
   * Decode the base64Image data to image
   *
   * @param base64ImageString
   * @param file
   * @author stefli
   */
  public static void convertBase64StringToImage(String base64ImageString, File file) {
    FileOutputStream os;
    try {
      Base64 d = new Base64();
      byte[] bs = d.decode(base64ImageString);
      os = new FileOutputStream(file.getAbsolutePath());
      os.write(bs);
      os.close();
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * ZXing二维码生成/解码
 */
public class ZXingCode {

  public static void main(String[] args) throws WriterException {
    try {
      String logoPath = "F:/logo.jpg";
      String logo_savePath = "F:/" + new Date().getTime() + ".png";
      String str = toQRCode("http://blog.csdn.net/phil_jing", logoPath, logo_savePath, "CSDN博客");
      System.out.println("finished zxing QRcode encode.");
      System.out.println(Arrays.toString(Base64.decodeBase64(str)));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 生成二维码图片
   * @param content
   * @param logoPath
   * @param savePath
   * @param remark
   * @return
   */
  public static String toQRCode(String content, String logoPath, String savePath, String remark) {
    int width = 400, height = 400;
    try {
      BufferedImage bim = toBufferedImage(content, BarcodeFormat.QR_CODE, width, height, toDecodeHintType());
      return encode(bim, logoPath, savePath, new LogoConfig(), remark);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 是否需要给二维码图片添加Logo
   * @param bim
   * @param logoPath
   * @param savePath
   * @param logoConfig
   * @param remark
   * @return
   */
  private static String encode(BufferedImage bim, String logoPath, String savePath, LogoConfig logoConfig, String remark) {
    ByteArrayOutputStream baos = null;
    try {
      /**
       * 读取二维码图片
       */
      BufferedImage image = bim;
      if(StringUtils.isBlank(logoPath)){ //不需要添加logo
        baos = new ByteArrayOutputStream();
        baos.flush();
        ImageIO.write(image, "png", baos);//流输出
        //ImageIO.write(bim, "png", new File(savePath));//直接写入某路径,本地测试加上
        return Base64.encodeBase64URLSafeString(baos.toByteArray());//Encodes binary data using a URL-safe variation of the base64 algorithm
      }
      /**
       * 构建绘图对象
       */
      Graphics2D g = image.createGraphics();
      /**
       * 读取Logo图片
       */
      BufferedImage logo = ImageIO.read(new File(logoPath));
      /**
       * 设置logo的大小,设置为二维码图片的20%,因为过大会盖掉二维码
       */
      int widthLogo = logo.getWidth(null) > image.getWidth() * 3 / 10 ? (image.getWidth() * 3 / 10)
          : logo.getWidth(null),
          heightLogo = logo.getHeight(null) > image.getHeight() * 3 / 10 ? (image.getHeight() * 3 / 10)
              : logo.getWidth(null);
      /**
       * logo放在中心
       */
      int x = (image.getWidth() - widthLogo) / 2;
      int y = (image.getHeight() - heightLogo) / 2;
      /**
       * logo放在右下角 int x = (image.getWidth() - widthLogo); int y = (image.getHeight() - heightLogo);
       */
      // 开始绘制图片
      g.drawImage(logo, x, y, widthLogo, heightLogo, null);
      // g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
      // g.setStroke(new BasicStroke(logoConfig.getBorder()));
      // g.setColor(logoConfig.getBorderColor());
      // g.drawRect(x, y, widthLogo, heightLogo);
      g.dispose();
      // 把备注添加上去,备注不要太长超过两行会自动截取
      if ( StringUtils.isNotBlank(remark)){
        // 新的图片,把带logo的二维码下面加上文字
        BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D outg = outImage.createGraphics();
        // 画二维码到新的面板
        outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
        // 画文字到新的面板
        outg.setColor(Color.BLACK);
        outg.setFont(new Font("微软雅黑", Font.BOLD, 30)); // 字体、字型、字号
        int strWidth = outg.getFontMetrics().stringWidth(remark);
        if (strWidth > 399) {
          // //长度过长就截取前面部分
          // outg.drawString(productName, 0, image.getHeight() +
          // (outImage.getHeight() - image.getHeight())/2 + 5 ); //画文字
          String productName1 = remark.substring(0, remark.length() / 2);
          String productName2 = remark.substring(remark.length() / 2, remark.length());
          int strWidth1 = outg.getFontMetrics().stringWidth(productName1);
          int strWidth2 = outg.getFontMetrics().stringWidth(productName2);
          outg.drawString(productName1, 200 - strWidth1 / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 12);
          BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
          Graphics2D outg2 = outImage2.createGraphics();
          outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
          outg2.setColor(Color.BLACK);
          outg2.setFont(new Font("微软雅黑", Font.BOLD, 30)); // 字体、字型、字号
          outg2.drawString(productName2, 200 - strWidth2 / 2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
          outg2.dispose();
          outImage2.flush();
          outImage = outImage2;
        } else {
          outg.drawString(remark, 200 - strWidth / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 12); // 画文字
        }
        outg.dispose();
        outImage.flush();
        image = outImage;
      }
      logo.flush();
      image.flush();
      baos = new ByteArrayOutputStream();
      baos.flush();
      ImageIO.write(image, "png", baos); //不用MatrixToImageWriter
      //ImageIO.write(image, "png", new File(savePath));//直接写入某路径
      return Base64.encodeBase64URLSafeString(baos.toByteArray());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      IOUtils.closeQuietly(baos);
    }
    return null;
  }

  /**
   * 生成二维码bufferedImage图片
   * @param content 编码内容
   * @param barcodeFormat 编码类型
   * @param width 图片宽度
   * @param height 图片高度
   * @param hints 设置参数
   * @return
   */
  private static BufferedImage toBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height,
      Map<EncodeHintType, ?> hints) {
    MultiFormatWriter multiFormatWriter = null;
    BitMatrix bitMatrix = null;
    BufferedImage image = null;
    try {
      multiFormatWriter = new MultiFormatWriter();
      // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
      bitMatrix = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
      int w = bitMatrix.getWidth();
      int h = bitMatrix.getHeight();
      image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
      for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
          image.setRGB(x, y, bitMatrix.get(x, y) ? MatrixToImageConfig.BLACK : MatrixToImageConfig.WHITE);
        }
      }
    } catch (WriterException e) {
      e.printStackTrace();
    }
    return image;
  }

  /**
   * 设置二维码的格式参数
   * @return
   */
  private static Map<EncodeHintType, Object> toDecodeHintType() {
    // 用于设置QR二维码参数
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    // 设置QR二维码的纠错级别(H为最高级别)具体级别信息
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 设置编码方式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hints.put(EncodeHintType.MARGIN, 0);
    //hints.put(EncodeHintType.MAX_SIZE, 350);//Only applicable to Data Matrix now
    //hints.put(EncodeHintType.MIN_SIZE, 100);//Only applicable to Data Matrix now
    return hints;
  }

  /**
   * 二维码解码
   *
   * @param imgPath
   * @return
   */
  public static String decode(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
      File file = new File(imgPath);
      image = ImageIO.read(file);
      if (image == null) {
        System.out.println("the decode image may be not exit.");
      }
      LuminanceSource source = new BufferedImageLuminanceSource(image);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
      hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
      result = new MultiFormatReader().decode(bitmap, hints);
      return result.getText();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

/**
 * Logo图片配置
 */
class LogoConfig {
  // logo默认边框颜色
  public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;
  // logo默认边框宽度
  public static final int DEFAULT_BORDER = 2;
  // logo大小默认为照片的1/5
  public static final int DEFAULT_LOGOPART = 5;

  private final int border = DEFAULT_BORDER;
  private final Color borderColor;
  private final int logoPart;

  /**
   * Creates a default config with on color {@link #BLACK} and off color
   * {@link #WHITE}, generating normal black-on-white barcodes.
   */
  public LogoConfig() {
    this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
  }

  public LogoConfig(Color borderColor, int logoPart) {
    this.borderColor = borderColor;
    this.logoPart = logoPart;
  }

  public Color getBorderColor() {
    return borderColor;
  }

  public int getBorder() {
    return border;
  }

  public int getLogoPart() {
    return logoPart;
  }
}

生成条形码

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
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;

/**
 * ZXing条形码编码/解码
 */
public class ZxingCode {

  /**
   * 条形码编码
   *
   * @param contents
   * @param width
   * @param height
   * @param imgPath
   */
  public static void encode(String contents, int width, int height, String imgPath) {
    int codeWidth = 3 + // start guard
        (7 * 6) + // left bars
+ // middle guard
        (7 * 6) + // right bars
        3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
      BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.EAN_13, codeWidth, height, null);
      MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 条形码解码
   *
   * @param imgPath
   * @return String
   */
  public static String decode(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
      image = ImageIO.read(new File(imgPath));
      if (image == null) {
        System.out.println("the decode image may be not exit.");
      }
      LuminanceSource source = new BufferedImageLuminanceSource(image);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      result = new MultiFormatReader().decode(bitmap, null);
      return result.getText();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    String imgPath = "F:/zxing_EAN-13.png";
    String contents = "6926557300360";
    int width = 105, height = 50;
    encode(contents, width, height, imgPath);
    System.out.println("finished zxing EAN-13 encode.");
    String decodeContent = decode(imgPath);
    System.out.println("解码内容如下:" + decodeContent);
    System.out.println("finished zxing EAN-13 decode.");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 基于google zxing的Java二维码生成与解码

    本文实例为大家分享了Java二维码生成与解码的具体代码,供大家参考,具体内容如下 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.1.0</version> &

  • java ZXing生成二维码及条码实例分享

    1.jar包:   ZXing-core-3.3.0.jar http://mvnrepository.com/artifact/com.google.zxing/core   ZXing-javase-3.3.0.jar   http://mvnrepository.com/artifact/com.google.zxing/javase BufferedImageLuminanceSource.java package com.webos.util; import java.awt.Grap

  • Java利用Zxing生成二维码的简单实例

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码 1.二维码的生成 1.1 将Zxing-core.jar 包加入到classpath下. 1.2 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用. import com.google.zxing.common.BitMatrix; i

  • java中ZXing 生成、解析二维码图片的小示例

    概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和Android. 官网:ZXing github仓库 实战 本例演示如何在一个非 android 的 Java 项目中使用 ZXing 来生成.解析二维码图片. 安装 maven项目只需引入依赖: <dependency> <groupId>com.google.zxing

  • Java使用Google Zxing生成二维码的例子

    以前只用过jQuery.qrcode生成过二维码,这次使用的是Google的zxing通过Java代码生成二维码并以流的方式输出到前台页面 所需jar包:zxing-3.2.1.jar 代码 前台展示页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); S

  • Java中基于maven实现zxing二维码功能

    maven所需jar <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifac

  • java中使用zxing批量生成二维码立牌

    使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址.文本等)生成二维码并放在该位置,最后加上立牌编号. 步骤: 1).做好背景图,如下图: 2).生成二维码BufferedImage对象.代码如下: /** * * @Title: toBufferedImage * @Description: 把文本转化成二维码图片对象 * @param text * 二维码内容 * @param width * 二维码高度 * @param height * 二位宽度 * @para

  • Java zxing生成条形码和二维吗代码实例

    在如今的生活中,二维码随处可见,二维码的出现既减少了宣传纸张的浪费,又方便了人们的生活.这一篇我来说说 Java 利用第三方 Jar 包 zxing 生成二维码. 依赖 <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <

  • C#利用ZXing.Net生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.而ZXing.Net是ZXing的端口之一. 在工程中引用ZXing.Net 在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示: ZXing.Net关键类结构图 包括Reader[识

  • Android生成条形码和二维码功能

    背景: 随着移动互联网的普及以及智能终端设备的广泛应用,移动支付变得越来越便捷,通过扫描二维码代替传统的刷卡行为.那么作为开发者而言生成二维码成为了一项必备技能. 准备: 使用zxing包 implementation "com.google.zxing:core:3.3.1" 核心代码: package com.wangpengpro.h5test.utils; import android.graphics.Bitmap; import com.google.zxing.Barco

  • Python之ReportLab绘制条形码和二维码的实例

    条形码和二维码 #引入所需要的基本包 from reportlab.pdfgen import canvas from reportlab.graphics.barcode import code39, code128, code93 from reportlab.graphics.barcode import eanbc, qr, usps from reportlab.graphics.shapes import Drawing from reportlab.lib.units import

  • .NET C#利用ZXing生成、识别二维码/条形码

    一.首先下载 ZXing.Net 地址是:http://zxingnet.codeplex.com/releases/view/117068 然后将对应版本 .dll 拖入项目中,再引用之. 主要是用 BarcodeWriter.BarcodeReader. 二.生成二维码 .NET 平台的代码始终要简单些. QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options.CharacterSet = "UTF-8&quo

  • Android上使用ZXing识别条形码与二维码的方法

    目前有越来越多的手机具备自动对焦的拍摄功能,这也意味着这些手机可以具备条码扫描的功能.手机具备条码扫描的功能,可以优化购物流程,快速存储电子名片(二维码)等. 本文所述实例就使用了ZXing 1.6实现条码/二维码识别.ZXing是个很经典的条码/二维码识别的开源类库,早在很久以前,就有开发者在J2ME上使用ZXing了,只不过需要支持JSR-234规范(自动对焦)的手机才能发挥其威力,而目前已经有不少Android手机具备自动对焦的功能. 本文代码运行的结果如下,使用91手机助手截图时,无法截

  • asp.net C#生成和解析二维码的实例代码

    类库文件我们在文件最后面下载 [ThoughtWorks.QRCode.dll 就是类库] 使用时需要增加: 复制代码 代码如下: using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtWorks.QRCode.Codec.Util; 主要源代码: 1.生成二维码 复制代码 代码如下: QRCodeEncoder qrCodeEncoder = new QRCodeEncoder()

  • Android二维码创建实例

    Android二维码之创建 实现效果图: 1.Android 有自带的jar包可以生成二维码core-3.0.0.jar,其中的com.google.zxing包 2.写一个二维码生成的工具类,网上搜的话应该一大堆. 实例代码: package com.example.administrator.twocodedemo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.

  • Java生成读取条形码和二维码的简单示例

    条形码 将宽度不等的多个黑条和白条,按照一定的编码规则排序,用以表达一组信息的图像标识符 通常代表一串数字 / 字母,每一位有特殊含义 一般数据容量30个数字 / 字母 二维码 用某种特定几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息 比一维条形码能存储更多信息,表示更多数据类型 能够存储数字 / 字母 / 汉字 / 图片等信息 可存储几百到几十KB字符 Zxing Zxing主要是Google出品的,用于识别一维码和二维码的第三方库 主要类: BitMatrix位图

随机推荐