java生成条形码
来源:互联网 发布:淘宝卖家无需物流禁忌 编辑:程序博客网 时间:2024/06/02 21:29
java生成条形码
这里直接贴代码,会在代码里面给详解 使用前要先引用 jbbarcode的jar包
<!--jbarcode--> <dependency> <groupId>org.jbarcode</groupId> <artifactId>JBarcode</artifactId> <version>0.2.8</version> </dependency>
代码示例
package barcode; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import org.apache.commons.lang.StringUtils; import org.jbarcode.JBarcode; import org.jbarcode.encode.Code128Encoder; import org.jbarcode.encode.EAN13Encoder; import org.jbarcode.encode.InvalidAtributeException; import org.jbarcode.paint.BaseLineTextPainter; import org.jbarcode.paint.EAN13TextPainter; import org.jbarcode.paint.WidthCodedPainter; import sun.misc.BASE64Encoder; public class BarcodeUtil { /** * 128条形码 * * @param strBarCode * 条形码:0-100位 支持阿拉伯数字 英文大小写 * @param dimension * 商品条形码:尺寸 * @param barheight * 商品条形码:高度 * @return 图片(Base64编码) */ public static String generateBarCode128(String strBarCode,String dimension,String barheight) { try { ByteArrayOutputStream outputStream = null; BufferedImage bi = null; int len = strBarCode.length(); JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance()); // 尺寸,面积,大小 密集程度 productBarcode.setXDimension(Double.valueOf(dimension).doubleValue()); // 高度 10.0 = 1cm 默认1.5cm productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue()); // 宽度 productBarcode.setWideRatio(Double.valueOf(30).doubleValue()); // 是否显示字体 productBarcode.setShowText(true); // 显示字体样式 productBarcode.setTextPainter(BaseLineTextPainter.getInstance()); // 生成二维码 bi = productBarcode.createBarcode(strBarCode); outputStream = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); // System.err.println(encoder.encode(outputStream.toByteArray())); return encoder.encode(outputStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); return "encodeError"; } } /** * 商品条形码 * @param strBarCode * 商品条形码:13位 只能数字 * @param dimension * 商品条形码:尺寸 * @param barheight * 商品条形码:高度 * @return 图片(Base64编码) */ public static String generateBarCode(String strBarCode,String dimension,String barheight) { // isNumeric 是否是数值 // 校验。。。。。 try { ByteArrayOutputStream outputStream = null; BufferedImage bi = null; int len = strBarCode.length(); JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance()); String barCode = strBarCode.substring(0, len - 1); String code = strBarCode.substring(len - 1, len); //校验13位 String checkCode = productBarcode.calcCheckSum(barCode); if (!code.equals(checkCode)) { return "checkCodeError"; } // 尺寸,面积,大小 productBarcode.setXDimension(Double.valueOf(dimension).doubleValue()); // 高度 10.0 = 1cm 默认1.5cm productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue()); // 宽度 productBarcode.setWideRatio(Double.valueOf(25).doubleValue()); // 是否校验13位,默认false productBarcode.setShowCheckDigit(true); //显示字符串内容中是否显示检查码内容 // productBarcode.setShowCheckDigit(true); // 生成二维码 bi = productBarcode.createBarcode(barCode); outputStream = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); // System.err.println(encoder.encode(outputStream.toByteArray())); return encoder.encode(outputStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); return "encodeError"; } } /** * @param args * @throws InvalidAtributeException */ public static void main(String[] args) throws InvalidAtributeException { String encode = BarcodeUtil.generateBarCode("6936983800013","0.5","30"); String encode2 = BarcodeUtil.generateBarCode128("69369833450938430579753045230800013","0.5","30"); System.out.println(encode); System.out.println(encode2); } }
生成图片后,用Base64编码后得到字符串,假如为:"123xyz"页面jsp里<img src="data:image/png;base64,123xyz"/>即可
以下方法是生成二维码图片并返回图片地址
import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import org.jbarcode.JBarcode; import org.jbarcode.encode.Code128Encoder; import org.jbarcode.paint.EAN13TextPainter; import org.jbarcode.paint.WidthCodedPainter; import org.jbarcode.util.ImageUtil; public class Util { /** * 生成商品条形码 * * @param filePath * 商品条形码图片存放路径:../xxx/yyy/ * @param jbarCode * 商品条形码:8位、13位 * @param format * 商品条形码图片格式:.gif/.png/.jpg/.jpeg * @return 图片存放路径+图片名称+图片文件类型 */ public String createBarCode(String filePath, String jbarCode, String format) { String barCodeName = jbarCode + format; try { BufferedImage bi = null; JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance()); bi = productBarcode.createBarcode(jbarCode); saveToJPG(bi, filePath, barCodeName); // 尺寸,面积,大小 密集程度 productBarcode.setXDimension(Double.valueOf("0.5").doubleValue()); // 高度 10.0 = 1cm 默认1.5cm productBarcode.setBarHeight(Double.valueOf("30").doubleValue()); // 宽度 productBarcode.setWideRatio(Double.valueOf(30).doubleValue()); //是否显示字体 productBarcode.setShowText(true); return filePath + barCodeName; } catch (Exception localException) { localException.printStackTrace(); return null; } } /** * 生成JPEG图片 * * @param paramBufferedImage * @param paramString */ @SuppressWarnings("unused") private void saveToJPG(BufferedImage paramBufferedImage, String filePath, String fileName) { saveToFile(paramBufferedImage, filePath, fileName, "jpeg"); } /** * 生成PNG图片 * * @param paramBufferedImage * @param paramString */ @SuppressWarnings("unused") private void saveToPNG(BufferedImage paramBufferedImage, String filePath, String fileName) { saveToFile(paramBufferedImage, filePath, fileName, "png"); } /** * 保存图片文件 * * @param paramBufferedImage * 图片流 * @param filePath * 文件路径 * @param imgName * 图片参数 * @param imgFormat * 图片格式 */ private void saveToFile(BufferedImage paramBufferedImage, String filePath, String imgName, String imgFormat) { try { System.out.println("saveToFile"); FileOutputStream fileOutputStream = null; try { String dirPath = filePath; File dirFile = new File(dirPath); if (!dirFile.exists()) { dirFile.mkdirs(); } String imgPath = dirPath + imgName; fileOutputStream = new FileOutputStream(imgPath); } catch (Exception e) { System.out.println("Create Img File Error:" + e.toString()); } ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat, fileOutputStream, 96, 96); fileOutputStream.close(); } catch (Exception localException) { System.out.println("Save Img File Error:" + localException); localException.printStackTrace(); } } }
阅读全文
0 0
- 条形码 java生成条形码
- java 生成条形码
- java 生成条形码
- Java生成条形码
- java条形码的生成
- java生成条形码
- java 条形码生成类
- java生成条形码
- JAVA生成条形码
- java生成条形码
- jAVA生成条形码
- java 生成条形码
- java 生成条形码
- java 生成条形码
- java生成条形码
- java代码生成条形码
- java生成条形码
- Java 条形码生成(一维条形码)
- mysql 主机免密登录设置
- 某互联网大厂kafka最佳实践
- SwipeBackLayout源码解析
- Go 语言交互性
- JSTL标签库之核心标签
- java生成条形码
- 你必须要知道CNN模型:ResNet
- 【重磅】AI击败顶级德扑玩家的秘密!德扑AI创造者现身reddit,全面解答34个提问,详解Libratus的现状和未来
- RecyclerView 点击item跳转到顶部最快方式,不支持平滑滚动。
- android开发常用的缓存策略详解(1)- LRU缓存策略
- 【配置阿里云】(续)xampp设置多站点配置(顶替默认加载dashboard修改方式)
- clion阅读gmapping
- Retrofit2的使用,1分钟入门
- 【SMS】SMS协议介绍之SGSN