ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Code128Bean 生成条码图片输出Base64或地址值

2021-12-28 11:34:43  阅读:256  来源: 互联网

标签:条码 Base64 String Code128Bean barCode bean new null


Code128Bean 生成Base64图片返回给前端:

barCode 为想要生成的条码号

public static String generateBarCode(String barCode) {
		String fileName = null;
		if (StringUtils.isEmpty(barCode)) {
			return null;
		}
		ByteArrayOutputStream os=null;

		// 如果想要其他类型的条码(CODE 39, EAN-8...)直接获取相关对象Code39Bean...
		Code128Bean bean = new Code128Bean();
		// 分辨率:值越大条码越长,分辨率越高
		final int dpi = 512;
		// 设置两侧是否加空白
		bean.doQuietZone(true);

		final double modouleWidth = UnitConv.in2mm(3.0f / dpi);
		bean.setModuleWidth(modouleWidth);
		bean.setBarHeight(5);
		// bean.setFontSize(3);
		// bean.setHeight(6);

		// 设置文本位置(包括是否显示)
		bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
		// 设置图片类型
		String format = "image/png";
		try {
			os = new ByteArrayOutputStream();
			// 输出到流
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(os, format, dpi, BufferedImage.TYPE_BYTE_BINARY,
					false, 0);
			// 生成条形码
			bean.generateBarcode(canvas, barCode);
			// 结束绘制
			canvas.finish();
			byte[] bytes = os.toByteArray();
			return Base64Utils.encodeToString(bytes);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (os != null)
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

Code128Bean 生成条码图片返回地址值

public static String generateBarCode(String barCode) {
		String fileName = null;
		if (StringUtils.isEmpty(barCode)) {
			return null;
		}
		FileOutputStream out = null;

		// 如果想要其他类型的条码(CODE 39, EAN-8...)直接获取相关对象Code39Bean...
		Code128Bean bean = new Code128Bean();
		// 分辨率:值越大条码越长,分辨率越高
		final int dpi = 512;
		// 设置两侧是否加空白
		bean.doQuietZone(true);

		final double modouleWidth = UnitConv.in2mm(3.0f / dpi);
		bean.setModuleWidth(modouleWidth);
		bean.setBarHeight(5);
		// bean.setFontSize(3);
		// bean.setHeight(6);

		// 设置文本位置(包括是否显示)
		bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
		// 设置图片类型
		String format = "image/png";
		try {
            //图片存放路径
			fileName = Constants.BAR_CODE_ABS_PATH + barCode + ".png";
			File file = new File(fileName);
			out = new FileOutputStream(file);
			// 输出到流
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, format, dpi, BufferedImage.TYPE_BYTE_BINARY,false, 0);
			// 生成条形码
			bean.generateBarcode(canvas, barCode);
			// 结束绘制
			canvas.finish();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return fileName;
	}

标签:条码,Base64,String,Code128Bean,barCode,bean,new,null
来源: https://blog.csdn.net/weixin_58301478/article/details/122188391

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有