有关于PC的扫码登录

来源:互联网 发布:unity3d密室逃脱代码 编辑:程序博客网 时间:2024/04/29 05:06

最近写了一下我们公司的App扫码,在PC端登录,再次想跟大家分享一下。

首先,我们需要有一个生成二维码的jar包,现在大多数人用的是 Goole提供的zxing.jar和Qrcode来生成二维码,我这里以goole的zxing为例给大家说一下生成二维码。

我们可以直接到直接下载zxing.jar.如果是用mvn构建的项目,我们可以到mvn的中央仓库中下载对应的配置(),把jar包放在项目中,我们直接在项目中创建一个辅助类

 package com.rbao.east.utils;      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);           }         }              }  
这个里面主要是用来生成我们所需要的二维码,大家不需要过多的研究。

@RequestMapping("GetCodes")public void GetCodes(HttpServletRequest request,HttpServletResponse response) throws Exception{String uuid = UUID.randomUUID().toString().replace("-", "")+System.currentTimeMillis(); //(int)(Math.random() * 100000);String sessionUUID = StringUtil.toString(request.getSession().getAttribute("sessionUUID"));String text ="XXXXXXXX";           int width = 300;          int height = 300;               //二维码的图片格式          String format = "jpg";        Hashtable hints = new Hashtable();        //内容所使用编码          hints.put(EncodeHintType.CHARACTER_SET, "utf-8");          BitMatrix bitMatrix = new MultiFormatWriter().encode(text,                  BarcodeFormat.QR_CODE, width, height, hints);        MatrixToImageWriter.writeToStream(bitMatrix, format,response.getOutputStream() ); }
这有对上面代码的解释,大家看下
,我们可以在扫过之后拿到唯一的uuid做轮询,拿到我们想要的值的时候就可以直接处理调用登录方法了,这个就不在过多的说明了。

至此,二维码的生成也就基本上完成了,有需要的小伙伴可以看下。

0 0