java实现生成二维码,生成标签

try{
message = "导出二维码成功!";
List BarcodeList = new ArrayList<>();
for(String id : ids.split(",")){
CcCarEntity entity = systemService.getEntity(CcCarEntity.class, id);
ccCarBarcodePDFVO ccarBarcode = new ccCarBarcodePDFVO();
ccarBarcode.setCcSampleName(entity.getCcSampleName());
/* if(StringUtil.isNotEmpty(entity.getCcSubordinateItems())&&entity.getCcSubordinateItems()!=null){
// String subordinate=ccCarService.checkSubordinateItems(entity.getCcSubordinateItems());
// ccarBarcode.setCcSubordinateItems(subordinate);
String subordinate=ccPartsService.getSubordinateItems(entity.getCcSubordinateItems());
ccarBarcode.setCcSubordinateItems(subordinate);
}else{
ccarBarcode.setCcSubordinateItems("");
}*/
ccarBarcode.setCcSubordinateItems(entity.getCcSubordinateItems()!=null?entity.getCcSubordinateItems():"");
ccarBarcode.setCcSampleNumber(entity.getCcSampleNumber());
ccarBarcode.setCcCarVinnum(entity.getCcCarVinnum());
/*ccarBarcode.setPdfPath2(request.getSession().getServletContext().getRealPath("/") + "/webpage/zcsb/equipment/images/LOGO.png");*/

OutputStream sos = response.getOutputStream();
try {
response.setContentType("image/png");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);


//String qrsid = OauthCookie.createdQrcodeSid();
// String qrsid = "aa213123213123123";
// String qrContent = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/qrlogin/" + qrsid + ".htm";
String qrContent = entity.getCcSampleNumber();    //设置二维码的值
BufferedImage bufferedImage = null;
try {
bufferedImage = GenerateQrCode.init().width(98).height(98)   //二维码的长宽
.content(qrContent).generate();
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file=new File(request.getSession().getServletContext().getRealPath("/") + "barcode");   //二维码的存放位置
if(!file.exists()){
file.mkdirs();
}
// ImageIO.write(bufferedImage, "png",new File(request.getSession().getServletContext().getRealPath("/") + Constants.BAR_CODE_PATH + "/" + ".png"));
ImageIO.write(bufferedImage, "png",new File(request.getSession().getServletContext().getRealPath("/") + Constants.BAR_CODE_PATH + "/" + entity.getCcSampleNumber().replace("/","-") + ".png"));
// ImageIO.write(bufferedImage, "png",new File(ResourceUtil.getConfigByName("realpath") + entity.getCcSampleNumber().replace("/","-") + ".png"));
// ImageIO.write(bufferedImage, "png", sos);
// System.out.println(request.getSession().getServletContext().getRealPath("/") + Constants.BAR_CODE_PATH + "/" + entity.getCcSampleNumber().replace("/","-") + ".png");
} finally {
sos.close();
sos.flush();
}
ccarBarcode.setPdfPath(request.getSession().getServletContext().getRealPath("/") + Constants.BAR_CODE_PATH + "/" + entity.getCcSampleNumber().replace("/","-") + ".png");
entity.setPdfPath(request.getSession().getServletContext().getRealPath("/") + Constants.BAR_CODE_PATH + "/" + entity.getCcSampleNumber().replace("/","-") + ".png");
systemService.saveOrUpdate(entity);
BarcodeList.add(ccarBarcode);


}
PDFUtils.generalBarcodePdf(BarcodeList, response);
systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
}catch(Exception e){
e.printStackTrace();
message = "操作失败!";
throw new BusinessException(e.getMessage());
}

你可能感兴趣的:(java)