Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

public class Main {
    public static byte[] getPrintBarCode(String data, int symbology, int height, int width, int textposition) {

        if (symbology < 0 || symbology > 8) {
            return new byte[] { 0x0A };
        }
        if (width < 2 || width > 6) {
            width = 2;
        }
        if (textposition < 0 || textposition > 3) {
            textposition = 0;
        }
        if (height < 1 || height > 255) {
            height = 162;
        }

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try {
            buffer.write(new byte[] { 0x1D, 0x66, 0x01, 0x1D, 0x48, (byte) textposition, 0x1D, 0x77, (byte) width,
                    0x1D, 0x68, (byte) height, 0x0A });

            byte[] barcode = data.getBytes();

            if (symbology == 8) {
                buffer.write(new byte[] { 0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, 0x42 });
            } else {
                buffer.write(new byte[] { 0x1D, 0x6B, (byte) (symbology + 0x41), (byte) barcode.length });
            }

            buffer.write(barcode);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toByteArray();
    }
}