java使用jar包生成二维码的示例代码

这篇文章主要介绍了java使用jar包生成二维码的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

使用java进行二维码的生成与读取使用到了谷歌的zxing.jar

第一步 导入,maven依赖或者下载指定jar包

  com.google.zxingjavase3.2.1

第二步 书写二维码生成器的工具类

 import java.awt.Color; import java.io.File; import java.util.Hashtable; import com.google.zxing.EncodeHintType; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * QRCode 生成器的格式 * * @author ai(ahx.show) */ public class QRCodeFormat { /** 图片大小 */ private int size; /** 内容编码格式 */ private String encode; /** 错误修正等级 (Error Collection Level) */ private ErrorCorrectionLevel errorCorrectionLevel; /** 错误修正等级的具体值 */ private double errorCorrectionLevelValue; /** 前景色 */ private Color foreGroundColor; /** 背景色 */ private Color backGroundColor; /** 图片的文件格式 */ private String imageFormat; /** 图片的外边距大小 (Quiet Zone) */ private int margin; /** 提供给编码器额外的参数 */ private Hashtable hints; /** 需要添加的图片 */ private File icon; /** * 创建一个带有默认值的 QRCode 生成器的格式。默认值如下 * * 
    *
  • 图片大小: 256px
  • *
  • 内容编码格式: UTF-8
  • *
  • 错误修正等级: Level M (有15% 的内容可被修正)
  • *
  • 前景色: 黑色
  • *
  • 背景色: 白色
  • *
  • 输出图片的文件格式: png
  • *
  • 图片空白区域大小: 0个单位
  • *
* * @return QRCode 生成器格式 */ public static QRCodeFormat NEW() { return new QRCodeFormat(); } private QRCodeFormat() { this.size = 256; this.encode = "utf-8"; this.errorCorrectionLevel = ErrorCorrectionLevel.M; this.errorCorrectionLevelValue = 0.15; this.foreGroundColor = Color.BLACK; this.backGroundColor = Color.WHITE; this.imageFormat = "png"; this.margin = 0; this.hints = new Hashtable(); } /** * 返回图片的大小。 * * @return 图片的大小 */ public int getSize() { return this.size; } /** * 设置图片的大小。图片的大小等于实际内容与外边距的值(建议设置成偶数值)。 * * @param size * 图片的大小 * * @return QRCode生成器的格式 */ public QRCodeFormat setSize(int size) { this.size = size; return this; } /** * 返回内容编码格式。 * * @return 内容编码格式 */ public String getEncode() { return encode; } /** * 设置内容编码格式。 * * @param encode * 内容编码格式 * * @return QRCode生成器的格式 */ public QRCodeFormat setEncode(String encode) { this.encode = encode; return this; } /** * 返回错误修正等级。 * * @return 错误修正等级 */ public ErrorCorrectionLevel getErrorCorrectionLevel() { return errorCorrectionLevel; } /** * 返回错误修正等级的具体值。 * * @return 错误修正等级的具体值 */ public double getErrorCorrectionLevelValue() { return errorCorrectionLevelValue; } /** * 设置错误修正等级。其定义如下 * *
    *
  • L: 有 7% 的内容可被修正
  • *
  • M: 有15% 的内容可被修正
  • *
  • Q: 有 25% 的内容可被修正
  • *
  • H: 有 30% 的内容可被修正
  • *
* * @param errorCorrectionLevel * 错误修正等级 * * @return QRCode生成器的格式 */ public QRCodeFormat setErrorCorrectionLevel(char errorCorrectionLevel) { switch (Character.toUpperCase(errorCorrectionLevel)) { case 'L': this.errorCorrectionLevel = ErrorCorrectionLevel.L; this.errorCorrectionLevelValue = 0.07; break; case 'M': this.errorCorrectionLevel = ErrorCorrectionLevel.M; this.errorCorrectionLevelValue = 0.15; break; case 'Q': this.errorCorrectionLevel = ErrorCorrectionLevel.Q; this.errorCorrectionLevelValue = 0.25; break; case 'H': this.errorCorrectionLevel = ErrorCorrectionLevel.H; this.errorCorrectionLevelValue = 0.3; break; default: this.errorCorrectionLevel = ErrorCorrectionLevel.M; } return this; } /** * 返回前景色。 * * @return 前景色 */ public Color getForeGroundColor() { return foreGroundColor; } /** * 设置前景色。值为十六进制的颜色值(与 CSS 定义颜色的值相同,不支持简写),可以忽略「#」符号。 * * @param foreGroundColor * 前景色的值 * * @return QRCode生成器的格式 */ public QRCodeFormat setForeGroundColor(String foreGroundColor) { try { this.foreGroundColor = getColor(foreGroundColor); } catch (NumberFormatException e) { this.foreGroundColor = Color.BLACK; } return this; } /** * 设置前景色。 * * @param foreGroundColor * 前景色的值 * * @return QRCode生成器的格式 */ public QRCodeFormat setForeGroundColor(Color foreGroundColor) { this.foreGroundColor = foreGroundColor; return this; } /** * 返回背景色。 * * @return 背景色 */ public Color getBackGroundColor() { return backGroundColor; } /** * 设置背景色。值为十六进制的颜色值(与 CSS 定义颜色的值相同,不支持简写),可以忽略「#」符号。 * * @param backGroundColor * 前景色的值 * * @return QRCode生成器的格式 */ public QRCodeFormat setBackGroundColor(String backGroundColor) { try { this.backGroundColor = getColor(backGroundColor); } catch (NumberFormatException e) { this.backGroundColor = Color.WHITE; } return this; } /** * 设置背景色。 * * @param backGroundColor * 前景色的值 * * @return QRCode生成器的格式 */ public QRCodeFormat setBackGroundColor(Color backGroundColor) { this.backGroundColor = backGroundColor; return this; } /** * 返回图片的文件格式。 * * @return 图片的文件格式 */ public String getImageFormat() { return imageFormat.toUpperCase(); } /** * 设置图片的文件格式 。 * * @param imageFormat * 图片的文件格式 * * @return QRCode生成器的格式 */ public QRCodeFormat setImageFormat(String imageFormat) { this.imageFormat = imageFormat; return this; } /** * 返回图片的外边距大小。 * * @return 图片的外边距大小 */ public int getMargin() { return margin; } /** * 设置图片的外边距大小 。 * * @param margin * 图片的外边距大小 * * @return QRCode生成器的格式 */ public QRCodeFormat setMargin(int margin) { this.margin = margin; return this; } /** * 返回提供给编码器额外的参数。 * * @return 提供给编码器额外的参数 */ public Hashtable getHints() { hints.clear(); hints.put(EncodeHintType.ERROR_CORRECTION, getErrorCorrectionLevel()); hints.put(EncodeHintType.CHARACTER_SET, getEncode()); hints.put(EncodeHintType.MARGIN, getMargin()); return hints; } /** * 返回添加的图片。 * * @return 添加的图片 */ public File getIcon() { return icon; } /** * 设置添加的图片 。 * * @param icon * 添加的图片 * * @return QRCode生成器的格式 */ public QRCodeFormat setIcon(File icon) { this.icon = icon; return this; } /** * 设置添加的图片 。 * * @param iconPath * 添加的图片 * * @return QRCode生成器的格式 */ public QRCodeFormat setIcon(String iconPath) { return setIcon(new File(iconPath)); } private Color getColor(String hexString) { if (hexString.charAt(0) == '#') { return new Color(Long.decode(hexString).intValue()); } else { return new Color(Long.decode("0xFF" + hexString).intValue()); } } }

