// ===============================================================================================
// 激活码路径
String jihuomafilePath = “C:\image\JHM.xlsx”;
File excelFile = new File(jihuomafilePath);
JSONArray jihuomaJson = null;
try {
jihuomaJson = ExcelReader.load(excelFile).toJSON();
} catch (Exception e) {
LOGGER.error(“读取Excel失败”, e);
throw new ServiceException(e);
}
// 卡号路径
String kahaoFilePath = "C:\\image\\KH.xlsx";
File kahaoExcelFile = new File(kahaoFilePath);
JSONArray kahaoJson = null;
try {
kahaoJson = ExcelReader.load(kahaoExcelFile).toJSON();
} catch (Exception e) {
LOGGER.error("读取Excel失败", e);
throw new ServiceException(e);
}
// ===============================================================================================
for (int i =0; i < codeArray.length; i++){
// 拿到第一行数据
Object jihuo = jihuomaJson.get(i);
Object kahao = kahaoJson.get(i);
JSONArray jihuoJsonArray = (JSONArray) jihuo;
JSONArray kahaoJsonArray = (JSONArray) kahao;
// 拿到第一列的数据
Object jihuoColumnValue = jihuoJsonArray.get(0);
Object KahaoColumnValue = kahaoJsonArray.get(0);
// 对数据转型
String jihuoValue = String.valueOf(jihuoColumnValue);
String kahaoValue = String.valueOf(KahaoColumnValue);
// ===============================================================================================
String codeVelue;
codeVelue = jihuoValue;
logger.info("================获取到第" + i +"个激活码:"+ codeVelue);
BufferedImage image;
image = getBarCodeImage(codeVelue);
// 创建一张空白图片,作为背景板
BufferedImage image2 = new BufferedImage(2678, 189, BufferedImage.TYPE_3BYTE_BGR);
// 得到空白图片
Graphics graphics = image2.getGraphics();
// 设置颜色填充背景板
graphics.setColor(Color.white);
// 矩形区域大小
graphics.fillRect(0, 0, 2678, 189);
// 将条形码移植
graphics.drawImage(image, 645, 10, null);
// 设置字体
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("宋体",Font.PLAIN, 40));
graphics.drawString(codeVelue, 1091, 185);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image2, "png", outputStream);
logger.info("================将图片流输出到字节数组:outputStream = "+ outputStream);
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream1 = new ByteArrayInputStream(bytes);
try {
Zip4jUtil.addStreamToZip(inputStream1,kahaoValue+".png",vipAcZipFilePath,String.valueOf(acId));
} catch (ZipException e) {
e.printStackTrace();
}
/*String fileUrl = "d:\\test.png";
FileOutputStream fout = new FileOutputStream(fileUrl);
fout.write(bytes);
fout.close();*/
}
===========================================================================
private BufferedImage getBarCodeImage(String codeValue) {
/*// 配置文件设定宽高
Integer width = Integer.valueOf(PropertyConfigurer.getProperties("activation.code.key.max.width"));
if (Objects.isNull(width) || Objects.equals(Integer.valueOf(width), 0)) {
throw new ParameterException("条形码参数错误");
}
Integer height = Integer.valueOf(PropertyConfigurer.getProperties("activation.code.key.min.height"));
if (Objects.isNull(height) || Objects.equals(Integer.valueOf(height), 0)){
throw new ParameterException("条形码参数错误");
}*/
HashMap<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
BarcodeFormat bFmat;
bFmat = BarcodeFormat.CODE_128;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeValue, bFmat, 1389, 142, hints);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
return image;
} catch (Exception e) {
e.printStackTrace();
logger.error("条形码生成错误", e);
}
return null;
}
本文详细介绍了从Excel文件中读取激活码与卡号数据,使用Java进行条形码生成,以及将条形码整合到图片并压缩为zip文件的过程。涉及关键技术包括Excel数据读取、条形码生成、图片处理和文件压缩。
2090

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



