- ZXing 공식 페이지
https://github.com/zxing/zxing
1. 기본코드
- 어플리케이션 build.gradle
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
- AndroidManifest.xml
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="android:screenOrientation" />
> tools:replace="android:screenOrientation"을 추가하지 않으면 android:screenOrientation="fullSensor" 에러가 발생한다.
- Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IntentIntegrator(MainActivity.this).initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
String text = result.getContents();
if (TextUtils.isEmpty(text) == false) {
TextView textView = findViewById(R.id.text);
textView.setText(text);
}
Log.d(TAG, "QR : " + result.getContents());
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
> initiateScan(); 메소드를 호출하면 CaptureActivity가 호출되고 카메라 사용 퍼미션에 대한 안내가 표시된다.
2. 샘플코드
소스코드
APK
- QR