|
package io.renren.modules.app.controller;
import java.util.Date;
import sun.misc.BASE64Encoder;
import org.springframework.beans.factory.annotation.Value;
import io.renren.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.crypto.asymmetric.RSA;
import org.apache.commons.io.IOUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.utils.QRCodeUtils;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 二维码测试接口
*
* @author Mark sunlightcs@gmail.com
*/
@RestController
@RequestMapping("/app")
@Api("二维码测试接口")
public class QRCodeTest {
@Value("${url.qrcode}")
private String qrcodeurl; // 读取配置文件中的指定目录
@Autowired
private QRCodeUtils qRCodeUtils;
@GetMapping("qcodetest.jpg")
@ApiOperation("获取二维码图片")
public void getCodeImgTest(HttpServletResponse response, String text)throws IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//获取图片验证码
// BufferedImage image = qRCodeUtils.encode(text);
ServletOutputStream out = response.getOutputStream();
// ImageIO.write(image, "jpg", out);
try{
qRCodeUtils.encode(text, out);
IOUtils.closeQuietly(out);
}catch(Exception e){
e.printStackTrace();
}
}
@GetMapping("qcodetesturl")
@ApiOperation("获取二维码图片地址")
public R getCodeImgUrl(String text)throws IOException {
String imgpath = "/kexiu/webdata/data/qcode";
String rpath = "www.ekexiu.com/data/qcode/";
String imgurl = "";
try{
String file = qRCodeUtils.encode(text, null, imgpath, false);
imgurl = rpath+file;
}catch(Exception e){
e.printStackTrace();
}
return R.ok().put("imgurl", imgurl);
}
@Login
@GetMapping("qrcode.jpg")
@ApiOperation("获取二维码图片")
public void getCodeImg(HttpServletResponse response, String orderId)throws IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//获取图片验证码
String text = qrcodeurl + orderId;
ServletOutputStream out = response.getOutputStream();
try{
qRCodeUtils.encode(text, out);
IOUtils.closeQuietly(out);
}catch(Exception e){
e.printStackTrace();
}
}
@Login
@GetMapping("qrcodebase64.jpg")
@ApiOperation("获取二维码图片")
public R getCodeImgBase64(String attenders_id)throws IOException {
//获取图片验证码
String text = qrcodeurl + attenders_id;
try {
BufferedImage image = QRCodeUtils.myencode(text, null, false);
BASE64Encoder encoder = new BASE64Encoder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(image, "jpg", baos);//写入流中
byte[] bytes = baos.toByteArray();//转换成字节
String jpg_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
//删除 \r\n
jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");
baos.close();
return R.ok().put("qrcodeimg", "data:image/jpg;base64,"+ jpg_base64);
} catch (Exception e) {
e.printStackTrace();
return R.error("未知错误");
}
}
}
|