项目刚好需要用到就记录一下
-- 依赖
<!-- 条形码生成 -->
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.avalon.framework/avalon-framework-api -->
<dependency>
<groupId>org.apache.avalon.framework</groupId>
<artifactId>avalon-framework-api</artifactId>
<version>4.3.1</version>
</dependency>
-- 工具类
package com.example.mybatis_plus;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.commons.io.FileUtils;
import org.krysalis.barcode4j.BarcodeException;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* 条形码工具类
*/
public class BarCodeUtils {
/**************************** 条形码 ********************************/
/**
* 生成code128条形码
*
* @param message 要生成的文本
* @param withQuietZone 是否两边留白
* @param hideText 隐藏可读文本
* @param filePath 文件生成的路径
*/
public static void generateBarCode128(String message, boolean withQuietZone, boolean hideText, String filePath) {
Code128Bean bean = new Code128Bean();
// 分辨率
int dpi = 512;
// 设置两侧是否留白
bean.doQuietZone(withQuietZone);
// 设置条形码高度和宽度(不填就是默认的)
// bean.setBarHeight((double) ObjectUtils.defaultIfNull(height, 9.0D));
// if (width != null) {
// bean.setModuleWidth(width);
// }
// 设置文本位置(包括是否显示)
if (hideText) {
bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
}
// 设置图片类型
String format = "image/png";
ByteArrayOutputStream ous = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
// 生产条形码
bean.generateBarcode(canvas, message);
try {
canvas.finish();
//字节码
byte[] bytes = ous.toByteArray();
FileUtils.writeByteArrayToFile(new File(filePath), bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
/**************************** 二维码 ********************************/
/**
* 生成二维码
* @param message 要生成的文本
* @param filePath 文件生成的路径
*/
public static void genQRCode(String message, String filePath){
try {
BarcodeUtil util = BarcodeUtil.getInstance();
DefaultConfiguration cfg = new DefaultConfiguration("barcode");
// Bar code type
DefaultConfiguration child = new DefaultConfiguration("datamatrix");
cfg.addChild(child);
// Human readable text position
DefaultConfiguration attr = new DefaultConfiguration("human-readable");
// //设置高度(无效果)
// attr = new DefaultConfiguration("height");
// attr.setValue(50);
// child.addChild(attr);
//设置二维码宽度
attr = new DefaultConfiguration("module-width");
attr.setValue("2.0");
child.addChild(attr);
BarcodeGenerator gen = util.createBarcodeGenerator(cfg);
//分辨率
int resolution = 300;
// 设置图片类型
String format = "image/png";
ByteArrayOutputStream ous = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, resolution,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
gen.generateBarcode(canvas, message);
canvas.finish();
//字节码
byte[] bytes = ous.toByteArray();
FileUtils.writeByteArrayToFile(new File(filePath), bytes);
} catch (ConfigurationException e) {
e.printStackTrace();
} catch (BarcodeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-- 测试方法
package com.example.mybatis_plus;
public class demo1 {
public static void main(String[] args) {
BarCodeUtils.generateBarCode128("SP0012", true, false,"D:\\aaab.jpg");
}
}
--文档
https://www.cnblogs.com/Yesi/p/11527369.html
官方地址:http://barcode4j.sourceforge.net/
嵌入到 Java 应用程序中(使用 JavaBean API) (sourceforge.net)
https://barcode4j.sourceforge.net/2.1/embedding-bean.html
Zxing 代码示例
pom.xml引入
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
package com.torey.javaAdvanced.mooc4.zxing;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName:BarCodeTest
* @Description:
* @author: Torey
*/
public class BarCodeTest {
/**
* 根据code生成相应的一维码
* @param file 一维码目标文件
* @param code 一维码内容
* @param width 图片宽度
* @param height 图片高度
*/
public static void generateCode(File file, String code,int width,int height){
//定义位图矩阵BitMatrix
BitMatrix matrix=null;
try{
//使用code_128格式进行表面生成100*25的条形码
MultiFormatWriter writer = new MultiFormatWriter();
matrix= writer.encode(code,BarcodeFormat.CODE_128,width,height,null);
// matrix= writer.encode(code,BarcodeFormat.EAN_13,width,height,null);
}catch (Exception ex){ex.printStackTrace();}
//将位图矩阵BitMatrix保存为图片
try(FileOutputStream outStream =new FileOutputStream(file)){
ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix),"png",outStream);
outStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
generateCode(new File("1dcode.png"),"123456789012",500,250);
readCode(new File("1dcode.png"));
}
/**
* 解析一维码
* @param file
*/
private static void readCode(File file) {
try{
BufferedImage read = ImageIO.read(file);
if (null==read) {
return;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(read);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType,Object> hints=new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET,"GBK");
hints.put(DecodeHintType.PURE_BARCODE,Boolean.TRUE);
hints.put(DecodeHintType.TRY_HARDER,Boolean.TRUE);
Result decode = new MultiFormatReader().decode(bitmap, hints);
System.out.println("条形码的内容是:"+decode.getText());
}catch (Exception ex){}
}
}