我有一个从随机String生成图像的java类,当我单独运行并且图像保存在本地目录中时,java代码工作正常。
现在我创建了一个简单的JSP表单进行注册,如何在JSP中的html <img>
中显示图像。以下是我的代码:
的index.jsp
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="convert.references"%>
<%@page import="java.io.*, java.util.*"%>
<html>
<head>
<title>Echoing HTML Request Parameters</title>
</head>
<body>
<h3>Registering user...</h3>
<form method="get">
Please enter your Username:
<input type="text" name="username"/><br>
Please enter your Email:
<input type="text" name="email"><br>
Please enter your Security Question:
<input type="text" name="qn"><br>
Please enter your Security Answer:
<input type="text" name="ans"><br>
Please enter your Recovery Password:
<input type="password" name="pass"><br>
<input type="submit" value="Enter">
</form>
<%
String username = request.getParameter("username");
String email = request.getParameter("email");
String qn = request.getParameter("qn");
String ans = request.getParameter("ans");
String pass = request.getParameter("pass");
if (username != null) {
references test = new references();
BufferedImage image = test.stringToImage();
File outputfile = new File("saved.png");
ImageIO.write(image, "png", outputfile);
%>
<h3>Your details:</h3>
<ul>
<li><%= username %></li>
<li><%= email %></li>
<li><%= qn %></li>
<li><%= ans %></li>
<li><%= pass %></li>
<li><img src="<%= outputfile%>" alt="Image not found" /></li>
</ul>
<a href="<%= request.getRequestURI() %>">BACK</a>
<%
}
%>
</body>
</html>
references.java(单独工作,能够生成)
package convert;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Scanner;
public class references {
private final int stringLength = 10;
public BufferedImage stringToImage(){
//reference from: http://stackoverflow.com/questions/18800717/convert-text-content-to-image
Scanner reader = new Scanner(System.in);
String imageText = "";
for(int count = 0; count < stringLength; count++){ //consisting of 8 value
Random rand = new Random(); //randomly selected
int randomNumber = rand.nextInt(10); //of 1-9
imageText += randomNumber; //one number at a time for 8 times
}
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 48);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
int width = fm.stringWidth(imageText);
int height = fm.getHeight();
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
g2d.drawString(imageText, 0, fm.getAscent());
g2d.dispose();
/*try{
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
} catch (IOException e) {}*/
return img;
}
}
在jsp上生成图像并显示的任何其他方法?
答案 0 :(得分:-1)
您可以在jsp中使用<img src="saved.png">
。只要saved.png位于您的Web应用程序上下文根目录中。
另一种方法是创建一个servlet,它将处理即时图像生成(无需保存到硬盘驱动器)。然后在jsp中你将servlet引用为图像的src。