Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Google code scanner throwing "MlKitException: Failed to scan code"

I have followed the tutorial here and got it work just fine. Then later, using the same code, I am getting this exception every time I try to open the QR code scanner:

com.google.mlkit.common.MlKitException: Failed to scan code.

I don't even leave my app, the exception is instantaneous.

It really bothers me on how this was working before and now it just doesn't work anymore.

like image 568
Michel Feinstein Avatar asked Feb 28 '26 23:02

Michel Feinstein


1 Answers

I post my solution to these problem. This solve problem to barcore scann error:

Found in this guide: https://developers.google.com/android/guides/module-install-apis?hl=es-419

public void onScanButtonClicked() {

    GmsBarcodeScannerOptions.Builder optionsBuilder = new GmsBarcodeScannerOptions.Builder();
    if (allowManualInput) {
        optionsBuilder.allowManualInput();
    }
    if (enableAutoZoom) {
        optionsBuilder.enableAutoZoom();
    }

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(getContext());

    OptionalModuleApi optionalModuleApi = GmsBarcodeScanning.getClient(getContext());
    moduleInstallClient
            .areModulesAvailable(optionalModuleApi)
            .addOnSuccessListener(
                    response -> {
                        if (response.areModulesAvailable()) {
                            // Modules are present on the device...
                            barcodeResultView.setText("Modules are present on the device");
                            GmsBarcodeScanner gmsBarcodeScanner = GmsBarcodeScanning.getClient(getContext(), optionsBuilder.build());
                            gmsBarcodeScanner
                                    .startScan()
                                    .addOnSuccessListener(barcode -> barcodeResultView.setText(getSuccessfulMessage(barcode)))
                                    .addOnFailureListener(
                                            e -> barcodeResultView.setText(getErrorMessage(e)))
                                    .addOnCanceledListener(
                                            () -> barcodeResultView.setText(getString(R.string.error_scanner_cancelled)));
                        } else {
                            // Modules are not present on the device...
                            barcodeResultView.setText("Modules are not present on the device");
                            moduleInstall();
                        }
                    })
            .addOnFailureListener(
                    e -> {
                        // Handle failure…
                        barcodeResultView.setText("Handle failure…");
                    });
}

final class ModuleInstallProgressListener implements InstallStatusListener {
    @Override
    public void onInstallStatusUpdated(ModuleInstallStatusUpdate update) {
        ModuleInstallStatusUpdate.ProgressInfo progressInfo = update.getProgressInfo();
        // Progress info is only set when modules are in the progress of downloading.
        if (progressInfo != null) {
            int progress =
                    (int) (progressInfo.getBytesDownloaded() * 100 / progressInfo.getTotalBytesToDownload());
            // Set the progress for the progress bar.
            progressBar.setProgress(progress);
        }
        // Handle failure status maybe…

        // Unregister listener when there are no more install status updates.
        if (isTerminateState(update.getInstallState())) {

            moduleInstallClient.unregisterListener(this);
        }
    }

    public boolean isTerminateState(@ModuleInstallStatusUpdate.InstallState int state) {
        return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED;
    }
}

private void moduleInstall(){
    InstallStatusListener listener = new ModuleInstallProgressListener();

    OptionalModuleApi optionalModuleApi = GmsBarcodeScanning.getClient(getContext());
    ModuleInstallRequest moduleInstallRequest =
            ModuleInstallRequest.newBuilder()
                    .addApi(optionalModuleApi)
                    // Add more API if you would like to request multiple optional modules
                    //.addApi(...)
                    // Set the listener if you need to monitor the download progress
                    .setListener(listener)
                    .build();

    moduleInstallClient.installModules(moduleInstallRequest)
            .addOnSuccessListener(
                    response -> {
                        if (response.areModulesAlreadyInstalled()) {
                            // Modules are already installed when the request is sent.
                            barcodeResultView.setText("Modules are already installed when the request is sent.");
                        }
                    })
            .addOnFailureListener(
                    e -> {
                        // Handle failure...
                        barcodeResultView.setText(getErrorMessage(e));
                    });

}
like image 159
José González Avatar answered Mar 02 '26 14:03

José González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!