ZXing 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
ZXing 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
ZXing 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 Apache
开发语言 Java
操作系统 跨平台
软件类型 开源软件
开源组织
地区 不详
投 递 者 红薯
适用人群 未知
收录时间 2009-06-24

软件简介

ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。

示例代码:

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
* 二维码工具类
*/
public class QRCodeUtil {
  private static final int width = 300;// 默认二维码宽度
  private static final int height = 300;// 默认二维码高度
  private static final String format = "png";// 默认二维码文件格式
  private static final Map<EncodeHintType, Object> hints = new HashMap();// 二维码参数

  static {
      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符编码
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容错等级 L、M、Q、H 其中 L 为最低, H 为最高
      hints.put(EncodeHintType.MARGIN, 2);// 二维码与图片边距
  }
  /**
   * 返回一个 BufferedImage 对象
   * @param content 二维码内容
   * @param width   宽
   * @param height  高
   */
  public static BufferedImage toBufferedImage(String content, int width, int height) throws WriterException, IOException {
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
      return MatrixToImageWriter.toBufferedImage(bitMatrix);
  }
  /**
   * 将二维码图片输出到一个流中
   * @param content 二维码内容
   * @param stream  输出流
   * @param width   宽
   * @param height  高
   */
  public static void writeToStream(String content, OutputStream stream, int width, int height) throws WriterException, IOException {
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
      MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
  }
  /**
   * 生成二维码图片文件
   * @param content 二维码内容
   * @param path    文件保存路径
   * @param width   宽
   * @param height  高
   */
  public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException {
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
      //toPath() 方法由 jdk1.7 及以上提供
      MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath());
  }
}
展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击加入讨论🔥(6) 发布并加入讨论🔥
2018/06/25 15:50

条形码处理类库 ZXing 3.3.3 发布,支持 Java 9

ZXing 3.3.3 已发布,ZXing 是一个开源 Java 类库,可用于解析多种格式的 1D/2D 条形码。目标是能够对 QR 编码、Data Matrix、UPC 的 1D 条形码进行解码。 该版本更新内容主要包含: 一些修复和改进 支持 Java 9+ 具体细节可查阅:https://github.com/zxing/zxing/milestone/11?closed=1 下载地址: Source code (zip) Source code (tar.gz)...

3
17
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
6 评论
339 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部