zxing code_128
时间: 2023-09-25 12:11:29 浏览: 49
ZXing是一个流行的开源条形码/二维码扫描和识别库,支持多种编程语言。如果你想要生成Code 128条形码,可以使用ZXing提供的Java库来实现。
以下是一个简单的示例代码,用于生成Code 128条形码:
```java
import java.util.EnumMap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class Code128Generator {
public static void main(String[] args) {
String code128Data = "1234567890"; // 要生成条形码的数据
int width = 300; // 条形码的宽度
int height = 100; // 条形码的高度
// 设置编码格式和其他参数
EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
try {
// 生成BitMatrix对象
BitMatrix bitMatrix = new MultiFormatWriter().encode(code128Data, BarcodeFormat.CODE_128, width, height, hints);
// 输出到控制台
for (int y = 0; y < height; y++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < width; x++) {
sb.append(bitMatrix.get(x, y) ? " " : "██");
}
System.out.println(sb.toString());
}
} catch (WriterException e) {
e.printStackTrace();
}
}
}
```
运行上述代码将会在控制台输出一个Code 128条形码。你可以将输出结果复制到其他地方,例如使用ASCII码输出到文本文件中。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)