Commit 1638949f authored by liuchao's avatar liuchao

no message

parent 8ff5707b
...@@ -240,6 +240,12 @@ ...@@ -240,6 +240,12 @@
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.2.20</version> <version>1.2.20</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency> <dependency>
<groupId>net.sourceforge.jexcelapi</groupId> <groupId>net.sourceforge.jexcelapi</groupId>
......
...@@ -26,7 +26,7 @@ import org.springframework.web.servlet.ModelAndView; ...@@ -26,7 +26,7 @@ import org.springframework.web.servlet.ModelAndView;
import com.qiankun.annotation.Auth; import com.qiankun.annotation.Auth;
import com.qiankun.controller.base.BaseController; import com.qiankun.controller.base.BaseController;
import com.qiankun.dao.core.Hints; import com.qiankun.dao.core.Hints;
import com.qiankun.entity.Product; import com.qiankun.entity.Contacts;
import com.qiankun.entity.User; import com.qiankun.entity.User;
import com.qiankun.pages.IPageList; import com.qiankun.pages.IPageList;
import com.qiankun.service.UserService; import com.qiankun.service.UserService;
...@@ -90,22 +90,11 @@ public class UserController extends BaseController<Object>{ ...@@ -90,22 +90,11 @@ public class UserController extends BaseController<Object>{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(date); String year = sdf.format(date);
Long code = null; Contacts product = new Contacts();
User last = userService.findLastUserByGroup(userVo.getGroups());
if (last != null) {
code = Long.valueOf(last.getUserId().substring(6))+1;
} else {
code = Long.valueOf(userVo.getGroups() + "000001");
}
String userId = "IE" + year + code;
user.setUserId(userId);
Product product = new Product();
product.setUser(user); product.setUser(user);
product.setCreateTime(date); user.getContacts().add(product);
user.getProduct().add(product);
view.getModelMap().put("status", 1); view.getModelMap().put("status", 1);
} else { } else {
...@@ -117,7 +106,7 @@ public class UserController extends BaseController<Object>{ ...@@ -117,7 +106,7 @@ public class UserController extends BaseController<Object>{
} }
} }
user = userService.findById(userVo.getId()); user = userService.findById(userVo.getId());
if (StringUtils.isNotBlank(userVo.getTeamName()) && !userVo.getTeamName().equals(user.getTeamName())) { if (StringUtils.isNotBlank(userVo.getTeamName())) {
Boolean userIsExistUserName = userService.validateUserExistByTeamName(userVo.getTeamName()); Boolean userIsExistUserName = userService.validateUserExistByTeamName(userVo.getTeamName());
if (userIsExistUserName) { if (userIsExistUserName) {
view.getModel().put("message", "userIsExist_teamName"); view.getModel().put("message", "userIsExist_teamName");
...@@ -129,7 +118,6 @@ public class UserController extends BaseController<Object>{ ...@@ -129,7 +118,6 @@ public class UserController extends BaseController<Object>{
user.setPwd(MD5.digest(userVo.getPwd())); user.setPwd(MD5.digest(userVo.getPwd()));
} }
user.setUserName(userVo.getUserName()); user.setUserName(userVo.getUserName());
user.setTeamName(userVo.getTeamName());
user.setEmail(userVo.getEmail()); user.setEmail(userVo.getEmail());
...@@ -223,19 +211,16 @@ public class UserController extends BaseController<Object>{ ...@@ -223,19 +211,16 @@ public class UserController extends BaseController<Object>{
ModelAndView view = new ModelAndView(); ModelAndView view = new ModelAndView();
User userSession = SessionUtils.getUser(request); User userSession = SessionUtils.getUser(request);
User user = userService.findById(userSession.getId()); User user = userService.findById(userSession.getId());
Product product = new Product(); Contacts product = new Contacts();
Set<Product> productSet = user.getProduct(); Set<Contacts> productSet = user.getContacts();
Iterator<Product> its = productSet.iterator(); Iterator<Contacts> its = productSet.iterator();
if (its.hasNext()) { if (its.hasNext()) {
product = its.next(); product = its.next();
} }
product.setName(productVo.getName()); product.setName(productVo.getName());
product.setContent1(productVo.getContent1());
product.setContent2(productVo.getContent2());
product.setContent3(productVo.getContent3());
product.setUpdateTime(new Date()); product.setUpdateTime(new Date());
user.getProduct().clear(); user.getContacts().clear();
user.getProduct().add(product); user.getContacts().add(product);
userService.save(user); userService.save(user);
view.getModelMap().put("message","success"); view.getModelMap().put("message","success");
return view; return view;
...@@ -255,9 +240,9 @@ public class UserController extends BaseController<Object>{ ...@@ -255,9 +240,9 @@ public class UserController extends BaseController<Object>{
ModelAndView view = new ModelAndView(); ModelAndView view = new ModelAndView();
User userSession = SessionUtils.getUser(request); User userSession = SessionUtils.getUser(request);
User user = userService.findById(userSession.getId()); User user = userService.findById(userSession.getId());
Product product = new Product(); Contacts product = new Contacts();
Set<Product> productSet = user.getProduct(); Set<Contacts> productSet = user.getContacts();
Iterator<Product> its = productSet.iterator(); Iterator<Contacts> its = productSet.iterator();
if (its.hasNext()) { if (its.hasNext()) {
product = its.next(); product = its.next();
} }
...@@ -328,7 +313,7 @@ public class UserController extends BaseController<Object>{ ...@@ -328,7 +313,7 @@ public class UserController extends BaseController<Object>{
User userSession = SessionUtils.getUser(request); User userSession = SessionUtils.getUser(request);
ResourceBundle rb = ResourceBundle.getBundle("system"); ResourceBundle rb = ResourceBundle.getBundle("system");
String path = rb.getString("file_path") + userSession.getUserId() + "/"; String path = rb.getString("file_path") + "/";
File f = new File(path); File f = new File(path);
if (!f.exists() && !f.isDirectory()) { if (!f.exists() && !f.isDirectory()) {
f.mkdir(); f.mkdir();
...@@ -380,27 +365,16 @@ public class UserController extends BaseController<Object>{ ...@@ -380,27 +365,16 @@ public class UserController extends BaseController<Object>{
User user = userService.findById(userSession.getId()); User user = userService.findById(userSession.getId());
Product product = new Product(); Contacts product = new Contacts();
Set<Product> productSet = user.getProduct(); Set<Contacts> productSet = user.getContacts();
Iterator<Product> its = productSet.iterator(); Iterator<Contacts> its = productSet.iterator();
if (its.hasNext()) { if (its.hasNext()) {
product = its.next(); product = its.next();
} }
if ("enroll".equals(type)) {
product.setEnroll(fileFullName);
}
if ("report".equals(type)) {
product.setReport(fileFullName);
}
if ("ppt".equals(type)) {
product.setPpt(fileFullName);
}
if ("attachment".equals(type)) {
product.setAttachment(fileFullName);
}
product.setUpdateTime(new Date()); product.setUpdateTime(new Date());
user.getProduct().clear(); user.getContacts().clear();
user.getProduct().add(product); user.getContacts().add(product);
userService.save(user); userService.save(user);
return view; return view;
...@@ -415,9 +389,7 @@ public class UserController extends BaseController<Object>{ ...@@ -415,9 +389,7 @@ public class UserController extends BaseController<Object>{
try { try {
User userSession = SessionUtils.getUser(request); User userSession = SessionUtils.getUser(request);
ResourceBundle rb = ResourceBundle.getBundle("system"); ResourceBundle rb = ResourceBundle.getBundle("system");
if (StringUtils.isBlank(userId)) {
userId = userSession.getUserId();
}
String path = rb.getString("file_path") + userId + "/" + type + "/" + fileName; String path = rb.getString("file_path") + userId + "/" + type + "/" + fileName;
response.setContentType("application/octet-stream"); response.setContentType("application/octet-stream");
......
package com.qiankun.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Activity {
@Id
@GenericGenerator(name = "systemUUID", strategy = "uuid2")
@GeneratedValue(generator = "systemUUID")
private String id;
private String name; //活动名称
private String type; //采样编号
@Lob
private String addr; //地点
@Lob
private String content; //详细信息
@ManyToOne
private User review; //数据初审者;
private Date day; //活动时间
private long max; //最大人数;
private String crowd; //适用人群
private boolean available; //活动是否可用
private String contacts; //联系人
private String tel; //联系电话
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE })
@JsonIgnore
private Set<User> user = new HashSet<User>();
private Date updateTime = new Date();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public User getReview() {
return review;
}
public void setReview(User review) {
this.review = review;
}
public Date getDay() {
return day;
}
public void setDay(Date day) {
this.day = day;
}
public long getMax() {
return max;
}
public void setMax(long max) {
this.max = max;
}
public String getCrowd() {
return crowd;
}
public void setCrowd(String crowd) {
this.crowd = crowd;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public String getContacts() {
return contacts;
}
public void setContacts(String contacts) {
this.contacts = contacts;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Set<User> getUser() {
return user;
}
public void setUser(Set<User> user) {
this.user = user;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
package com.qiankun.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Blood {
@Id
@GenericGenerator(name = "systemUUID", strategy = "uuid2")
@GeneratedValue(generator = "systemUUID")
private String id;
private String bloodId; //采样编号
@Lob
private String bloodAddr; //采血地点
@ManyToOne
private User review; //数据初审者;
@ManyToOne
private User user = new User();
private Date updateTime = new Date();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBloodId() {
return bloodId;
}
public void setBloodId(String bloodId) {
this.bloodId = bloodId;
}
public String getBloodAddr() {
return bloodAddr;
}
public void setBloodAddr(String bloodAddr) {
this.bloodAddr = bloodAddr;
}
public User getReview() {
return review;
}
public void setReview(User review) {
this.review = review;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
package com.qiankun.entity; package com.qiankun.entity;
import java.util.Date; import java.util.Date;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
...@@ -18,7 +17,7 @@ import org.hibernate.annotations.GenericGenerator; ...@@ -18,7 +17,7 @@ import org.hibernate.annotations.GenericGenerator;
@Entity @Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product { public class Contacts {
@Id @Id
@GenericGenerator(name = "systemUUID", strategy = "uuid2") @GenericGenerator(name = "systemUUID", strategy = "uuid2")
...@@ -27,31 +26,22 @@ public class Product { ...@@ -27,31 +26,22 @@ public class Product {
private String name; private String name;
@Lob private String relation; //关系
private String content1; //摘要
@Lob
private String content2; //作品思路和应用的理论方法
@Lob
private String content3; //作品的创新性
private String enroll; //报名表上传 private String mobile;//手机
private String report; //报告上传 private String qq;
private String ppt; //ppt上传 private String weixin;
private String attachment; //附件上传 @Lob
private String addr; //地址
@ManyToOne @ManyToOne
private User user = new User(); private User user = new User();
private Date createTime = new Date();
private Date updateTime = new Date(); private Date updateTime = new Date();
private Boolean isSubmit = false; //是否提交
public String getId() { public String getId() {
return id; return id;
...@@ -69,68 +59,52 @@ public class Product { ...@@ -69,68 +59,52 @@ public class Product {
this.name = name; this.name = name;
} }
public String getContent1() { public String getRelation() {
return content1; return relation;
}
public void setContent1(String content1) {
this.content1 = content1;
}
public String getContent2() {
return content2;
}
public void setContent2(String content2) {
this.content2 = content2;
}
public String getContent3() {
return content3;
} }
public void setContent3(String content3) { public void setRelation(String relation) {
this.content3 = content3; this.relation = relation;
} }
public String getEnroll() { public String getMobile() {
return enroll; return mobile;
} }
public void setEnroll(String enroll) { public void setMobile(String mobile) {
this.enroll = enroll; this.mobile = mobile;
} }
public String getReport() { public String getQq() {
return report; return qq;
} }
public void setReport(String report) { public void setQq(String qq) {
this.report = report; this.qq = qq;
} }
public String getPpt() { public String getWeixin() {
return ppt; return weixin;
} }
public void setPpt(String ppt) { public void setWeixin(String weixin) {
this.ppt = ppt; this.weixin = weixin;
} }
public String getAttachment() { public String getAddr() {
return attachment; return addr;
} }
public void setAttachment(String attachment) { public void setAddr(String addr) {
this.attachment = attachment; this.addr = addr;
} }
public Date getCreateTime() { public User getUser() {
return createTime; return user;
} }
public void setCreateTime(Date createTime) { public void setUser(User user) {
this.createTime = createTime; this.user = user;
} }
public Date getUpdateTime() { public Date getUpdateTime() {
...@@ -140,22 +114,7 @@ public class Product { ...@@ -140,22 +114,7 @@ public class Product {
public void setUpdateTime(Date updateTime) { public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public Boolean getIsSubmit() {
return isSubmit;
}
public void setIsSubmit(Boolean isSubmit) {
this.isSubmit = isSubmit;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
......
...@@ -11,6 +11,7 @@ import javax.persistence.GeneratedValue; ...@@ -11,6 +11,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Inheritance; import javax.persistence.Inheritance;
import javax.persistence.InheritanceType; import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import org.hibernate.annotations.Cache; import org.hibernate.annotations.Cache;
...@@ -29,12 +30,38 @@ public class User { ...@@ -29,12 +30,38 @@ public class User {
@GeneratedValue(generator = "systemUUID") @GeneratedValue(generator = "systemUUID")
private String id; private String id;
private String userId; //编号 private String userName; //用户名
private String userName; //单位名称 private String name; //姓名
private String teamName; //团队名称 private String gender; //性别
private String mobile; //手机 private String birthdate; //出生年月
private String paperType; //证件类型
private String paperId; //证件号
private String nation; //民族
private String nativePlace; //籍贯
private String nationality; //国籍
private String education; //学历
private String domicilePlace; //户籍所在地
private String addr; //经常居住地
private String unit; //单位、学校
private String unitAddr; //单位、学校地址
private String unitTel; //单位、学校电话
private String profession; //职业
private String mobile; //本人手机
private String email; //电话
private String tel; //固定电话
private String qq;
private String weixin; //微信号
private Integer donateBloodCount; //无偿献血次数
private String bloodType; //血型
private Integer height; //身高
private Integer weight; //体重
@Lob
private String remark; //备注
private String pwd; //密码 private String pwd; //密码
private String email;
private String role; //角色 1.管理员 2.志愿者 3.预注册用户 private String role; //角色 1.管理员 2.志愿者 3.预注册用户
...@@ -42,7 +69,17 @@ public class User { ...@@ -42,7 +69,17 @@ public class User {
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL}, orphanRemoval = true, mappedBy = "user") @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL}, orphanRemoval = true, mappedBy = "user")
@JsonIgnore @JsonIgnore
private Set<Product> product = new HashSet<Product>(); private Set<Contacts> contacts = new HashSet<Contacts>();
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL}, orphanRemoval = true, mappedBy = "user")
@JsonIgnore
private Set<Blood> blood = new HashSet<Blood>();
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL}, orphanRemoval = true, mappedBy = "review")
@JsonIgnore
private Set<Blood> review = new HashSet<Blood>();
private Boolean isRemove=false;//是否删除用户,默认false private Boolean isRemove=false;//是否删除用户,默认false
...@@ -52,12 +89,7 @@ public class User { ...@@ -52,12 +89,7 @@ public class User {
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() { public String getUserName() {
return userName; return userName;
} }
...@@ -76,12 +108,7 @@ public class User { ...@@ -76,12 +108,7 @@ public class User {
public void setPwd(String pwd) { public void setPwd(String pwd) {
this.pwd = pwd; this.pwd = pwd;
} }
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getEmail() { public String getEmail() {
return email; return email;
} }
...@@ -100,11 +127,11 @@ public class User { ...@@ -100,11 +127,11 @@ public class User {
public void setCreateTime(Date createTime) { public void setCreateTime(Date createTime) {
this.createTime = createTime; this.createTime = createTime;
} }
public Set<Product> getProduct() { public Set<Contacts> getContacts() {
return product; return contacts;
} }
public void setProduct(Set<Product> product) { public void setContacts(Set<Contacts> Contacts) {
this.product = product; this.contacts = Contacts;
} }
public Boolean getIsRemove() { public Boolean getIsRemove() {
return isRemove; return isRemove;
...@@ -112,6 +139,156 @@ public class User { ...@@ -112,6 +139,156 @@ public class User {
public void setIsRemove(Boolean isRemove) { public void setIsRemove(Boolean isRemove) {
this.isRemove = isRemove; this.isRemove = isRemove;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public String getPaperType() {
return paperType;
}
public void setPaperType(String paperType) {
this.paperType = paperType;
}
public String getPaperId() {
return paperId;
}
public void setPaperId(String paperId) {
this.paperId = paperId;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getNativePlace() {
return nativePlace;
}
public void setNativePlace(String nativePlace) {
this.nativePlace = nativePlace;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getDomicilePlace() {
return domicilePlace;
}
public void setDomicilePlace(String domicilePlace) {
this.domicilePlace = domicilePlace;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getUnitAddr() {
return unitAddr;
}
public void setUnitAddr(String unitAddr) {
this.unitAddr = unitAddr;
}
public String getUnitTel() {
return unitTel;
}
public void setUnitTel(String unitTel) {
this.unitTel = unitTel;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getWeixin() {
return weixin;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public Integer getDonateBloodCount() {
return donateBloodCount;
}
public void setDonateBloodCount(Integer donateBloodCount) {
this.donateBloodCount = donateBloodCount;
}
public String getBloodType() {
return bloodType;
}
public void setBloodType(String bloodType) {
this.bloodType = bloodType;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Set<Blood> getBlood() {
return blood;
}
public void setBlood(Set<Blood> blood) {
this.blood = blood;
}
public Set<Blood> getReview() {
return review;
}
public void setReview(Set<Blood> review) {
this.review = review;
}
......
package com.qiankun.utils;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* <p>Title:QRCodeUtil </p>
* <p>Description: 二维码生成工具类</p>
* @author Administrator
* @version
* @since
*/
public final class QRCodeUtil extends LuminanceSource {
private static final Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);
// 二维码颜色
private static final int BLACK = 0xFF000000;
// 二维码颜色
private static final int WHITE = 0xFFFFFFFF;
private final BufferedImage image;
private final int left;
private final int top;
public QRCodeUtil(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
}
public QRCodeUtil(BufferedImage image, int left, int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new QRCodeUtil(image, this.left + left, this.top + top, width, height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new QRCodeUtil(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}
/**
* @param matrix
* @return
*/
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 生成二维码图片
*
* @param matrix
* @param format
* @param file
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 生成二维码图片流
*
* @param matrix
* @param format
* @param stream
* @throws IOException
*/
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
/**
* 根据内容,生成指定宽高、指定格式的二维码图片
*
* @param text 内容
* @param width 宽
* @param height 高
* @param format 图片格式
* @return 生成的二维码图片路径
* @throws Exception
*/
private static String generateQRCode(String text, int width, int height, String format, String pathName)
throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);// 指定纠错等级
hints.put(EncodeHintType.MARGIN, 1); // 白边大小,取值范围0~4
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File(pathName);
writeToFile(bitMatrix, format, outputFile);
return pathName;
}
/**
* 输出二维码图片流
*
* @param text 二维码内容
* @param width 二维码宽
* @param height 二维码高
* @param format 图片格式eg: png, jpg, gif
* @param response HttpServletResponse
* @throws Exception
*/
public static void generateQRCode(String text, int width, int height, String format, HttpServletResponse response)
throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);// 指定纠错等级
hints.put(EncodeHintType.MARGIN, 1); // 白边大小,取值范围0~4
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
writeToStream(bitMatrix, format, response.getOutputStream());
}
/**
* 解析指定路径下的二维码图片
*
* @param filePath 二维码图片路径
* @return
*/
public static String parseQRCode(String filePath) {
String content = "";
try {
File file = new File(filePath);
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new QRCodeUtil(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatReader formatReader = new MultiFormatReader();
Result result = formatReader.decode(binaryBitmap, hints);
logger.info("result 为:" + result.toString());
logger.info("resultFormat 为:" + result.getBarcodeFormat());
logger.info("resultText 为:" + result.getText());
// 设置返回值
content = result.getText();
} catch (Exception e) {
logger.error(e.getMessage());
}
return content;
}
public static void main(String[] args) {
String text = "cd64a479-5c2d-494a-8a86-4bcf0647a596"; // 随机生成验证码
System.out.println("随机码: " + text);
int width = 100; // 二维码图片的宽
int height = 100; // 二维码图片的高
String format = "png"; // 二维码图片的格式
try {
// 生成二维码图片,并返回图片路径
String pathName = generateQRCode(text, width, height, format, "D:/new.png");
System.out.println("生成二维码的图片路径: " + pathName);
String content = parseQRCode(pathName);
System.out.println("解析出二维码的图片的内容为: " + content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment