How to create a jwt image within a spring controller
Hey, I have a spring controller as shown below which is there to create certain image.
public class ImageMapController extends AbstractController {
protected ModelAndView handleRequestInternal( HttpServletRequest request,
HttpServletResponse response ) throws Exception
{
File file = new File("C:/Users/Rahul/Pictures/EmptyParkingLot.jpg");
BufferedImage image= javax.imageio.ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage buffer =
new BufferedImage(width*2,
height,
BufferedImage.TYPE_INT_RGB);
Graphics g=buffer.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setBackground(Color.white);
g2.setColor(Color.red);
g2.drawString("Item 1",0,0);
g2.drawImage(image, 40, 40, null);
g2.drawString("Item 2",width,0);
g2.drawImage(image, 40 + width, 40, null);
ServletOutputStream out1 = response.getOutputStream();
JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(out1);
encoder.encode(buffer);
return new ModelAndView("parkingimage");
}
}
The above code works really fine and it displays the image I want 开发者_StackOverflowin parkingimage.jsp. But as opposed to flushing it out as jsp I just want to use it as an image within my jsp with <img>
tag OR how do I include this jsp within another jsp as apart from displaying the image I have some text that I want to display in the same jsp I'm displaying the image.
Any help is appreciated. Thanks in advance. Regards, Serotonin Chase
You need to create another controller to generate a page, and <img src = "...">
in that page should point to the controller that generates images (these URLs can contain parameters if needed).
Another option is to use Data URI, but it has limited size and browser support.
精彩评论