摘要
这次分享给大家的是,如何将导入的excel数据,将某些字段生成base64位的二维码。
效果图

后台开发
1、pom.xml 相关jar引入

2、代码
@PostMapping("/importData")
@ResponseBody
public AjaxResult importData(MultipartFile file, boolean updateSupport, String code) throws Exception
{
System.out.println(code);
ExcelUtil<SqJcitem> util = new ExcelUtil<SqJcitem>(SqJcitem.class);
List<SqJcitem> userList = util.importExcel(file.getInputStream());
Date date= new Date();
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
String time=dateFormat.format(date);
System.out.println("---"+time);
for(int i=0;i<userList.size();i++)
{
if(StringUtils.isNotBlank(userList.get(i).getCode()))
{
SqJcitem n=userList.get(i);
List<SqJcitem> sqjcitemList= sqjcitemDao.listByCode(n.getCode());
if(sqjcitemList.size()==0)
{
SqJcitem sqjcitem=new SqJcitem();
sqjcitem.setCode(code+"-"+n.getCode()+"-"+n.getJcode()+"-"+time);
sqjcitem.setItems(n.getItems());
sqjcitem.setItemsname(n.getItemsname());
sqjcitem.setMethodcode(n.getMethodcode());
sqjcitem.setMethondname(n.getMethondname());
sqjcitem.setFxitems(n.getFxitems());
sqjcitem.setImg(QrCodeUtils.creatRrCode(sqjcitem.getCode(), 200,200));//生成base64的二维码
sqjcitemDao.insert(sqjcitem);
}
}
}
return AjaxResult.success("");
}
工具类 QrCodeUtils
package com.stylefeng.guns.modular.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
public class QrCodeUtils {
public static String creatRrCode(String contents, int width, int height) {
String binary = null;
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
contents, BarcodeFormat.QR_CODE, width, height, hints);
// 1、读取文件转换为字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage image = toBufferedImage(bitMatrix);
//转换成png格式的IO流
ImageIO.write(image, "png", out);
byte[] bytes = out.toByteArray();
// 2、将字节数组转为二进制
BASE64Encoder encoder = new BASE64Encoder();
binary = encoder.encodeBuffer(bytes).trim();
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return binary;
}
/**
* image流数据处理
*
* @author ianly
*/
public 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) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}
public static void main(String[] args) {
String binary = QrCodeUtils.creatRrCode("https://blog.youkuaiyun.com/ianly123", 200,200);
System.out.println(binary);
}
}
1129

被折叠的 条评论
为什么被折叠?



