Welcome to My World (www.dgmayor.com)

소프트웨어/자바 GUI & C# 등...

33. 자바 바코드 프린터 프로그램 - 참고자료

dgmayor 2022. 3. 25. 11:24
728x90

[Java(자바)]  barbecue 바코드 생성 라이브러리

이번에 소개할 내용은 barbecue(바베큐) 바코드 생성 라이브러리에 대해서 소개하고자 한다.


1. 적용 라이선스

오픈소스 "BSD License(이하 "비에스디 라이선스")를 적용받는다.

http://barbecue.sourceforge.net 

 


2. 이클립스에서의 셋팅방법

프로젝트 환경설정에서 Liberies에 ClassPath(클래스패스)에 "Add External JARS..."를 클릭해서 등록해서 사용하면 된다.

그림 1. 프로젝트 속성 -> 라이브러리에 클래스패스에 Jar파일 등록하기

그림 2. 프로젝트 속성, Order and Export("읽기: 오더 엔 익스폴트")

jdom("읽기: 제이돔")과 barbecue("읽기: 바베큐")를 체크한다.

Apply("읽기: 어플라이", "뜻: 적용")을 클릭한다.

 


3. 이클립스 - 자바 소스코드

다음처럼 소스코드를 작성하여 적용할 수 있다.

package com.barcode.sample;

import java.io.File;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;

public class Program {

     public static void main(String[] args) {
          String str = "123412341234";
          try {

               Barcode barcode = BarcodeFactory.createCode128(str);
               File file = new File("c:/barcode1.png");

              BarcodeImageHandler.savePNG(barcode, file);
         } catch (Exception e) {
              e.printStackTrace();
         }
    }

}

4. 바코드 생성 모습

실제 적용되는 바코드의 모습이다. 바코드 입출력 장치(이하 "바코드 스캐너라고도 불림. 글자 입력방식의 장치")로
식별하면 인식된다.

 

그림 3. 바코드 생성 모습의 예

 


5. 라이브러리 첨부(Library)

 

 

BSD License를 적용받는다.

 


6. 첨부(Attachment)

 

 

 

자바에 적용된 코드이다. 많은 도움이 되었으면 한다.

 

 

 

[Apache Licence v2.0을 적용받는다.]


* 맺음글(Conclusion)

자바에서 바코드를 생성하는 방법에 대해서 살펴보았다.


* 참고자료(References)

1. Barbecue project, http://barbecue.sourceforge.net, Accessed by 2021-03-09, Last Modified 2021-03-09.

2. OpenJDK (java.net), http://openjdk.java.net, Accessed by 2021-03-09, Last Modified 2021-03-09.

3. Enabling Open Innovation & Collaboration | The Eclipse Foundation, http://www.eclipse.org, Accessed by 2021-03-09, Last Modified 2021-03-09.

4. JDOM, http://www.jdom.org, Accessed by 2021-03-09, Last Modified 2021-03-09.

 

 

 

////////////////////////////////////////////////////////////////////////////////////

회사에서 Barcode에 관한 이슈가 생겼다.

필자가 작업을 하게 되지는 않을꺼 같지만 궁금증이 일어서 Java로 Barcode만드는 법을 찾아보았다.

검색을 하다보니 "역시 Java~~~" 라는 말이 절로 나왔다.

Java에서 Barcode 생성을 지원하는 Barbecue라는 오픈 API가 존재하고 있었던 것이다.

Barbecue API 사이트 바로가기 ==> http://barbecue.sourceforge.net/



1. 지원하는 Barcode Format


 

Code128, Code128A, Code128B, Code128C, UCC128, EAN128, EAN13, Bookland, UPC-A, Standard 2 of 5, Interleave 2 of 5, PDF417, Code 39, Codabar, PostNet




2. 지원하는 Output Format


 

PNG, JPEG, GIF, SVG, and EPS




3. 프린터로 바코드 출력하기 예제


 

01.import java.awt.print.PrinterJob;
02.  
03.import net.sourceforge.barbecue.Barcode;
04.import net.sourceforge.barbecue.BarcodeFactory;
05.  
06.public class TestPrintBarcode {
07.    public static void main(String[] args) {
08.        String str = "http://huikyun.tistory.com";
09.        try {
10.            Barcode barcode = BarcodeFactory.createCode128(str);
11.            PrinterJob job = PrinterJob.getPrinterJob();
12.            job.setPrintable(barcode);
13.  
14.            if (job.printDialog())
15.                job.print();
16.  
17.        } catch (Exception e) {
18.            e.printStackTrace();
19.        }
20.    }
21.}




4. 이미지로 바코드 저장하기 예제


 

01.import java.io.File;
02.  
03.import net.sourceforge.barbecue.Barcode;
04.import net.sourceforge.barbecue.BarcodeFactory;
05.import net.sourceforge.barbecue.BarcodeImageHandler;
06.  
07.public class TestCreateBarcodeImage {
08.    public static void main(String[] args) {
09.        String str = "http://huikyun.tistory.com";
10.        try {
11.            Barcode barcode = BarcodeFactory.createCode128B(str);
12.  
13.            File file = new File("c:/barcode1.png");
14.  
15.            BarcodeImageHandler.savePNG(barcode, file);
16.        } catch (Exception e) {
17.            e.printStackTrace();
18.        }
19.    }
20.}

 




1) 라벨 지정하기


 

barcode.setLabel("Barcode creation test...");

 




2) 텍스트 안보이게 하기


 

barcode.setDrawingText(false);

 




3) 높이 조절하기


 

barcode.setBarHeight(50);

 




4) 배경색 변경하기


 

barcode.setBackground(Color.GREEN);

 




5) 바코드색 변경하기


 

barcode.setForeground(Color.RED);

 




Barbecue API는 Java에서 손쉽게 Barcode를 생성할 수 있게 해주는 공개 API

다양한 바코드 포멧과 충분한 출력 포멧을 지원함을 물론이고

간단하고 짧은 코딩만으로도 사용이 가능한 매우 잘 만들어진 공개 API라는 생각이 든다.

 

728x90