안드로이드

[안드로이드] ZXing을 이용하여 간단하게 바코드 이미지 생성하기

부르스리 2017. 1. 26. 15:16
반응형


바코드 이미지가 필요한 경우가 종종 있다.


스캐너가 필요한 경우도 있고 바코드 생성이 필요한 경우도 있겠다.


대표적인 오픈소스 라이브러리인 ZXing 을 이용하면 스캔도 생성도 가능하다.


https://github.com/zxing/zxing



스캔 없이 생성만 하려면 아주 쉽게 가능하다.


바코드 생성에는 core 라이브러리만 있으면 된다.


https://repo1.maven.org/maven2/com/google/zxing/core/3.3.0/


코어 라이브러리를 다운 받고


아래와 같은 함수를 만들어 주면 끗.


public Bitmap makeBarcode(String str, int width, int height){
Bitmap bmp = null;
com.google.zxing.Writer c9 = new Code128Writer();
try {
BitMatrix bm = c9.encode(str, BarcodeFormat.CODE_128,width, height);
bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
bmp.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}

return bmp;

}


바코드 타입이 여러종류인데 원하는 종류에 맞춰서


Code128 이든 Code93 이든 바꿔서 사용하면 되겠다.




스캔은 git 에서 소스를 받아서 처리해야 하는데 좀 복잡하므로 다음에...


반응형