Exchange

QR Code Scan in Android Studio

in  build.gradle(:app)


// QR Scan
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'


in MainActivity.Java

Create button and set on click listener, Call a method scanCode();

btnConfirmReceived.setOnClickListener(view -> {
scanCode();
});

private void scanCode() {

ScanOptions options = new ScanOptions();
options.setPrompt("Volume up to flash on");
options.setBeepEnabled(true);
options.setOrientationLocked(true);
options.setCaptureActivity(CaptureAct.class);
barLauncher.launch(options);

}

ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result -> {

if(result.getContents() != null){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}

});

create class CaptureAct.java


public class CaptureAct extends CaptureActivity {
}

in AndroidManifest.xml


<uses-permission android:name="android.permission.CAMERA"/>

<activity android:name=".CaptureAct"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"/>




Post a Comment

0 Comments