如何在HTTP响应体(使用Spark)中发送QR码的PNG? [英] How can I send a PNG of a QR-code in a HTTP response body (with Spark)?

查看:292
本文介绍了如何在HTTP响应体(使用Spark)中发送QR码的PNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成QR码图像,将其转换为PNG并将其作为HTTP响应返回给我的客户端。

I want to generate a QR-code image, convert it to PNG and return it as a HTTP response to my client.

为了生成QR码我使用ZXing。我已经使用 FileOutputStream 使用 MatrixToImageWriter.writeToStream(...)编写了转换部分。这就像一个魅力。

To generate the QR-code I use ZXing. I already tested the conversion part by writing using a FileOutputStream with MatrixToImageWriter.writeToStream(...). That works like a charm.

我目前使用的网络框架是 Spark (版本1.1.1)。返回句柄(...) -method被设置为响应主体。我在这里做错了什么?

The web framework I am currently using is Spark (Version 1.1.1). The return of the handle(...)-method is set as the response body. What am I doing wrong here?

使用当前的解决方案我得到无法显示图像http:// localhost:4567 / qrcode因为它在使用Firefox执行GET请求时包含错误

With the current solution I get The image "http://localhost:4567/qrcode" cannot be displayed because it contains errors when performing the GET-request with Firefox.

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import static spark.Spark.get;
import spark.Request;
import spark.Response;
import spark.Route;

import com.google.gson.Gson;

import com.google.common.io.BaseEncoding;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class App {
    public static void main(String[] args) {
        get(new Route("/qrcode") {

            @Override
            public Object handle(Request request, Response response) {
                // Test data
                QrData data = new QrData("test");

                // Data is wrapped in JSON
                String json = new Gson().toJson(data);

                // Transform JSON to QR-code PNG byte string
                String qrString = "";
                try {
                    qrString = generatePngQrCode(json);
                } catch (Exception e) {
                    e.printStackTrace();
                } 

                // Set response parameters
                response.type("image/png");
                response.status(200);

                // Return response body
                return qrString;
            }
        });
    }

    public String generatePngQrCode(String content) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // ZXing QR-code encoding
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 400, 400);

        // Convert to PNG image and write to stream
        MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);

        // Encode to Base 64  
        return BaseEncoding.base64().encode(outputStream.toByteArray());
    }
}


推荐答案

使用response.getRaw获取应该用于将PNG写入的OutputStream(使用MatrixToImageWriter)。

Use response.getRaw to obtain an OutputStream that should be used to write the PNG to (using MatrixToImageWriter).

这篇关于如何在HTTP响应体(使用Spark)中发送QR码的PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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