如何从 Azure Java 函数 return 2D BufferedImage?
How to return 2D BufferedImage from a Azure Java Function?
无法弄清楚如何 return 来自无服务器 Azure 函数的缓冲图像。在 Google 上找不到任何现有示例。有没有人知道如何做到这一点?
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
final Integer scale = Integer.parseInt(request.getQueryParameters().get("scale"));
final String text = request.getQueryParameters().get("text");
final String fillColor = request.getQueryParameters().get("fillColor");
BufferedImage bufferedImage = new BufferedImage(30 * scale, 30 * scale, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 30 * scale, 30 * scale);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
g2d.drawString(text, 10, 26);
g2d.dispose();
if (text == null || text.trim().length() == 0) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass the 3 required parameter names.").build();
} else {
OutputStream out = ??????
return request.createResponseBuilder(HttpStatus.OK)
.body(ImageIO.write(bufferedImage, "png", out))
.build();
}
}
我明白了。只是 return 作为原始字节 []:
} else {
try {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "image/png")
.body(toByteArray(bufferedImage, "png"))
.build();
} catch (IOException e) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("There was an error: " + e.getMessage()).build();
}
}
}
public static byte[] toByteArray(BufferedImage bi, String format) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, format, baos);
return baos.toByteArray();
}
我想知道是否有更好的方法。
无法弄清楚如何 return 来自无服务器 Azure 函数的缓冲图像。在 Google 上找不到任何现有示例。有没有人知道如何做到这一点?
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
final Integer scale = Integer.parseInt(request.getQueryParameters().get("scale"));
final String text = request.getQueryParameters().get("text");
final String fillColor = request.getQueryParameters().get("fillColor");
BufferedImage bufferedImage = new BufferedImage(30 * scale, 30 * scale, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 30 * scale, 30 * scale);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
g2d.drawString(text, 10, 26);
g2d.dispose();
if (text == null || text.trim().length() == 0) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass the 3 required parameter names.").build();
} else {
OutputStream out = ??????
return request.createResponseBuilder(HttpStatus.OK)
.body(ImageIO.write(bufferedImage, "png", out))
.build();
}
}
我明白了。只是 return 作为原始字节 []:
} else {
try {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "image/png")
.body(toByteArray(bufferedImage, "png"))
.build();
} catch (IOException e) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("There was an error: " + e.getMessage()).build();
}
}
}
public static byte[] toByteArray(BufferedImage bi, String format) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, format, baos);
return baos.toByteArray();
}
我想知道是否有更好的方法。