![](/img/trans.png)
[英]How to display Text and images both in same JSP page in easiest way?
[英]how to display generated images in jsp page?
<%@page import="com.google.zxing.client.j2se.MatrixToImageWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<h1>Hello World!</h1>
<%@page language='java'%>
<%@page import="java.io.*,java.util.*,java.util.Hashtable,javax.imageio.*,com.google.zxing.*,com.google.zxing.client.*,com.google.zxing.common.*,com.google.zxing.qrcode.decoder.*"%>
<%
String qrCodeData = "Hello World!";
String filePath = "QRCode.png";
String charset = "UTF-8"; // or "ISO-8859-1"
Hashtable hintMap = new Hashtable();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
out.println("QR Code image created successfully!");
%>
<%!
public void createQRCode(String qrCodeData, String filePath,
String charset,Hashtable hintMap, int qrCodeheight, int qrCodewidth)
throws WriterException, IOException {
BitMatrix matrix = new MultiFormatWriter().encode(
new String(qrCodeData.getBytes(charset), charset),
BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight,hintMap);
MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
.lastIndexOf('.') + 1), new File(filePath));
}
%>
</body>
</html>
此代码在tomcat \\ bin目录中生成QR图像。我想将目录更改为jsp文件所在的位置。所以我可以在网页中显示它们请帮助...
我做了一个例子,生成qrcode而不保存它。 请参阅此答案: 使用jsp将动态生成的图像显示到浏览器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.