zxing-android 라이브러리를 이용한 QR 코드 생성

android 에서 QR코드를 생성하는 하는 법을 알아 보도록 하겠습니다.


compile 'com.journeyapps:zxing-android-embedded:3.5.0'
build.gradle의 dependencies에 zxing라이브러리를 추가 합니다.


QR코드는 숫자 최대 7089 자, 영문자와 숫자[코드표가 따로 존재] 최대 4296 자, 8비트 바이트 최대 2953 바이트, 한자 1817 자를 담을 수 있습니다. 문자열을 받아와 QR코드를 생성 해보겠습니다.
 
public static Bitmap generateQRCode(String contents) {
        Bitmap bitmap = null;

        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            bitmap = toBitmap(qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, 200, 200));
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    private static Bitmap toBitmap(BitMatrix matrix) {
        int height = matrix.getHeight();
        int width = matrix.getWidth();
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        return bmp;
    }
1) QRcodeWriter 객체를 생성합니다.
2) QR코드에 들어갈 문자정보, 바코드포맷, QR코드 가로사이즈, QR코드 세로사이즈 셋팅
3) Bitmap 생성

위의 세가지 단계를 거쳐 간단하게 QR코드를 생성 할수 있습니다.




댓글

주간 인기글

카드뉴스 마케팅 팁

[ubuntu] 신규 계정에 sudo 권한 추가하기

[Spring Boot] JPA + Pageable 을 이용한 페이징 처리

HTML 템플릿을 인쇄용으로 가공하기

[정보] 인스타그램은 당신의 소리를 '듣고' 있을 수도 있습니다