我有一个@Produces(MediaType.APPLICATION_JSON)
的REST服务。在REST服务中,我创建了BufferedImage
类型的Image,然后将其作为png图像写入ImageIO
。接下来,我将此图像编码为Base64 encoded
字符串。现在,我需要以JSON
格式返回此编码字符串,然后在客户端,需要使用img
标签中的Base64字符串,如下所示:
<img id="captchaImage" src="data:image/png;base64,<%=Base64ImageString%>"/>
我如何以MediaType.APPLICATION_JSON
的形式传递Base64
代码:
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Base64;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@RestService
@Named("CaptchaServlet")
@Path("/reCaptchaImg")
@Produces(MediaType.APPLICATION_JSON)
public class CaptchaServlet implements ICaptchaServlet{
@Override
public Response processRequest(HttpServletRequest request) throws GeneralSecurityException, IOException {
try {
System.out.println("shaan Inside processRequest");
int width = 150;
int height = 50;
//Random string generation happens
Random r = new Random();
int length = 6;
System.out.println("shaan Creating string parameter");
StringBuffer captchaStringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
int baseCharNumber = ((r.nextInt((500-1)+1)+1) % 62);
int charNumber = 0;
if (baseCharNumber < 26) {
charNumber = 65 + baseCharNumber;
}
else if (baseCharNumber < 52){
charNumber = 97 + (baseCharNumber - 26);
}
else {
charNumber = 48 + (baseCharNumber - 52);
}
captchaStringBuffer.append((char)charNumber);
}
String captchaString = captchaStringBuffer.toString();
System.out.println("The random string generated is : "+captchaString);
char[] s3 = captchaString.toCharArray();
System.out.println("shaan1");
//Converting Captcha String to Image
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
System.out.println("shaan2");
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
System.out.println("shaan3");
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
System.out.println("shaan4");
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
System.out.println("shaan5");
GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, height / 2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
System.out.println("shaan6");
String captcha = String.copyValueOf(s3);
System.out.println("shaan7");
int x = 0;
int y = 0;
for (int i = 0; i < s3.length; i++) {
x += 10 + (Math.abs(r.nextInt()) % 15);
y = 20 + Math.abs(r.nextInt()) % 20;
g2d.drawChars(s3, i, 1, x, y);
}
g2d.dispose();
System.out.println("shaan8");
System.out.println("shaan9");
//Sending Image in output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
byte[] imageData = baos.toByteArray();
System.out.println("shaan10" + captcha);
request.getSession().setAttribute("captcha", captcha);
System.out.println("shaan11");
baos.flush();
baos.close();
System.out.println("shaan12");
System.out.println("The imageData is :"+imageData);
//Encoding to Base64
byte[] encoded = Base64.getEncoder().encode(imageData);
System.out.println("The encoded is :"+encoded);
return Response.ok(encoded, MediaType.APPLICATION_JSON).build();
}catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("In Error Catch block");
e.printStackTrace();
return Response.status(Status.BAD_REQUEST).build();
}
}
仅供参考,代码中各行的输出为:
System.out.println("The imageData is :"+imageData);
The imageData is :[B@238bbb96
System.out.println("The encoded is :"+encoded);
The encoded is :[B@1a477e17
答案 0 :(得分:0)
我认为您尝试做的事情行不通,对我来说也没有意义。
数据URI的意思是将图像数据直接放在HTML内部,而不是从外部URL链接。
您应该只返回图像而不将其编码为Base64并直接链接。
您可以将代码更改为此:
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Named("CaptchaServlet")
@Path("/reCaptchaImg")
@Produces("image/png")
public class CaptchaServlet {
@GET
public Response processRequest(@Context HttpServletRequest request) {
try {
System.out.println("shaan Inside processRequest");
int width = 150;
int height = 50;
//Random string generation happens
Random r = new Random();
int length = 6;
System.out.println("shaan Creating string parameter");
StringBuffer captchaStringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
int baseCharNumber = ((r.nextInt((500 - 1) + 1) + 1) % 62);
int charNumber = 0;
if (baseCharNumber < 26) {
charNumber = 65 + baseCharNumber;
} else if (baseCharNumber < 52) {
charNumber = 97 + (baseCharNumber - 26);
} else {
charNumber = 48 + (baseCharNumber - 52);
}
captchaStringBuffer.append((char) charNumber);
}
String captchaString = captchaStringBuffer.toString();
System.out.println("The random string generated is : " + captchaString);
char[] s3 = captchaString.toCharArray();
System.out.println("shaan1");
//Converting Captcha String to Image
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
System.out.println("shaan2");
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
System.out.println("shaan3");
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
System.out.println("shaan4");
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
System.out.println("shaan5");
GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, height / 2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
System.out.println("shaan6");
String captcha = String.copyValueOf(s3);
System.out.println("shaan7");
int x = 0;
int y = 0;
for (int i = 0; i < s3.length; i++) {
x += 10 + (Math.abs(r.nextInt()) % 15);
y = 20 + Math.abs(r.nextInt()) % 20;
g2d.drawChars(s3, i, 1, x, y);
}
g2d.dispose();
System.out.println("shaan8");
System.out.println("shaan9");
//Sending Image in output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
byte[] imageData = baos.toByteArray();
System.out.println("shaan10" + captcha);
request.getSession(true).setAttribute("captcha", captcha);
System.out.println("shaan11");
baos.flush();
baos.close();
System.out.println("The imageData is :" + imageData);
return Response.ok(imageData, "image/png").build();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("In Error Catch block");
e.printStackTrace();
return Response.status(Status.BAD_REQUEST).build();
}
}
}
然后您可以使用如下图像:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<img id="captchaImage" alt="muh" src="/YOUR_REST_PATH/reCaptchaImg"/>
</body>
</html>