Integrating Barcode Reader on Android

Asked

Viewed 4,008 times

1

I’m trying to add barcode reading functionality to my app through com.google.zxing.client.android.SCAN.

I added the calls to the application via Intent according to the code below:

static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.codigodebarras, container, false);
    }

    @Override
    public void onStart() {
        String aux;
        super.onStart();

        UpdateView();

    }

    private void UpdateView() {

        scanQR = (Button) getActivity().findViewById(R.id.scanQR);
        scanQR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                try {
                    Intent intent = new Intent(ACTION_SCAN);
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);
                } catch (ActivityNotFoundException anfe) {
                    CustomDialogSimNao cdsn = new CustomDialogSimNao(getActivity(), "Nenhum scanner foi encontrado. Deseja fazer o download?");
                    cdsn.show();
                }
            }
        });

        scanCode = (Button) getActivity().findViewById(R.id.scanCodBar);
        scanCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                try {
                    Intent intent = new Intent(ACTION_SCAN);
                    intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
                    startActivityForResult(intent, 0);
                } catch (ActivityNotFoundException anfe) {
                    CustomDialogSimNao cdsn = new CustomDialogSimNao(getActivity(), "Nenhum scanner foi encontrado. Deseja fazer o download?");
                    cdsn.show();
                }

            }
        });

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == getActivity().RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Toast toast = Toast.makeText(getActivity(), "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }

The application is called normally and the result goes back to my application as it should be, but there are two problems and I would like if possible help me in these issues.

  1. When running the application it usually recognizes the barcodes very quickly, but when it is called via Intent by my application it has a lot of difficulty to read the same barcodes.

  2. I cannot locate the number in the results returned to my application.

How to make the call correctly so it can recognize the codes quickly? Where I find the barcode code in the returned information?

Grateful.

1 answer

1


I’ve solved the issues. I’ve removed the lines:

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

and

intent.putExtra("SCAN_MODE", "PRODUCT_MODE");

Adding these lines restricts the type of code the reader can read and causes the delay and confuses it, as it returns a wrong code when the read is executed.

Without the lines above works correctly and quickly. And I found that we can add the following line:

intent.putExtra("SCAN_MODE", "SCAN_MODE");

I added and works also correctly.

Browser other questions tagged

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