QRCODE reader android

2

Well, I need to put a qrcode reader in my android app, can anyone give me a hint? I do not want it to open another app, I want it to be inside the app itself

    
asked by anonymous 18.03.2018 / 02:17

1 answer

1

For this, we will use the library ZXING

In build.gradle (app) we add the libraries:

implementation 'com.google.zxing:core:2.2'
implementation 'com.embarkmobile:zxing-android-minimal:1.2.1@aar'

In the AndroidManifest.xml file we will add the QRCode Capture Activity:

    <activity
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.zxing.client.android.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

To use the camera, you must check for permission:

   private void checkPermission() {
        // Verifica necessidade de verificacao de permissao
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                Toast.makeText(this, "Não há permissão para utilizar a camera!", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        CODE_PERMISSION_CAMERA);
            } else {
                // Solicita permissao
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        CODE_PERMISSION_CAMERA);
            }
        }
    }

Invoke code reader:

 private void openCamera(){
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
       // QR_CODE_MODE: QRCODE , ONE_D_MODE: Codigo de barras
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }

The result returns through the onActivityResult method:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == Activity.RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            Toast.makeText(getApplicationContext(), contents, Toast.LENGTH_LONG).show();
            Log.i("CONTENT SCAN ", contents);

        } else if (resultCode == Activity.RESULT_CANCELED) {
            // Handle cancel
        }
    }
}
    
19.03.2018 / 19:20