android QRCODE reader

Asked

Viewed 2,198 times

3

Guys, I need to put a qrcode reader in my android app, can someone give me a hint? I don’t want it to open another app, I want it to be within the app itself

  • 1

    Use the ZXING library: https://github.com/journeyapps/zxing-android-embedded

  • I’m using the library, I’m going to put the code. So far everything is fine, I’m not able to make that when the reader opens the qrcode.. he only gives a "Toast"

  • It has this method also very practical... https://www.youtube.com/watch?v=125WPZHxU7E

1 answer

3


For this, we will use the library ZXING

In the build.Gradle(app) add libraries:

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

In the archive Androidmanifest.xml Let’s 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>

For use of the camera, it is necessary to check if there is 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 the 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 method onActivityResult:

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
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.