如何从服务器发回图像? (编辑)
How to send back an image from a server? (edited)
我有一个创建二维码的网络服务器。在此过程中,我得到一个 BarcodeQRCode 对象,我可以从中获取图像 (.getImage())。
我不确定如何将这张图片发回给客户。我不想将它保存在文件中,只是为了响应 JSON 请求而发回数据。
有关信息,我有一个类似的案例,我从中获得了一个效果很好的 PDF 文件:
private ByteArrayRepresentation getPdf(String templatePath, JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(..., baos);
// setup PDF content...
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.APPLICATION_PDF);
}
有没有办法做类似的事情:
private ByteArrayRepresentation getImage(JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image qrCode = getQRCode(json); /// return the BarcodeQRCode.getImage()
ImageIO.write(qrCode, "png", baos);
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
}
但这不起作用。我得到:参数不匹配;无法将图像转换为 RenderedImage。
编辑
按如下建议修改后没有编译错误。但是,返回的图像似乎是空的(或者至少不正常)。如果有人知道哪里出了问题,我会放上无错误代码:
@Post("json")
public ByteArrayRepresentation accept(JsonRepresentation entity) throws IOException, DocumentException, WriterException {
JSONObject json = entity.getJsonObject();
return createQR(json);
}
private ByteArrayRepresentation createQR(JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image codeQR = getQRCode(json);
BufferedImage buffImg = new BufferedImage(codeQR.getWidth(null), codeQR.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(codeQR, 0, 0, null);
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
}
private Image getQRCode(JSONObject json) throws IOException, DocumentException, WriterException {
JSONObject url = json.getJSONObject("jsonUrl");
String urls = (String) url.get("url");
BarcodeQRCode barcode = new BarcodeQRCode(urls, 200, 200, null);
Image codeImage = barcode.createAwtImage(Color.BLACK, Color.WHITE);
return codeImage;
}
首先,将图像转换为RenderedImage
:
BufferedImage buffImg = new BufferedImage(qrCode.getWidth(null), qrCode.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(qrCode, 0, 0, null);
如果你使用com.itextpdf.text.Image
你可以使用这个代码
BarcodeQRCode qrcode = new BarcodeQRCode("testo testo testo", 1, 1, null);
Image image = qrcode.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getWidth(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(image, 0, 0, null);
buffImg.getGraphics().dispose();
File file = new File("tmp.png");
ImageIO.write(buffImg, "png", file);
希望对您有所帮助
恩里科
我有一个创建二维码的网络服务器。在此过程中,我得到一个 BarcodeQRCode 对象,我可以从中获取图像 (.getImage())。
我不确定如何将这张图片发回给客户。我不想将它保存在文件中,只是为了响应 JSON 请求而发回数据。 有关信息,我有一个类似的案例,我从中获得了一个效果很好的 PDF 文件:
private ByteArrayRepresentation getPdf(String templatePath, JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(..., baos);
// setup PDF content...
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.APPLICATION_PDF);
}
有没有办法做类似的事情:
private ByteArrayRepresentation getImage(JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image qrCode = getQRCode(json); /// return the BarcodeQRCode.getImage()
ImageIO.write(qrCode, "png", baos);
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
}
但这不起作用。我得到:参数不匹配;无法将图像转换为 RenderedImage。
编辑
按如下建议修改后没有编译错误。但是,返回的图像似乎是空的(或者至少不正常)。如果有人知道哪里出了问题,我会放上无错误代码:
@Post("json")
public ByteArrayRepresentation accept(JsonRepresentation entity) throws IOException, DocumentException, WriterException {
JSONObject json = entity.getJsonObject();
return createQR(json);
}
private ByteArrayRepresentation createQR(JSONObject json) throws IOException, DocumentException, WriterException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image codeQR = getQRCode(json);
BufferedImage buffImg = new BufferedImage(codeQR.getWidth(null), codeQR.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(codeQR, 0, 0, null);
return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
}
private Image getQRCode(JSONObject json) throws IOException, DocumentException, WriterException {
JSONObject url = json.getJSONObject("jsonUrl");
String urls = (String) url.get("url");
BarcodeQRCode barcode = new BarcodeQRCode(urls, 200, 200, null);
Image codeImage = barcode.createAwtImage(Color.BLACK, Color.WHITE);
return codeImage;
}
首先,将图像转换为RenderedImage
:
BufferedImage buffImg = new BufferedImage(qrCode.getWidth(null), qrCode.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(qrCode, 0, 0, null);
如果你使用com.itextpdf.text.Image
你可以使用这个代码
BarcodeQRCode qrcode = new BarcodeQRCode("testo testo testo", 1, 1, null);
Image image = qrcode.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getWidth(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(image, 0, 0, null);
buffImg.getGraphics().dispose();
File file = new File("tmp.png");
ImageIO.write(buffImg, "png", file);
希望对您有所帮助
恩里科