二维码生成和解析

来源:互联网 发布:大数据平台技术架构图 编辑:程序博客网 时间:2024/05/16 10:57

我不知道二维码怎么生成的,所以问了度娘,写出下面简单的生成方式:

首先我们需要两个jar包: core-3.2.1.jar  和  javase-3.2.1.jar

我是在http://mvnrepository.com/ 这个网站下载的jar包

这是生成二维码的类,它会将指定内容 contents 生成指定格式 format 的二维码图片存放到指定路径 path 

public class QRCodeWriter {private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;private static final int width = 200;private static final int height = 200;private QRCodeWriter() {}/** * 将指定内容生成二维码并存到指定路径 *  * @param contents 需要编成二维码的内容 * @param path 二维码存放路径 * @param format 二维码图片的格式 * @throws IOException  */public static void generateQRCodeToFile(String contents, String path,String format) throws IOException {File file = new File(path);BitMatrix matrix = getMatrix(contents);BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, file)) {throw new IOException("Could not write an image of format " + format + " to " + file);}}/** * 将内容生成二维码并转换为输出流 *  * @param contents 需要编成二维码的内容 * @param stream 二维码的输出流 * @param format 二维码图片格式 * @throws IOException  */public static void generateQRCodeToStream(String contents, OutputStream stream, String format) throws IOException {BitMatrix matrix = getMatrix(contents);BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format " + format);}}/** *  * @param matrix jar包生成的二维码 * @return */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;}/** *  * @param contents 需要编成二维码的内容 * @return */private static BitMatrix getMatrix(String contents) {MultiFormatWriter multiFormatWriter = new MultiFormatWriter();Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);BitMatrix matrix = null;try {matrix = multiFormatWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hints);} catch (WriterException e) {// TODO Auto-generated catch blocke.printStackTrace();}return matrix;}}


这是测试类,它将“我是二维码”生成了二维码图片保存在 e:/image/12.jpg中

public class Test {public static void main(String[] args) throws IOException {QRCodeWriter.generateQRCodeToFile("我是二维码", "e:/image/12.jpg", "jpg");}}
下面是二维码解析的类
public class QRCodeReader {private QRCodeReader(){}/** *  * @param imgPath 需要解析的二维码路径 * @return */public static String decoderQRCode(String imgPath) {String content = null;Result result = getResult(imgPath);content = result.getText();return content;}/** *  * @param imgPath 需要解析的二维码路径 * @return */private static Result getResult(String imgPath){Result result = null;File file = new File(imgPath);try {BufferedImage image = ImageIO.read(file);BufferedImageLuminanceSource luminanceSource = new BufferedImageLuminanceSource(image);HybridBinarizer binarizer = new HybridBinarizer(luminanceSource);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);try {result = new MultiFormatReader().decode(binaryBitmap);} catch (NotFoundException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}return result;}}
下面是测试类,它将解析 e:/image/12.jpg的二维码
public class Test {public static void main(String[] args) {System.out.println(QRCodeReader.decoderQRCode("e:/image/12.jpg"));}}



0 0
原创粉丝点击