본문 바로가기

개발/Android

Android 바코드 스캐너 만들기

반응형

1. 프로젝트 생성

compileSdkVersion 27

minSdkVersion 16

targetSdkVersion 16


*해당 SDK 버전은 다르게 설정해도 무방함.


2. 라이브러리 Import

build.gradle 파일에 해당 compile 코드 한줄 추가

compile 'com.journeyapps:zxing-android-embedded:3.5.0'


3. Layout 코드

<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/dbvBarcode"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_centerHorizontal="true"></com.journeyapps.barcodescanner.DecoratedBarcodeView>

4. Activity 코드

private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.dbvBarcode);

capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(this.getIntent(), savedInstanceState);
capture.decode();
barcodeScannerView.decodeContinuous(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
readBarcode(result.toString());
}

@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
});
}

@Override
protected void onResume() {
super.onResume();
capture.onResume();
}

@Override
protected void onPause() {
super.onPause();
capture.onPause();
}

@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}

public void readBarcode(String barcode) {
Toast.makeText(getApplicationContext(), barcode, Toast.LENGTH_LONG).show();
}


5. 실행 화면




반응형