java二维码生成的方法
时间:2020-10-03 10:14:42|栏目:JAVA代码|点击: 次
本文实例为大家分享了java二维码的实现代码,供大家参考,具体内容如下
这次用到的jar包是zxing,没有用到core的jar包
先导入zxing.jar包
生成二维码
package cn.huse.erweima; import java.io.File; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 生成二维码 * */ public class CreateQRCode { public static void main(String[] args) { int width = 300; int height = 300; String format = "gif"; String content = "www.baidu.com"; //定义二维码的参数 HashMap hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); try { BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); File file = new File("e:"+File.separator+"new.gif"); MatrixToImageWriter.writeToFile(matrix, format, file); } catch (Exception e) { e.printStackTrace(); } } }
解析二维码
package cn.huse.erweima; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; //解析二维码 public class ReadQRCode { public static void main(String[] args) { MultiFormatReader formatReader = new MultiFormatReader(); File file = new File("e:"+File.separator+"new.gif"); try { BufferedImage image = ImageIO.read(file); 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 (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
上一篇:Java实现DES加密与解密,md5加密以及Java实现MD5加密解密类
栏 目:JAVA代码
本文标题:java二维码生成的方法
本文地址:http://www.codeinn.net/misctech/6325.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机