把上面的完整序列填充到相应规格的二维码矩阵的区域中
6. 掩摸: 将掩摸图形用于符号的编码区域,使得二维码图形中的深色和浅色(黑色和白色)区域能够比率最优的分布。
7. 格式和版本信息: 生成格式和版本信息放入相应区域内。
版本7-40都包含了版本信息,没有版本信息的全为0。二维码上两个位置包含了版本信息,它们是冗余的。
版本信息共18位,6X3的矩阵,其中6位时数据为,如版本号8,数据位的信息时 001000,后面的12位是纠错位。
练习实现二维码编码与解码过程:(调用 三方包zxing 其中的解码与编码的API)
编码过程:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public
static
void
encode(String content, String format, String filePath) {
try
{
Hashtable hints =
new
Hashtable();
//设置编码类型
hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_ENCODING);
//编码
BitMatrix bitMatrix =
new
QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE, DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_HEIGHT,hints);
//输出到文件,也可以输出到流
File file =
new
File(filePath);
MatrixToImageWriter.writeToFile(bitMatrix, format, file);
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(WriterException e1) {
e1.printStackTrace();
}
}
|
解码过程:
01
02
03
04
05
06
07
08
|
BufferedImage image = ImageIO.read(file);
//读取文件
LuminanceSource source =
new
BufferedImageLuminanceSource(image);
BinaryBitmap bitmap =
new
BinaryBitmap(
new
HybridBinarizer(
source));
//解码
Result result =
new
MultiFormatReader().decode(bitmap);
String resultStr = result.getText();
System.out.println(resultStr);
|