前台:
<script>
$(function(){
var projectId = $V("projectId");
$.ajax({
url : '../../../ZdyPmProjectInfo.project/qrCode?projectId='+projectId,
dataType : 'json',
type : 'html',
success : function(data) {
$('#qrCodeId').html("<img src='"+data.qrCodeUrl+"' />");
}
})
})
</script>
</head>
<body class="z-body-list">
<input type="hidden" id="projectId" value="${projectId}" />
<div id="qrCodeId" />
</body>
</html>
</script>
后台:
@Alias("project/qrCode")
public void qrCode(ZAction za)
throws WriterException, IOException {
JSONObject json = new JSONObject();
String projectId = $V("projectId");
// 移动端打开的链接地址
String surveyUrl = ConstantsAllUtils.SERVER_URL_165 + "&projectId=" + projectId;
int width = 300;
int height = 300;
// 二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(surveyUrl, BarcodeFormat.QR_CODE,
width, height, hints);
// wwwroot_release/.../ upload/qrCode
String filePath = SiteUtil.getSiteRoot(SiteBL.getCurrentSite()) + QRCODE_URL;
// 二维码文件目录
File outputFile = new File(filePath + projectId + "_qrcode." + format);
if (!outputFile.exists()) {
FileUtil.mkdir(filePath);
}
// 生成二维码图片
QrCodeUtil.writeToFile(bitMatrix, format, outputFile);
json.put("qrCodeUrl", SiteUtil.getPreviewPrefix(SiteBL.getCurrentSite()) + QRCODE_URL
+ projectId + "_qrcode." + format);
za.writeJS(json.toJSONString());
}