如何从id获取二维码? [英] How to get QR code from id?

查看:102
本文介绍了如何从id获取二维码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我有以下方法:

In my controller I have the following method:

import net.glxn.qrgen.QRCode;
import org.marcio.demospringboot.dao.FormationRepository;
import org.marcio.demospringboot.model.Formation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.io.ByteArrayOutputStream;
import java.util.Map;

@Controller
public class FormationController {

@Autowired
private FormationRepository formationRepository;

@RequestMapping (value="formation/qr/{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> qr(@PathVariable final Long id) {

    ByteArrayOutputStream stream = QRCode.from(formationRepository.findOne(id).toString()).stream();

    byte[] bytes = stream.toByteArray();

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);

    return new ResponseEntity<byte[]> (bytes, headers, HttpStatus.CREATED);
}
}

在我的 html 页面中:

In my html page:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>QR Code</title>
</head>
<body>

   <img th:src="@{/formation/qr/${id}}" />

</body>
</html>

这是生成的图像:

我想从您的id"中获取用户数据.我正在使用一个简单的存储库 Spring formationRepository 和库 net.glxn.qrgen.QRCode.该应用程序可以工作,但没有使用有关的用户数据生成二维码"ID".谢谢.

I want to get user data from your "id". I am using a simple repository Spring formationRepository and the library net.glxn.qrgen.QRCode.The application works, but not generate the QR code with the user data regarding the "id". Thanks.

推荐答案

感谢您的帮助和评论.他们都非常有帮助.我可以通过升级到:

Thank you for your help and comments. They were all very helpful. I could solve the problem by upgrading to:

@RequestMapping (value="/formation/qr/{id}", method = RequestMethod.GET)
public HttpEntity<byte[]> qr(@PathVariable Long id) {
    byte[] bytes = QRCode.from(formationRepository.findOne(id).getTheme()
                   .toString()).withSize(120, 120).stream().toByteArray();
    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);
    headers.setContentLength(bytes.length);
    return new ResponseEntity<byte[]> (bytes, headers, HttpStatus.CREATED); 
}

解决方案是byte []bytes = QRCode.from(formation Repository.findOne (id).getTheme().所以我得到了注册内容(getTheme)并在二维码中呈现.

The solution was byte []bytes = QRCode.from(formation Repository.findOne (id).getTheme(). So I got the registration content (getTheme) and presents it in QR code.

这篇关于如何从id获取二维码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