代码之家  ›  专栏  ›  技术社区  ›  Marcio

如何从id获取二维码?

  •  -1
  • Marcio  · 技术社区  · 9 年前

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

    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页面中:

    <!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>
    

    这是生成的图像:

    enter image description here

    我想从你的“id”中获取用户数据。我使用的是一个简单的存储库Spring formationRepository 和图书馆 net.glxn.qrgen.QRCode 。该应用程序可以工作,但不会使用有关“id”的用户数据生成二维码。谢谢

    2 回复  |  直到 9 年前
        1
  •  1
  •   Aviad    9 年前

    忘记“/” @RequestMapping (value="/formation/qr/{id}", method = RequestMethod.GET) 形成前

    编辑:

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

    这应该可以正常工作,这样浏览器就会理解它是一个图像并显示它

        2
  •  0
  •   Marcio    9 年前

    感谢您的帮助和评论。他们都很乐于助人。我可以通过升级到以下位置来解决问题:

    @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),并以二维码呈现。