question

clover-faq avatar image
clover-faq asked morenoh149 commented

How do I programmatically use the camera barcode scanner on the Mobile?

I have an app that utilizes the Barcode Scanner camera on Mini and Station. How do I get the same functionality on a Mobile device?
Clover Mobilebarcode scanner
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

clover-faq avatar image
clover-faq answered morenoh149 commented
Given the below code sample, we're able to utilize the scanner on all Clover devices. The difference between the Mini/Station and the Mobile is that Mini/Station uses the camera and the Mobile has a separate built in barcode scanner. With this code, a button within your app can be pushed so the preview window for scanning will pop up on the Mini and Station or a red light will appear out of the side of the Mobile (both indicating that you may begin scanning items).

There is not a direct way to use the camera on a Mobile for scanning, but it may be possible through the use of third party libraries.

Code Sample:
import android.app.Activity;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;
import android.view.View;

import com.clover.sdk.v1.Intents;
import com.clover.sdk.v3.scanner.BarcodeScanner;
import com.clover.sdk.v3.scanner.BarcodeResult;

public class MainActivity extends Activity {

    private Button mBarcodeOnButton;
    private Button mBarcodeOffButton;
    private BarcodeScanner mBarcodeScanner;
    private TextView barcode_result;

    private BroadcastReceiver barcodeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BarcodeResult barcodeResult = new BarcodeResult(intent);

            if (barcodeResult.isBarcodeAction()) {
                String barcode = barcodeResult.getBarcode();
                barcode_result.setText("Result: " + barcode);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBarcodeScanner = new BarcodeScanner(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerBarcodeScanner();
        barcode_result = (TextView) findViewById(R.id.barcode_result);

        mBarcodeOnButton = (Button) findViewById(R.id.barcode_scan_on_button);
        mBarcodeOnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle extras = new Bundle();
                extras.putBoolean(Intents.EXTRA_SCAN_QR_CODE, true); # Checks for QR codes
                extras.putBoolean(Intents.EXTRA_SCAN_1D_CODE, true); # Checks for a standard barcode
                mBarcodeScanner.executeStartScan(extras);
            }
        });

        mBarcodeOffButton = (Button) findViewById(R.id.barcode_scan_off_button);
        mBarcodeOffButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBarcodeScanner.executeStopScan(null);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterBarcodeScanner();
    }

    private void registerBarcodeScanner() {
        registerReceiver(barcodeReceiver, new IntentFilter(BarcodeResult.INTENT_ACTION));
    }

    private void unregisterBarcodeScanner() {
        unregisterReceiver(barcodeReceiver);
    }
}
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

morenoh149 avatar image morenoh149 commented ·

This code seems to depend on some accompanying layout file. I get

error: cannot find symbol variable activity_main

and other errors when accessing `R.layout` or `R.id`. Could you share some accompanying layout file?

1 Like 1 ·
morenoh149 avatar image morenoh149 morenoh149 commented ·

Seems like the example has changed over time. The current version is at https://github.com/clover/android-examples/blob/master/barcodereceiver/app/src/main/java/com/clover/example/barcodereceiver/app/MainActivity.java and has the accompanying layout resources.

1 Like 1 ·

Welcome to the
Clover Developer Community