zxing 을 이용하여 QR코드를 스캔하는 방법에 대한 코드 작성에 필요한 핵심 내용을 기술한다.
앱 build.gradle
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0' implementation 'gun0912.ted:tedpermission:2.0.0' implementation 'com.google.android.material:material:1.1.0' }
|
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.link2me.android.qrcodesample">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" tools:ignore="ProtectedPermissions" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:allowBackup="false" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <activity android:name=".MainActivity" android:screenOrientation="portrait"/> <activity android:name="com.journeyapps.barcodescanner.CaptureActivity" android:screenOrientation="portrait" tools:replace="screenOrientation" /> <activity android:name=".SplahActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ScanQRActivity" android:screenOrientation="portrait"/> </application>
</manifest>
|
ScanQRActivity.java
public class ScanQRActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); private IntentIntegrator qrScan; Context mContext; String QRcode; TextView textView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scanqr); mContext = ScanQRActivity.this;
qrScan = new IntentIntegrator(this); qrScan.setOrientationLocked(false); qrScan.setPrompt("QRcode Sample!"); qrScan.initiateScan();
textView = findViewById(R.id.textView); }
@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(); Intent intent = new Intent(ScanQRActivity.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } else { if(result.getContents() != null) { QRcode = result.getContents(); PrefsHelper.write("QRCode",result.getContents()); // QRcode 결과를 가지고 서버에서 정보를 조회하여 가져오거나, 다른 처리를 하면 된다. // QRcode 읽은 결과를 화면에 표시 textView.setText(QRcode); } } } else { super.onActivityResult(requestCode, resultCode, data); } }
}
|
resource 등 코드 세부적인 사항은 https://github.com/jsk005/JavaProjects/tree/master/qrcodesample 를 참조하시길.