Chocolate Chip Cookie
본문 바로가기
App/Android

바코드 라이브러리 사용기 - Zxing - journeyapps 소스 분석 중

by Khookie 2021. 6. 19.

출처 : https://github.com/journeyapps/zxing-android-embedded

 

journeyapps/zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder - journeyapps/zxing-android-embedded

github.com

 

 

메니페스트에 하드웨어 가속 필요

    <application android:hardwareAccelerated="true" ... >

방향 변경

AndroidManifest.xml

 

견본:

<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />

 

 

바코드 뷰 

<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/db_qr"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

 

 

//'zxing_scanner_layout' 옵션을 사용하면 상세 커스텀이 가능

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.journeyapps.barcodescanner.BarcodeView
        android:id="@+id/zxing_barcode_surface"
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:zxing_framing_rect_height="100dp"
        app:zxing_framing_rect_width="250dp" />

    <com.journeyapps.barcodescanner.ViewfinderView
        android:id="@+id/zxing_viewfinder_view"
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"
        app:zxing_result_view="@color/white"
        app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser"
        app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" />

</merge>

 

 

zxing_viewfinder_mask : 바코드를 인식하는 부분이 아닌 카메라뷰 부분의 배경색

zxing_viewfinder_laser : 바코드를 인식하는 부분의 가운데 레이저(?) 색

 

레이아웃 파일을 생성하고 원하는 부분 수정



출처: https://superwony.tistory.com/78 [개발자 키우기]

 

데코레잇바코드뷰에 레이아웃을 추가

 

<com.journeyapps.barcodescanner.DecoratedBarcodeView
    android:id="@+id/db_qr"
    android:layout_width="300dp"
    android:layout_height="300dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:zxing_scanner_layout="@layout/view_qrcode"
    app:layout_constraintTop_toTopOf="parent" />

 

 

바코드 인식하기

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);  //회전 가능하게 할지 여부

intentIntegrator.setBeepEnabled(true); //바코드 인식시 소리날지
integrator.initiateScan();  //스캔시작

 

intentIntegrator.setCaptureActivity(QReaderActivity.class); //커스텀 뷰에서는 스캔함수 호출전 이 라인도 필요한듯함


 

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity

스캔 완료후 데이터 추출
// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);


    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

 

프래그먼트에서 의 인텐트

IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment

 

// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.

 

 

인텐트 커스텀 옵션

Customize options:

IntentIntegrator integrator = new IntentIntegrator(this); integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES); integrator.setPrompt("Scan a barcode"); integrator.setCameraId(0); // Use a specific camera of the device integrator.setBeepEnabled(false); integrator.setBarcodeImageEnabled(true); integrator.initiateScan();

 

 

바코드 샘플 만들기

//While this is not the primary purpose of this library, it does include basic support for generating some barcode types:

 

 

try {
  BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
  Bitmap bitmap = barcodeEncoder.encodeBitmap("content", BarcodeFormat.QR_CODE, 400, 400);
  ImageView imageViewQrCode = (ImageView) findViewById(R.id.qrCode);
  imageViewQrCode.setImageBitmap(bitmap);
} catch(Exception e) {

}

//No customization of the image is currently supported, including changing colors or padding. If you
require more customization, copy and modify the source for the encoder.

 

 


스캔 하는 화면 리소스

 

private DecoratedBarcodeView  barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);

데코레이티드 바코드 뷰 객체이다

 

메소드로는 

DecorateBarcodeView의 .setTorchOff : 라이트 끄기 , .setTorchOn : 라이트 켜기 

 

 

 

 

상태 처리

 

public interface TorchListener { //카메라 라이트 불빛 관련 이벤트 처리

void onTorchOn();

void onTorchOff();
}

 

 

public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
   case KeyEvent.KEYCODE_FOCUS:
   case KeyEvent.KEYCODE_CAMERA:
// Handle these events so they don't launch the Camera app
         return true;
// Use volume up/down to turn on light
   case KeyEvent.KEYCODE_VOLUME_DOWN:  //볼륨 버튼 다운 시
          setTorchOff();
         return true;
    case KeyEvent.KEYCODE_VOLUME_UP:   //볼륨 버튼 업 시
           setTorchOn();
           return true;
  }
return super.onKeyDown(keyCode, event);
}

 

 

 

 

 

 

참고 사이트 [안드로이드/Android] QR코드 리더기 만들기 (tistory.com)

 

[안드로이드/Android] QR코드 리더기 만들기

ZXing 라이브러리를 사용하면 손쉽게 QRCode Reader를 구현할 수 있습니다. 이번 포스팅은 기본적인 라이브러리 사용 방법과 뷰 커스텀을 중점적으로 다루겠습니다. 설정 프로젝트의 타겟 버전에 따

superwony.tistory.com

 

'App > Android' 카테고리의 다른 글

[공유]안드로이드 이벤트 처리방식  (0) 2021.06.28
이벤트 처리 관련 코드  (0) 2021.06.23
인텐트 전송 관련 공유  (0) 2021.06.11
[공유]리스트뷰 어뎁터 관련  (0) 2021.06.09
안드로이드 스튜디오 에러 :  (0) 2021.06.08

댓글