第三步 使用生成器对象按照指定格式进行生成读取二维码

 import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.ChecksumException; import com.google.zxing.FormatException; import com.google.zxing.LuminanceSource; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.qrcode.QRCodeWriter; /** * QRCode 处理器 * @ClassName: QRCode * @Description: TODO * @author: ai(ahx.show) * @date: 2016年12月18日 下午1:22:50 */ public final class QRCode { /** QRCode 生成器格式 */ private QRCodeFormat format = null; /** 生成的 QRCode 图像对象 */ private BufferedImage qrcodeImage = null; /** 生成的 QRCode 图片文件 */ private File qrcodeFile = null; /** * 返回生成的 QRCode 图像对象 * * @return 生成的 QRCode 图像对象 */ public BufferedImage getQrcodeImage() { return qrcodeImage; } /** * 返回生成的 QRCode 图片文件 * * @return 生成的 QRCode 图片文件 */ public File getQrcodeFile() { return qrcodeFile; } private QRCode() { } /** * 使用带默认值的「QRCode 生成器格式」来创建一个 QRCode 处理器。 * * @param content *      所要生成 QRCode 的内容 * * @return QRCode 处理器 */ public static QRCode NEW(final String content) { return NEW(content, QRCodeFormat.NEW()); } /** * 使用指定的「QRCode 生成器格式」来创建一个 QRCode 处理器。 * * @param content *      所要生成 QRCode 的内容 * @param format *      QRCode 生成器格式 * * @return QRCode 处理器 */ public static QRCode NEW(final String content, QRCodeFormat format) { QRCode qrcode = new QRCode(); qrcode.format = format; qrcode.qrcodeImage = toQRCode(content, format); return qrcode; } /** * 把指定的内容生成为一个 QRCode 的图片,之后保存到指定的文件中。 * * @param f *      指定的文件 * * @return QRCode 处理器 */ public QRCode toFile(String f) { return toFile(new File(f), this.format.getIcon()); } /** * 把指定的内容生成为一个 QRCode 的图片,之后保存到指定的文件中。 * * @param qrcodeFile *      指定的文件 * * @return QRCode 处理器 */ public QRCode toFile(File qrcodeFile) { return toFile(qrcodeFile, this.format.getIcon()); } /** * 把指定的内容生成为一个 QRCode 的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。 * * @param qrcodeFile *      QRCode 图片生成的指定的文件 * @param appendFile *      需要添加的图片。传入的文件路径如果没有(null 或者为空)的时候将忽略该参数 * * @return QRCode 处理器 */ public QRCode toFile(String qrcodeFile, String appendFile) { if (null == appendFile || appendFile.length() == 0) { return toFile(new File(qrcodeFile)); } return toFile(new File(qrcodeFile), new File(appendFile)); } /** * 把指定的内容生成为一个 QRCode 的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。 * * @param qrcodeFile *      QRCode 图片生成的指定的文件 * @param appendFile *      需要添加的图片。传入的图片不存在的时候将忽略该参数 * * @return QRCode 处理器 */ public QRCode toFile(File qrcodeFile, File appendFile) { try { if (!qrcodeFile.exists()) { qrcodeFile.getParentFile().mkdirs(); qrcodeFile.createNewFile(); } if (null != appendFile && appendFile.isFile() && appendFile.length() != 0) { appendImage(ImageIO.read(appendFile)); } if (!ImageIO.write(this.qrcodeImage, getSuffixName(qrcodeFile), qrcodeFile)) { throw new RuntimeException("Unexpected error writing image"); } } catch (IOException e) { throw new RuntimeException(e); } this.qrcodeFile = qrcodeFile; return this; } private void appendImage(BufferedImage appendImage) { appendImage(this.qrcodeImage, appendImage, this.format); } private static void appendImage(BufferedImage qrcodeImage, BufferedImage appendImage, QRCodeFormat format) { int baseWidth = qrcodeImage.getWidth(); int baseHeight = qrcodeImage.getHeight(); // 计算 icon 的最大边长 // 公式为 二维码面积*错误修正等级*0.4 的开方 int maxWidth = (int) Math.sqrt(baseWidth * baseHeight * format.getErrorCorrectionLevelValue() * 0.4); int maxHeight = maxWidth; // 获取 icon 的实际边长 int roundRectWidth = (maxWidth 

第四步 使用

工具类中的方法使用的静态方法,可以直接使用QRCode.方法进行执行

生成二维码方法

QRCode.NEW(str).toFile(url);

str:二维码中包含的字符串(如果包含地址前缀添加http或https 否则不能自动跳转 会解析地址字符串)

url:二维码图片生成位置

QRCode.from(url);

url:要解析二维码图片位置

以上就是java使用jar包生成二维码的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java