How to get QR code from id?

2019-06-14 16:55发布

问题:

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);
}
}

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>

This is the generated image:

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.

回答1:

Forgot '/' @RequestMapping (value="/formation/qr/{id}", method = RequestMethod.GET) before formation

EDIT:

ByteArrayOutputStream stream = QRCode.from(formationRepository.findOne(id).toString()).stream();
byte[] data = stream.toByteArray();;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
headers.setContentLength(data.length);

return new HttpEntity<byte[]>(data, headers);

This should work in order so the browser will understand it is an image and will show it



回答2:

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); 
}

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.