技术 qq 交流群: JavaDream:251572072
1. 首先介绍一个 API.
? Zxing 是 Google 提供的关于条码
? (一维码, 二维码) 的解析工具, 提供了二维码的生成与解析的方法, 如今我简介一下使用 Java 利用 Zxing 生成与解析二维码
2, 二维码的生成
? 1.1 将 Zxing-core.jar 包增加到 classpath 下.
1.2 二维码的生成须要借助 MatrixToImageWriter 类.
? ? ? 该类是由 Google 提供的. 能够将该类复制到源代码中, 这里我将该类的源代码贴上. 能够直接使用.
- ? ? ?import com.google.zxing.common.BitMatrix;
- ?
- ?import javax.imageio.ImageIO;
- ?import java.io.File;
- ?import java.io.OutputStream;
- ?import java.io.IOException;
- ?import java.awt.image.BufferedImage;
- ?
- ?
- ?public final class MatrixToImageWriter {
- ?
- ? ?private static final int BLACK = 0xFF000000;
- ? ?private static final int WHITE = 0xFFFFFFFF;
- ?
- ? ?private MatrixToImageWriter() {
- }
- ?
- ? ?
- ? ?public static 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;
- ? ?
- }
- ?
- ? ?
- ? ?public static void writeToFile(BitMatrix matrix, String format, File file)
- ? ? ? ?throws IOException {
- ? ? ?BufferedImage image = toBufferedImage(matrix);
- ? ? ?if (!ImageIO.write(image, format, file)) {
- ? ? ? ?throw new IOException("Could not write an image of format" + format + "to" + file);
- ? ? ?
- }
- ? ?
- }
- ?
- ? ?
- ? ?public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
- ? ? ? ?throws IOException {
- ? ? ?BufferedImage image = toBufferedImage(matrix);
- ? ? ?if (!ImageIO.write(image, format, stream)) {
- ? ? ? ?throw new IOException("Could not write an image of format" + format);
- ? ? ?
- }
- ? ?
- }
- ?
- ?
- }
- ?------------------------------------------------------------------------
?2. 以下创建一个 javaproject 来生成名片
?------------------------------------------
? 项目结构例如以下:
- ?ErWerMa
- /ErWerMa/src
- credream
- credream.main
- /ErWerMa/src/credream/main/CreQcode.java
- credream.utils
- /ErWerMa/src/credream/utils/MatrixToImageWriter.java
- /ErWerMa/src/Zxing-core.jar
- -------------------------------------------------------------
- package credream.main;
- import java.io.File;
- import java.util.HashMap;
- import java.util.Map;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.common.BitMatrix;
- import credream.utils.MatrixToImageWriter;
- public class CreQcode {
- public static void main(String[] args) {
- try {
- //1. 以下是我測试用的一个 format
- //BEGIN:VCARD
- //"VERSION:3.0
- //N: 李德伟
- //EMAIL:[email protected]
- //TEL:12345678912
- //TEL;CELL:12345678912
- //ADR: 山东济南齐鲁软件园
- //ORG: 济南
- //TITLE: 软件 project 师
- //URL:http://blog.csdn.net/lidew521
- //NOTE: 呼呼測试下吧..
- .
- //END:VCARD"
- //2. 依据測试结果推理
- //---------------------------------------
- // 測试结果不加回车是不行的这种话会出现故障
- // 就是扫描出来以后会没有内容
- // 这里能够看出, 微信解析二维码的方式
- //-------------------------------------------------
- ? ? ? ? //3. 測试 \ n 能够被二维码识别
- // ? 这里也是有原因的, 由于微信扫描二维码后会进行二次加工,
- // ? 这里加工的时候, 是用 java 代码的, 由于是 Android 系统, 所以在
- // ?java 中的回车是 \ n, 因此这里就要用 \ n 来切割
- ? ? String content = "BEGIN:VCARD\n" +
- ? ? "VERSION:3.0\n" +
- ? ? "N: 李德伟 \ n" +
- ? ? "EMAIL:[email protected]\n" +
- ? ? "TEL:12345678912" +
- ? ? "TEL;CELL:12345678912" +
- ? ? "ADR: 山东济南齐鲁软件园 \ n" +
- ? ? "ORG:" +
- ? ? "济南 \ n" +
- ? ? "TITLE: 软件 project 师 \ n" +
- ? ? "URL:http://blog.csdn.net/lidew521\n" +
- ? ? "NOTE: 呼呼測试下吧...\n" +
- ? ? "END:VCARD";
- ? ? String path = "D:/4S";
- ? ??
- ? ? MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
- ? ??
- ? ??
- ? ??
- ? ??
- ? ? Map hints = new HashMap();
- ? ? hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- ? ? BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
- ? ? File file1 = new File(path,"名片. jpg");?
- ? ? MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
- ? ??
- } catch (Exception e) {
- ? ? e.printStackTrace();
- }
- }
- }
- -----------------------------------------------------------------------
3. 搞定了这样就能够了
? 用微信扫描后, 会自己主动出现保存页面, 然后用户能够选择保存到通讯录中
------------------------------------------------------------------
4. 分析:
? a. 这里有些亲们可能以为, 扫描出来是个页面, 就以为二维码里存放了一个 jsp 相似的页面
? ? 事实上这样想是错误的
? b. 非常好证明, 由于用微信或者 qq 能够把扫描的内容加到通讯录里, 可是用普通的二维码扫描软件是不能够的
? ? 这就说明, 事实上二维码中存放的不过一串被格式化了的数据而已, 这段数据在上文中有提到
? c. 微信之所以能够扫描成联系人的格式, 是由于微信内部做了处理, 这里处理过程应该是这种
? ? 首先微信会调用二维码 API 来解析二维码
? d. 其次微信会用解析出来的内容填充的实现准备好的 jsp 页面中, 然后, 在这个 jsp 页面中, 就存在一段
? ? 能够跟微信程序中 java 方法, 联系的 JS 代码, 这样就能够通过微信中的 java 方法, 调用 Android 系统的通讯录了
? ? (这个部分说的可能不太清楚, Android 中 JS 和 java 方法是能够相互调用传值的, 大家能够学习下 Android, 通讯录部分, 也能够參考下
? ? ? 我曾经写过的 Android 教程)
来源: http://www.bubuko.com/infodetail-3065822.html