Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加二维码名片功能 #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
<maven>3.0</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/mossle/user/web/QRCodeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.mossle.user.web;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;














import org.springframework.web.bind.annotation.RequestParam;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.mossle.user.persistence.domain.UserBase;
import com.mossle.user.persistence.manager.UserBaseManager;


@Controller
@RequestMapping("/user")
public class QRCodeController {
private UserBaseManager userBaseManager;
@RequestMapping("qrcode")
public void qrcode(@RequestParam(value = "id", required = false) Long id,HttpServletResponse response) throws IOException{
System.out.println(id);
UserBase userBase = null;
if (id != null) {
userBase = userBaseManager.get(id);
}else {
userBase = new UserBase();
}

StringBuilder sb = new StringBuilder();
sb.append("BEGIN:VCARD");sb.append("\r\n");
sb.append("VERSION:3.0");sb.append("\r\n");
sb.append("FN:"+userBase.getUsername());sb.append("\r\n");
sb.append("EMAIL;PREF;INTERNET:"+userBase.getEmail());sb.append("\r\n");
sb.append("TEL;CELL;VOICE:"+userBase.getMobile());sb.append("\r\n");
sb.append("END:VCARD");sb.append("\r\n");

ByteArrayOutputStream out = QRCode.from(sb.toString()).to(ImageType.PNG).stream();
response.setContentType("image/png");
response.setContentLength(out.size());
OutputStream outStream = response.getOutputStream();
outStream.write(out.toByteArray());
outStream.flush();
outStream.close();

}
/**
* 编码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encode(String contents, int width, int height, String imgPath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, hints);

MatrixToImageWriter
.writeToFile(bitMatrix, "png", new File(imgPath));

} catch (Exception e) {
e.printStackTrace();
}
}
@Resource
public void setUserBaseManager(UserBaseManager userBaseManager) {
this.userBaseManager = userBaseManager;
}
}
12 changes: 12 additions & 0 deletions webapp/content/user/qrcode.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>
96 changes: 96 additions & 0 deletions webapp/content/user/user-base-list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,90 @@ $(function() {
table.configPageSize('.m-page-size');
});
</script>
<script>
var MouseEvent = function(e){
this.x = e.clientX;
this.y = e.clientY;
}
var Mouse = function(e){
var kdheight = jQuery(document).scrollTop();
mouse = new MouseEvent(e);
leftpos = mouse.x - 200;
var aTop = mouse.y;
var aBottom = $(window).height() - mouse.y;
if(aBottom > 240){
toppos = mouse.y + kdheight;
} else if(aTop >240){
toppos = mouse.y +kdheight -240;
} else {
toppos = kdheight;
}
}
var fix = false;
$(function(){

jQuery(".e1").hover(
function(e){
fix = false;
Mouse(e);
var $cell = $(e.target).closest("tr");
var $childrens = $cell.children();
var $id = $childrens.eq(1);

var src = "${scopePrefix}/user/qrcode.do?id="+$.trim($id.text());
$("#qrimg").attr("src",src);
$("#erweima").css({ top:toppos,left:leftpos }).fadeIn(100);
},function(){
if(!fix){
$("#erweima").hide();
}
})

jQuery(".e1").click(function(){
fix = true;
})
jQuery("#closeImg").click(function(){
$("#erweima").hide();
})
})


</script>
<style type="text/css">
#erweima{
display:none;
left:1px;
top:1px;
width:190px;
color:#000000;
background:#ffffff;
position:absolute;
z-index:9999;
border:1px solid gray;
margin:0 auto;
min-height:160px;
}
#qrimg
{
width:170px;
height:150px;
padding:0 10px 2px 10px;
display:block;
}
#closeImg
{
width:15px;
height:15px;
padding:2px 2px 2px 180px;;
}
#divEwm p
{
margin:0;
padding-top:4px;
display:block;
padding-bottom:10px;
}
</style>
</head>

<body>
Expand Down Expand Up @@ -112,6 +196,7 @@ $(function() {
<th class="sorting" name="nickName">显示名</th>
<th class="sorting" name="status"><spring:message code="user.user.list.status" text="状态"/></th>
<th width="80">&nbsp;</th>
<th>名片</th>
</tr>
</thead>

Expand All @@ -126,9 +211,20 @@ $(function() {
<td>
<a href="user-base-input.do?id=${item.id}" class="a-update"><spring:message code="core.list.edit" text="编辑"/></a>
</td>
<td><img class="e1" title="单击二维码固定" style="cursor:pointer;width:15px;height:15px;" src="${scopePrefix}/widgets/xform/images/visualpharm/th.png"></td>
</tr>
</c:forEach>
</tbody>
<div id="erweima">
<span id="closeImg" style="cursor:pointer">X</span>
<img id="qrimg" src=""/>
<p style='text-align:center;'>
<font size="4pt" style="font-weight:bold;">二维码名片</font>
<br/>
<font size="2pt">("扫一扫"如使用微信)</font>
</p>

</div>
</table>
</form>
</div>
Expand Down
Binary file added webapp/widgets/xform/images/visualpharm/th.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.