You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java生成三行Code128条码的实现方案咨询

How to Generate Three Code128 Barcodes in a Single Image

Got it, let's tweak your existing code to render three Code128 barcodes onto one image. The core idea is to create a single BufferedImage large enough to hold all three barcodes, then draw each barcode at a different vertical position. Here's the step-by-step solution:

Step 1: Adjust the BufferedImage Size

First, make sure your image has enough height to fit three barcodes plus some spacing between them. For example, if each barcode is 200px tall, adding 50px of spacing between each gives a total height of 200*3 + 50*2 = 700px (you can adjust this based on your needs).

Step 2: Create Three Barcode Instances

Generate a separate Barcode object for each of your three code values. Keep the same styling (no text, bar height/width) as your original code for consistency.

Step 3: Draw Each Barcode at Different Y Positions

Use the same Graphics2D context to draw each barcode, shifting the vertical position (y parameter in barcode.draw()) for each one to avoid overlapping.

Full Working Code

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

// Ensure you have the correct imports for your Barcode library
public class TripleBarcodeGenerator {
    public static void main(String[] args) throws Exception {
        // Replace these with your three actual code values
        String codeValue1 = "CODE128-001";
        String codeValue2 = "CODE128-002";
        String codeValue3 = "CODE128-003";

        // Create a BufferedImage large enough for three barcodes (width: 600, height: 700)
        BufferedImage image = new BufferedImage(600, 700, BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g = (Graphics2D) image.getGraphics();

        // First barcode: draw at top position
        Barcode barcode1 = BarcodeFactory.createCode128(codeValue1);
        barcode1.setDrawingText(false);
        barcode1.setBarHeight(200);
        barcode1.setBarWidth(5);
        barcode1.draw(g, 6, 30); // x=6, y=30

        // Second barcode: draw with spacing below the first
        Barcode barcode2 = BarcodeFactory.createCode128(codeValue2);
        barcode2.setDrawingText(false);
        barcode2.setBarHeight(200);
        barcode2.setBarWidth(5);
        int secondBarY = 30 + 200 + 50; // first y + bar height + 50px spacing
        barcode2.draw(g, 6, secondBarY);

        // Third barcode: draw with spacing below the second
        Barcode barcode3 = BarcodeFactory.createCode128(codeValue3);
        barcode3.setDrawingText(false);
        barcode3.setBarHeight(200);
        barcode3.setBarWidth(5);
        int thirdBarY = secondBarY + 200 + 50;
        barcode3.draw(g, 6, thirdBarY);

        // Clean up graphics resources
        g.dispose();

        // Save the combined image to file
        File f = new File("Path\\Bar_Img.jpeg");
        FileOutputStream fileOutputStream = new FileOutputStream(f);
        // Alternative if BarcodeImageHandler doesn't work with pre-drawn image:
        // ImageIO.write(image, "jpeg", fileOutputStream);
        BarcodeImageHandler.writeJPEG(barcode1, fileOutputStream);
        fileOutputStream.close();
    }
}

Key Notes:

  • Spacing Adjustment: The 50-pixel gap between barcodes is flexible—tweak it to match your layout needs.
  • Image Dimensions: If your barcodes are wider than 600px, increase the BufferedImage width to prevent clipping.
  • Resource Cleanup: Always call g.dispose() to free up graphics resources after drawing.
  • Saving Fallback: If BarcodeImageHandler.writeJPEG() doesn’t work with the pre-drawn image, use ImageIO.write(image, "jpeg", fileOutputStream) (just add import javax.imageio.ImageIO;).

内容的提问来源于stack exchange,提问作者Azharudeen Mohammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:53:52