什么是二维码
二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
功能
●信息获取(名片、地图、WIFI密码、资料)
●网站跳转(跳转到微博、手机网站、网站)
●广告推送(用户扫码,直接浏览商家推送的视频、音频广告)
●手机电商(用户扫码、手机直接购物下单)
●防伪溯源(用户扫码、即可查看生产地;同时后台可以获取最终消费地)
●优惠促销(用户扫码,下载电子优惠券,抽奖)
●会员管理(用户手机上获取电子会员信息、VIP服务)
●手机支付(扫描商品二维码,通过银行或第三方支付提供的手机端通道完成支付)
应用前景
尽管二维码应用渐趋广泛,但与日韩等国相比,中国的二维码发展还远远不够。制约因素除了运营商的支持度外,还有技术、终端适配、盈利模式等方面。炒得很火热的是二维码与O2O(Online To Offline)模式的结合,即利用二维码的读取将线上的用户引流给线下的商家。腾讯很看好这个模式,马化腾称"二维码是线上线下的一个关键入口"。尽管有些人不看好二维码的应用,但无可否认,只要培养了足够多的用户群,再结合良好的商业模式,二维码将成为桥接现实与虚拟最得力的工具之一。
优点
1.高密度编码,信息容量大。
2.编码范围广。
3.容错能力强,具有纠错功能。
4.译码可靠性高。
5.可引入加密措施。
6.成本低,易制作,持久耐用。
如何生成自己的二维码?
1 采用第三方生成工具
草料网 https://cli.im/在线生成 http://www.liantu.com/
微微二维码 http://www.wwei.cn/
2 可以通过代码来生成
废话不多说,直接上代码。
使用zxing生成二维码并前台显示,图片保存后放到指定路径即可
maven依赖
com.google.zxing
core
3.3.1
com.google.zxing
javase
3.3.1
生成二维码代码
public class QRCodeUtil {
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF;
private static QRCodeUtil qrCodeUtil = null;
public QRCodeUtil() {
}
public static QRCodeUtil getInstance() {
if (qrCodeUtil == null) {
qrCodeUtil = new QRCodeUtil();
}
return qrCodeUtil;
}
/**
* 根据参数生成二维码图片。
*
*
* @param imgCharactCode 字符编码, 默认为:UTF-8.
* @param imgWidth 图片宽度, 默认为: 300px
* @param imgHeight 图片高度, 默认为: 300px
* @param strImgFileFoler 图片存储目录
* @param imgFileName 图片名称(如:myTestQrImg.png)
* @param qrContent 二维码内容
* @return 二维码图片的文件对象
*/
public File genQrCodeImg(String imgCharactCode, int imgWidth, int imgHeight, String strImgFileFoler, String imgFileName, String qrContent) {
File imgFullFile = null;
if (strImgFileFoler == null || "".equals(strImgFileFoler) || imgFileName == null || "".equals(imgFileName) || qrContent == null || "".equals(qrContent)) {
return imgFullFile;
}
BitMatrix bitMatrix = null;
try {
// 定义二维码参数的哈希映射表
HashMap hints = new HashMap();
// 编码方式,支持中文
imgCharactCode = (imgCharactCode == null || "".equals(imgCharactCode) ? "UTF-8" : imgCharactCode);
hints.put(EncodeHintType.CHARACTER_SET, imgCharactCode);
// 容错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 二维码边距
hints.put(EncodeHintType.MARGIN, 1);
// 生成点阵
imgWidth = (imgWidth <= 0 ? 300 : imgWidth); // 默认为300px
imgHeight = (imgHeight <= 0 ? 300 : imgHeight); // 默认为300px
bitMatrix = new MultiFormatWriter().encode(qrContent, BarcodeFormat.QR_CODE, imgWidth, imgHeight, hints);
// 创建目录
File fileImgFoler = new File(strImgFileFoler);
if (!fileImgFoler.exists()) {
fileImgFoler.mkdir();
}
// 图片的文件对象
String strImgFullName = fileImgFoler.getPath() + "/" + imgFileName;
imgFullFile = new File(strImgFullName);
// 图片扩展名(即:图片格式)
Path filePath = imgFullFile.toPath();
String imgFormat = imgFileName.substring(imgFileName.lastIndexOf(".") + 1);
// 输出文件
MatrixToImageWriter.writeToPath(bitMatrix, imgFormat, filePath);
} catch (WriterException | IOException e) {
e.printStackTrace();
imgFullFile = null;
}
return imgFullFile;
}
/**
* 生成QRCode二维码
* 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的
* static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";
* 修改为UTF-8,否则中文编译后解析不了
*/
public void encode(String contents, File file, BarcodeFormat format, int width, int height, Map hints) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
writeToFile(bitMatrix, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二维码图片
*
* @param matrix
* @param format 图片格式
* @param file 生成二维码图片位置
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
ImageIO.write(image, format, file);
}
/**
* 生成二维码内容
* @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_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
}
/**
* 解析QRCode二维码
*/
@SuppressWarnings("unchecked")
public void decode(File file) {
try {
BufferedImage image;
try {
image = ImageIO.read(file);
if (image == null) {
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
//解码设置编码方式为:utf-8
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
System.out.println("解析后内容:" + resultStr);
} catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (ReaderException re) {
System.out.println(re.toString());
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
/**
* 测试
*/
public static void main(String[] args) {
String src = "C:\\Users\\tizzy\\Desktop\\SaomaKaipiao\\WeixinKaiPiao_webPage\\src\\main\\resources\\static\\img";
QRCodeUtil test = new QRCodeUtil();
File file = new File(src+"\\test.png");
test.encode("https://www.baidu.com", file, BarcodeFormat.QR_CODE, 200, 200, null);
test.decode(file);
}
}
生成的二维码,用微信扫码后即可访问百度首页
需要可以高性能交流学习群,群里有各有小哥哥小姐姐讲解技术,以及Java大型互联网技术的视频免费分享给大家。
写文不易,小手动一动,据说长得帅的小哥哥小姐姐都关注我了。