生成条形码的代码

b/s(asp.net):先添加引用BarcodeLib.dll,然后再后台写代码:
//生成条形码
               BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
                type = BarcodeLib.TYPE.CODE93;
                try
                {
                    if (type != BarcodeLib.TYPE.UNSPECIFIED)
                    {
                        b.IncludeLabel = IsInclud;
                        b.Encode(type, number, System.Drawing.Color.Black, System.Drawing.Color.White, 200, 70).Save (Server.MapPath("../Photos/carCode.jpg"));
                        imgCarNum.ImageUrl = "~/Photos/carCode.jpg?id=" + rd.Next(1, 1000);
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
c/s(winform): 先添加引用BarcodeLib.dll,然后再后台写代码:
       //生成条形码
        BarcodeLib.Barcode b = new BarcodeLib.Barcode();
        bool IsInclude = true;
        private void CreateBarCode()
        {
            BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
            type = BarcodeLib.TYPE.CODE93;
            try
            {
                if (type != BarcodeLib.TYPE.UNSPECIFIED)
                {
                    b.IncludeLabel = IsInclude;
                    pictureBox1.Image = b.Encode(type, "W" + System.DateTime.Now.ToString("yyMMddHHmmss"), Color.Black, Color.White, 200, 100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Java中生成条形码,您可以使用第三方库,比如ZXing(Zebra Crossing)。下面是一个使用ZXing库生成条形码的示例代码: 首先,您需要将ZXing库添加到您的Java项目中。您可以在Maven或Gradle中添加以下依赖项: Maven: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` Gradle: ```groovy implementation 'com.google.zxing:core:3.4.1' implementation 'com.google.zxing:javase:3.4.1' ``` 然后,您可以使用以下代码生成条形码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; 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; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class BarcodeGenerator { private static final String PATH = "barcode.png"; // 条形码保存路径 public static void main(String[] args) { String barcodeData = "123456789"; // 条形码数据 try { generateBarcode(barcodeData, PATH); System.out.println("条形码生成并保存到文件: " + PATH); } catch (Exception e) { System.out.println("生成条形码时出错: " + e.getMessage()); } } private static void generateBarcode(String barcodeData, String filePath) throws WriterException, Exception { int width = 300; // 条形码宽度 int height = 100; // 条形码高度 // 设置条形码参数 com.google.zxing.EncodeHintType hintType = EncodeHintType.ERROR_CORRECTION; com.google.zxing.EncodeHintType hintType2 = EncodeHintType.CHARACTER_SET; java.util.Map<EncodeHintType, Object> hints = new java.util.HashMap<>(); hints.put(hintType, ErrorCorrectionLevel.H); hints.put(hintType2, "UTF-8"); // 生成条形码 BitMatrix bitMatrix = new MultiFormatWriter().encode(barcodeData, BarcodeFormat.CODE_128, width, height, hints); // 将条形码保存为图片文件 File outputFile = new File(filePath); OutputStream outputStream = new FileOutputStream(outputFile); MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream); outputStream.close(); } } ``` 在示例代码中,我们使用ZXing库生成Code 128格式的条形码。您可以将`barcodeData`变量替换为您想要的条形码数据。生成条形码将保存为PNG格式的图片文件,并保存在指定的路径(`PATH`变量)中。 请确保将ZXing库正确添加到项目依赖项中,并根据您的需求进行相应的修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值