투케이2K

1049. (Android/Java) 안드로이드 zxing 라이브러리 사용해 QR 및 Barcode 이미지 파일 (png, jpg) 스캔 수행 본문

Android

1049. (Android/Java) 안드로이드 zxing 라이브러리 사용해 QR 및 Barcode 이미지 파일 (png, jpg) 스캔 수행

투케이2K 2026. 1. 1. 12:21
728x90

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------

- 언어 : Java / Kotlin


- 개발 툴 : AndroidStudio


- 기술 구분 : Android / zxing / QR / Barcode


- 사전) ZXing 라이브러리 설명 정리 : 

  >> zxing 이란 안드로이드에서 QR 코드 생성 및 QR 스캔, Barcode 스캔에 도움을 주는 라이브러리 입니다

  >> zxing 특징 : 

    - Intents를 통해 사용할 수 있습니다​

    - UI 및 논리의 고급 사용자 정의를 위해 활동에 포함할 수 있습니다.

    - 가로 또는 세로 모드에서 스캔을 수행할 수 있습니다.

    - 카메라는 빠른 시작 시간을 위해 백그라운드 스레드에서 관리됩니다.


- 사전) 안드로이드 Build.gradle 설정 사항 : 

    android {

        // [컴파일 버전]
        compileSdk 34

        // [Config 셋팅]
        defaultConfig {
            // ----------------------------
            applicationId "com.example.appproject" // 앱 아이디
            // ----------------------------
            versionCode 1 // 빌드 버전
            // ----------------------------
            versionName '1.0.1' // 빌드 네임
            // ----------------------------
            minSdk 24 // 최소 빌드 버전
            // ----------------------------
            targetSdk 34 // TODO 타겟 빌드 버전
            // ----------------------------
        }

        // [컴파일 자바 버전 지정]
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        // [아파치 http 사용 설정]
        useLibrary ('org.apache.http.legacy')
    }

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[설명 정리]
// --------------------------------------------------------------------------------------

// ----------------------------------------------------------------------
// TODO [SEARCH FAST] : [Observable] : [QR 및 Barcode 저장 된 이미지 스캔 메소드]
// ----------------------------------------------------------------------
// TODO [필요 설치 라이브러리] :
// ----------------------------------------------------------------------
/*
* implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
* implementation "com.google.zxing:core:3.3.0"
* */
// ----------------------------------------------------------------------
// TODO [호출 방법 소스 코드]
// ----------------------------------------------------------------------
/*
try {

    AssetManager assetManager = getAssets();
    InputStream inputStream = assetManager.open("testQrImg.png");

    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException ignored) {}
    }


    C_Ui_View.getQrBarcodeImageFileScan(A_Webview.this, bitmap);
}
catch (Exception e){
    e.printStackTrace();
}
*/
// ----------------------------------------------------------------------
public static String getQrBarcodeImageFileScan(Context mContext, Bitmap bitmap){

    // [리턴 변수 선언]
    String returnData = "";
    String M_LOG = "";


    // [로직 처리 수행]
    try {

        if (mContext != null && bitmap != null){

            Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

            int width = newBitmap.getWidth();
            int height = newBitmap.getHeight();
            int[] pixels = new int[width * height];

            newBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

            RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

            MultiFormatReader reader = new MultiFormatReader();

            Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(DecodeHintType.POSSIBLE_FORMATS, Arrays.asList( // TODO 포맷 제한
                    BarcodeFormat.QR_CODE,
                    BarcodeFormat.CODE_128
                    //BarcodeFormat.CODE_39,
                    //BarcodeFormat.EAN_13,
                    //BarcodeFormat.EAN_8,
                    //BarcodeFormat.UPC_A,
                    //BarcodeFormat.DATA_MATRIX,
                    //BarcodeFormat.PDF_417
            ));
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(DecodeHintType.TRY_HARDER, true);

            reader.setHints(hints);

            Result result = reader.decode(binaryBitmap);

            Log.d("BARCODE_TYPE", result.getBarcodeFormat().toString());
            Log.d("BARCODE_TEXT", result.getText());

            /*
            BARCODE_TYPE: CODE_128
            BARCODE_TEXT: 123456789012
            */

            returnData = result.getText();
            M_LOG = "[Success] : Qr 및 Barcode 이미지 스캔 성공";
        }
        else {
            M_LOG = "[Error] : Input Context Or Bitmat Is Null";
        }

    } catch (final Exception e){
        e.printStackTrace();
        M_LOG = "[Exception] : " + String.valueOf(e.getMessage());
    }


    // ===============================================================
    S_Log._F_(mContext, "Qr 및 Barcode 이미지 파일 스캔 수행 실시", new String[]{
            "M_LOG :: " + M_LOG,
            "ReturnData :: " + returnData
    });
    // ===============================================================


    // [리턴 변수 반환]
    return returnData;
}

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------

[zxing 라이브러리 사용해 QR 코드 스캔 실시]

https://kkh0977.tistory.com/732

https://blog.naver.com/kkh0977/222347746837?trackingCode=blog_bloghome_searchlist


[참고 사이트] [온라인] QR 코드 생성 사이트

https://blog.naver.com/kkh0977/223112864218?trackingCode=blog_bloghome_searchlist


[라이브러리] [Android] zxing (Java / QR / Barcode)

https://blog.naver.com/kkh0977/222914899604?trackingCode=blog_bloghome_searchlist


[Mobile] 모바일 ( android , ios ) 에서 QR 코드 스캔 후 정보 확인 및 다시 스캔 로직 수행

https://blog.naver.com/kkh0977/223624907452?trackingCode=blog_bloghome_searchlist


[Mobile] 모바일 ( android , ios ) 에서 QR 코드 생성 시 화면 밝기 및 QR 코드 사이즈 최대 표시 Alert 팝업창 활성

https://blog.naver.com/kkh0977/223624876714?trackingCode=blog_bloghome_searchlist

// --------------------------------------------------------------------------------------
 
728x90
반응형
Comments