Java实现带logo的二维码
二维码应用到生活的各个方面,会用代码实现二维码,我想必定是一项加分的技能。好了,咱们来一块儿实现一下吧。java
咱们实现的二维码是基于QR Code的标准的,QR Code是由日本Denso公司于1994年研制的一种矩阵二维码符号码,全称是Quick Response Codedom
QR Code:专利公开,支持中文;maven
QR Code与其余二维码相比,具备识读速度快、数据密度大、占用空间小的优点;函数
纠错能力:工具
L级:约可纠错7%的数据码字 M级:约可纠错15%的数据码字 Q级:约可纠错25%的数据码字 H级:约可纠错30%的数据码字测试
知道了这些硬知识后,咱们作一下准备工做,咱们须要下载两个jar包,辅助咱们开发,这里我放上连接 http://mavenrepository.com/artifact/com.google.zxing/javase http://mavenrepository.com/artifact/com.google.zxing/core/3.3.2ui
咱们在咱们的工程下,新建一个java project 项目就能够了,能够新建三个lib、utils、test三个文件夹;lib存放jar包,utils放咱们写的工具类,test用来放咱们写的测试类this
咱们先从不带logo的二维码开始
思路:
一、设置一些二维码的参数;如 字符集、外边距、容错等级google
二、生成二维码spa
好了,咱们来实现一下
/** * * @param width 二维码的宽 * * @param height 二维码的高 * * @param content 二维码的内容 * */ public static void createQrCode(int width, int height, String content) { // 一、设置二维码的一些参数 HashMap hints = new HashMap(); // 1.1设置字符集 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 1.2设置容错等级;由于有了容错,在必定范围内能够把二维码p成你喜欢的样式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 1.3设置外边距;(即白色区域) hints.put(EncodeHintType.MARGIN, 1); // 二、生成二维码 try { // 2.1定义BitMatrix对象 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 2.二、设置二维码存放路径,以及二维码的名字 Path codePath = new File("c:/Users/admin/Desktop/code/" + UUID.randomUUID() + ".png").toPath(); // 2.三、执行生成二维码 MatrixToImageWriter.writeToPath(bitMatrix, "png", codePath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
咱们来实现一下带logo的二维码
思路:
一、 咱们先生成一个LogoConfig的配置类,主要是设置logo边框颜色;logo边框宽度;logo大小
二、 设置一些二维码的参数
三、 生成二维码
四、 生成带logo的二维码
咱们来实现一下
LogoConfig类
class LogoConfig { // logo默认边框颜色 public static final Color DEFAULT_BORDERCOLOR = Color.WHITE; // logo默认边框宽度 public static final int DEFAULT_BORDER = 2; // logo大小默认为照片的1/6 public static final int DEFAULT_LOGOPART = 6; private final int border = DEFAULT_BORDER; private final Color borderColor; private final int logoPart; 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; } }
生成中间有logo的二维码的工具函数
/** * 生成中间有logo的二维码 * * @param width * @param height * @param content */ public static void createLogoQrCode(int width, int height, String content) { // 一、设置二维码的一些参数 HashMap hints = new HashMap(); // 1.1设置字符集 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 1.2设置容错等级;由于有了容错,在必定范围内能够把二维码p成你喜欢的样式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 1.3设置外边距;(即白色区域) hints.put(EncodeHintType.MARGIN, 1); // 二、生成二维码 try { // 2.1定义BitMatrix对象 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 2.二、设置二维码存放路径,以及二维码的名字 // qrFile用来存放生成的二维码图片 File qrFile = new File("c:/Users/admin/Desktop/code", UUID.randomUUID() + ".jpg"); // logoFile用来存放带有logo的二维码图片 File logoFile = new File("c:/Users/admin/Desktop/code", "test.jpg"); // 2.三、执行生成二维码 MatrixToImageWriter.writeToPath(bitMatrix, "jpg", qrFile.toPath()); // 2.4在二维码中添加logo LogoConfig logoConfig = new LogoConfig(); addLogo(qrFile, logoFile, logoConfig); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
添加logo的工具类
/** * * @param qrPic 二维码文件路径 * @param logoPic logo文件路径 * @param logoConfig 配置类 */ private static void addLogo(File qrPic, File logoPic, LogoConfig logoConfig) { if (!qrPic.isFile() || !logoPic.isFile()) { System.out.println("file not found!"); System.exit(0); } try { // 一、读取二维码图片,并构建绘图对象 BufferedImage image = ImageIO.read(qrPic); Graphics2D graph = image.createGraphics(); // 二、读取logo图片 BufferedImage logo = ImageIO.read(logoPic); int widthLogo = image.getWidth() / logoConfig.getLogoPart(); int heightLogo = image.getHeight() / logoConfig.getLogoPart(); // 三、计算图片放置的位置 int x = (image.getWidth() - widthLogo) / 2; int y = (image.getHeight() - heightLogo) / 2; // 四、绘制图片 graph.drawImage(logo, x, y, widthLogo, heightLogo, null); graph.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10); graph.setStroke(new BasicStroke(logoConfig.getBorder())); graph.setColor(logoConfig.getBorderColor()); graph.drawRect(x, y, widthLogo, heightLogo); graph.dispose(); ImageIO.write(image, "jpeg", new File("C:/Users/admin/Desktop/code/newPic.jpg")); } catch (Exception e) { System.out.println(e); } }
咱们写一个测试类来测试一下
@Test public void test() { QrCodeUtils.createQrCode(100, 100, "你好,世界"); QrCodeUtils.readQrCode("C:\\Users\\admin\\Desktop\\code\\4ad3a0a4-8d5c-4cd3-9ee5-5f680233a33f.png"); QrCodeUtils.createLogoQrCode(300, 300, "https://www.jianshu.com/u/f84a2d49420b"); QrCodeUtils.readQrCode("C:\\Users\\admin\\Desktop\\code\\newPic.jpg"); }
我就不放生成好的二维码了,你们能够本身试试
咱们补充一个读取二维码内容的工具函数
/** * 解析二维码 * * @param codePath 二维码存放全路径 * */ public static void readQrCode(String codePath) { try { MultiFormatReader formatReader = new MultiFormatReader(); File QrCode = new File(codePath); BufferedImage image = ImageIO.read(QrCode); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); // 设置二维码的参数 HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); Result result = formatReader.decode(binaryBitmap, hints); // 打印解析结果 System.out.println(result.toString()); // 打印二维码格式 System.out.println(result.getBarcodeFormat()); // 二维码文本内容 System.out.println(result.getText()); } catch (Exception e) { System.out.println(e); } }
注:二维码是有必定纠错能力的,你能够把二维码p成你喜欢的样式