## 二維碼
1. 在代碼編制上巧妙地利用構成計算機內部邏輯基礎的“0”、“1”比特流的概念,使用若干個與二進制相對應的幾何形體來表示文字數值信息,通過圖象輸入設備或光電掃描設備自動識讀以實現信息自動處理。
2. 在許多種類的二維條碼中,常用的碼制有:
Data Matrix,MaxiCode, Aztec,QR Code, Vericode,PDF417,Ultracode,Code 49,Code 16K等,
3. QR code碼是1994年由日本DW公司發明。QR來自英文「Quick Response」的縮寫,即快速反應的意思,源自發明者希望QR碼可讓其內容快速被解碼。QR碼最常見于日本、韓國;并為目前日本最流行的二維空間條碼。但二維碼的安全性也正備受挑戰,帶有惡意軟件和病毒正成為二維碼普及道路上的絆腳石。發展與防范二維碼的濫用正成為一個亟待解決的問題。

糾錯能力越高,二維碼存儲的信息就越少,對二維碼的清晰度要求就越低。

## 1. 生成二維碼
### 1.1 直接用MatrixToImageWriter寫入文件或者流

* 有問題的
* 依賴
~~~
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
~~~
### 1.2
~~~
package net.aexit.galaxy.earth.mediashare.qrcode;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
/**
* Created by dailin on 2017/11/30.
* 生成二維碼工具類
*/
public class QrGenerator {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private int width = 300; //二維碼寬度
private int height = 300;//二維碼高度
private String format = "png"; // 二維碼圖片保存格式
private String contents = ""; //掃描二維碼時產生的內容
/**
* 生成BitMatrix二維碼
* @return BitMatrix
*/
public BitMatrix CreatQRCode() throws WriterException {
//定義二維碼的參數
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設置二維碼的容錯等級
hints.put(EncodeHintType.MARGIN, 2);//邊距
return new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
}
public BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 將生產的二維碼寫入圖片文件
* @param file
* @throws IOException
*/
public void writeToFile(File file) throws IOException, WriterException {
BitMatrix matrix = CreatQRCode();
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 將生成的二維碼以二進制數組的形式返回
*/
public byte[] writeToBytes() throws WriterException, IOException {
BitMatrix matrix = CreatQRCode();
byte[] bytes = null;
try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, byteArray)) {
throw new IOException("faild to write an image to bytes");
}
bytes = byteArray.toByteArray();
}
return bytes;
}
/**
* 將生成的二維碼以base64編碼的形式返回
*/
public String writeToBase64() throws WriterException, IOException {
BitMatrix matrix = CreatQRCode();
String base64String=null;
try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, byteArray)) {
throw new IOException("faild to write an image to bytes");
}
byte[] bytes = byteArray.toByteArray();
base64String = Base64.getEncoder().encodeToString(bytes);
}
return base64String;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}
~~~
2. 實體類
~~~
package JavaTest.erweima;
import javax.security.auth.Subject;
/**
* Created by dailin on 2017/11/30.
*/
public class StudentInfo {
private String name ;
private String IdCard ;
private String CarType ;
private String number;
private int Subject_One_grade;
private int Subject_One_total;
private int Subject_Two_grade;
private int Subject_Two_total;
private int Subject_Three_grade;
private int Subject_Three_total;
private int Subject_Four_grade;
private int Subject_Four_total;
private Boolean IsGraduate;
public Boolean getGraduate() {
return IsGraduate;
}
public void setGraduate(Boolean graduate) {
IsGraduate = graduate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return IdCard;
}
public void setIdCard(String idCard) {
IdCard = idCard;
}
public String getCarType() {
return CarType;
}
public void setCarType(String carType) {
CarType = carType;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getSubject_One_grade() {
return Subject_One_grade;
}
public void setSubject_One_grade(int subject_One_grade) {
Subject_One_grade = subject_One_grade;
}
public int getSubject_One_total() {
return Subject_One_total;
}
public void setSubject_One_total(int subject_One_total) {
Subject_One_total = subject_One_total;
}
public int getSubject_Two_grade() {
return Subject_Two_grade;
}
public void setSubject_Two_grade(int subject_Two_grade) {
Subject_Two_grade = subject_Two_grade;
}
public int getSubject_Two_total() {
return Subject_Two_total;
}
public void setSubject_Two_total(int subject_Two_total) {
Subject_Two_total = subject_Two_total;
}
public int getSubject_Three_grade() {
return Subject_Three_grade;
}
public void setSubject_Three_grade(int subject_Three_grade) {
Subject_Three_grade = subject_Three_grade;
}
public int getSubject_Three_total() {
return Subject_Three_total;
}
public void setSubject_Three_total(int subject_Three_total) {
Subject_Three_total = subject_Three_total;
}
public int getSubject_Four_grade() {
return Subject_Four_grade;
}
public void setSubject_Four_grade(int subject_Four_grade) {
Subject_Four_grade = subject_Four_grade;
}
public int getSubject_Four_total() {
return Subject_Four_total;
}
public void setSubject_Four_total(int subject_Four_total) {
Subject_Four_total = subject_Four_total;
}
@Override
public String toString() {
String graduation = "未結業";
String NEW_LINE = System.getProperty("line.separator");
if (IsGraduate==true)
graduation = "結業";
return "學員姓名:" + name + NEW_LINE +
"學員身份證號:" + IdCard + NEW_LINE +
"學員車型:" + CarType + NEW_LINE +
"學員編號:" + number + NEW_LINE +
"科目一:" + Subject_One_grade + "/" + Subject_One_total + NEW_LINE +
"科目二:" + Subject_Two_grade + "/" + Subject_Two_total + NEW_LINE +
"科目三:" + Subject_Three_grade + "/" + Subject_Three_total + NEW_LINE +
"科目四:" + Subject_Four_grade + "/" + Subject_Four_total + NEW_LINE +
"是否結業:" + graduation;
}
}
~~~
3. 測試類
~~~
package JavaTest.erweima;
import com.google.zxing.common.BitMatrix;
import java.io.*;
/**
* Created by dailin on 2017/11/30.
*/
public class test {
public static void main(String[] args) {
StudentInfo student = new StudentInfo();
student.setName("代林");
student.setIdCard("220122199108105513");
student.setCarType("大奔");
student.setNumber("123");
student.setSubject_One_grade(25);
student.setSubject_One_total(100);
student.setSubject_Two_grade(25);
student.setSubject_Two_total(100);
student.setSubject_Three_grade(25);
student.setSubject_Three_total(100);
student.setSubject_Four_grade(25);
student.setSubject_Four_total(100);
student.setGraduate(true);
String str = student.toString();
File file = new File("C:\\Users\\Administrator\\Desktop\\dialin.png");
ErWeiMa erweima = new ErWeiMa();
erweima.setContents(str);
try {
erweima.writeToFile(file); // 寫成圖片文件
byte[] bytes = erweima.writeToStream();
OutputStream baos2 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\aixin.png");
baos2.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
}
~~~
## 改正后
1. 底層工具類要拋出具體的異常,不要try catch攔截,否則上層無法捕捉錯誤信息
2. 沒關閉流
~~~
/**
* 生成BitMatrix二維碼
* @return BitMatrix
*/
public BitMatrix CreatQRCode() throws WriterException {
//定義二維碼的參數
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設置二維碼的容錯等級
hints.put(EncodeHintType.MARGIN, 2);//邊距
return new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
}
public BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 將生產的二維碼寫入圖片文件
* @param file
* @throws IOException
*/
public void writeToFile(File file) throws IOException, WriterException {
BitMatrix matrix = CreatQRCode();
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 將生成的二維碼以二進制數組的形式返回
*/
public byte[] writeToBytes() throws WriterException, IOException {
BitMatrix matrix = CreatQRCode();
byte[] bytes = null;
// 這么寫,完事后自動關閉流
try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, byteArray)) {
throw new IOException("faild to write an image to bytes");
}
bytes = byteArray.toByteArray();
}
return bytes;
}
~~~
- Docker
- 什么是docker
- Docker安裝、組件啟動
- docker網絡
- docker命令
- docker swarm
- dockerfile
- mesos
- 運維
- Linux
- Linux基礎
- Linux常用命令_1
- Linux常用命令_2
- ip命令
- 什么是Linux
- SELinux
- Linux GCC編譯警告:Clock skew detected. 錯誤解決辦法
- 文件描述符
- find
- 資源統計
- LVM
- Linux相關配置
- 服務自啟動
- 服務器安全
- 字符集
- shell腳本
- shell命令
- 實用腳本
- shell 數組
- 循環與判斷
- 系統級別進程開啟和停止
- 函數
- java調用shell腳本
- 發送郵件
- Linux網絡配置
- Ubuntu
- Ubuntu發送郵件
- 更換apt-get源
- centos
- 防火墻
- 虛擬機下配置網絡
- yum重新安裝
- 安裝mysql5.7
- 配置本地yum源
- 安裝telnet
- 忘記root密碼
- rsync+ crontab
- Zabbix
- Zabbix監控
- Zabbix安裝
- 自動報警
- 自動發現主機
- 監控MySQL
- 安裝PHP常見錯誤
- 基于nginx安裝zabbix
- 監控Tomcat
- 監控redis
- web監控
- 監控進程和端口號
- zabbix自定義監控
- 觸發器函數
- zabbix監控mysql主從同步狀態
- Jenkins
- 安裝Jenkins
- jenkins+svn+maven
- jenkins執行shell腳本
- 參數化構建
- maven區分環境打包
- jenkins使用注意事項
- nginx
- nginx認證功能
- ubuntu下編譯安裝Nginx
- 編譯安裝
- Nginx搭建本地yum源
- 文件共享
- Haproxy
- 初識Haproxy
- haproxy安裝
- haproxy配置
- virtualbox
- virtualbox 復制新的虛擬機
- ubuntu下vitrualbox安裝redhat
- centos配置雙網卡
- 配置存儲
- Windows
- Windows安裝curl
- VMware vSphere
- 磁盤管理
- 增加磁盤
- gitlab
- 安裝
- tomcat
- Squid
- bigdata
- FastDFS
- FastFDS基礎
- FastFDS安裝及簡單實用
- api介紹
- 數據存儲
- FastDFS防盜鏈
- python腳本
- ELK
- logstash
- 安裝使用
- kibana
- 安準配置
- elasticsearch
- elasticsearch基礎_1
- elasticsearch基礎_2
- 安裝
- 操作
- java api
- 中文分詞器
- term vector
- 并發控制
- 對text字段排序
- 倒排和正排索引
- 自定義分詞器
- 自定義dynamic策略
- 進階練習
- 共享鎖和排它鎖
- nested object
- 父子關系模型
- 高亮
- 搜索提示
- Redis
- redis部署
- redis基礎
- redis運維
- redis-cluster的使用
- redis哨兵
- redis腳本備份還原
- rabbitMQ
- rabbitMQ安裝使用
- rpc
- RocketMQ
- 架構概念
- 安裝
- 實例
- 好文引用
- 知乎
- ACK
- postgresql
- 存儲過程
- 編程語言
- 計算機網絡
- 基礎_01
- tcp/ip
- http轉https
- Let's Encrypt免費ssl證書(基于haproxy負載)
- what's the http?
- 網關
- 網絡IO
- http
- 無狀態網絡協議
- Python
- python基礎
- 基礎數據類型
- String
- List
- 遍歷
- Python基礎_01
- python基礎_02
- python基礎03
- python基礎_04
- python基礎_05
- 函數
- 網絡編程
- 系統編程
- 類
- Python正則表達式
- pymysql
- java調用python腳本
- python操作fastdfs
- 模塊導入和sys.path
- 編碼
- 安裝pip
- python進階
- python之setup.py構建工具
- 模塊動態導入
- 內置函數
- 內置變量
- path
- python模塊
- 內置模塊_01
- 內置模塊_02
- log模塊
- collections
- Twisted
- Twisted基礎
- 異步編程初探與reactor模式
- yield-inlineCallbacks
- 系統編程
- 爬蟲
- urllib
- xpath
- scrapy
- 爬蟲基礎
- 爬蟲種類
- 入門基礎
- Rules
- 反反爬蟲策略
- 模擬登陸
- problem
- 分布式爬蟲
- 快代理整站爬取
- 與es整合
- 爬取APP數據
- 爬蟲部署
- collection for ban of web
- crawlstyle
- API
- 多次請求
- 向調度器發送請求
- 源碼學習
- LinkExtractor源碼分析
- 構建工具-setup.py
- selenium
- 基礎01
- 與scrapy整合
- Django
- Django開發入門
- Django與MySQL
- java
- 設計模式
- 單例模式
- 工廠模式
- java基礎
- java位移
- java反射
- base64
- java內部類
- java高級
- 多線程
- springmvc-restful
- pfx數字證書
- 生成二維碼
- 項目中使用log4j
- 自定義注解
- java發送post請求
- Date時間操作
- spring
- 基礎
- spring事務控制
- springMVC
- 注解
- 參數綁定
- springmvc+spring+mybatis+dubbo
- MVC模型
- SpringBoot
- java配置入門
- SpringBoot基礎入門
- SpringBoot web
- 整合
- SpringBoot注解
- shiro權限控制
- CommandLineRunner
- mybatis
- 靜態資源
- SSM整合
- Aware
- Spring API使用
- Aware接口
- mybatis
- 入門
- mybatis屬性自動映射、掃描
- 問題
- @Param 注解在Mybatis中的使用 以及傳遞參數的三種方式
- mybatis-SQL
- 逆向生成dao、model層代碼
- 反向工程中Example的使用
- 自增id回顯
- SqlSessionDaoSupport
- invalid bound statement(not found)
- 脈絡
- beetl
- beetl是什么
- 與SpringBoot整合
- shiro
- 什么是shiro
- springboot+shrio+mybatis
- 攔截url
- 枚舉
- 圖片操作
- restful
- java項目中日志處理
- JSON
- 文件工具類
- KeyTool生成證書
- 兼容性問題
- 開發規范
- 工具類開發規范
- 壓縮圖片
- 異常處理
- web
- JavaScript
- 基礎語法
- 創建對象
- BOM
- window對象
- DOM
- 閉包
- form提交-文件上傳
- td中內容過長
- 問題1
- js高級
- js文件操作
- 函數_01
- session
- jQuery
- 函數01
- data()
- siblings
- index()與eq()
- select2
- 動態樣式
- bootstrap
- 表單驗證
- 表格
- MUI
- HTML
- iframe
- label標簽
- 規范編程
- layer
- sss
- 微信小程序
- 基礎知識
- 實踐
- 自定義組件
- 修改自定義組件的樣式
- 基礎概念
- appid
- 跳轉
- 小程序發送ajax
- 微信小程序上下拉刷新
- if
- 工具
- idea
- Git
- maven
- svn
- Netty
- 基礎概念
- Handler
- SimpleChannelInboundHandler 與 ChannelInboundHandler
- 網絡編程
- 網絡I/O
- database
- oracle
- 游標
- PLSQL Developer
- mysql
- MySQL基準測試
- mysql備份
- mysql主從不同步
- mysql安裝
- mysql函數大全
- SQL語句
- 修改配置
- 關鍵字
- 主從搭建
- centos下用rpm包安裝mysql
- 常用sql
- information_scheme數據庫
- 值得學的博客
- mysql學習
- 運維
- mysql權限
- 配置信息
- 好文mark
- jsp
- jsp EL表達式
- C
- test