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:
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.content.BroadcastReceiver;
  4. import android.widget.Button;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.widget.TextView;
  9. import android.view.View;
  10.  
  11. import com.clover.sdk.v1.Intents;
  12. import com.clover.sdk.v3.scanner.BarcodeScanner;
  13. import com.clover.sdk.v3.scanner.BarcodeResult;
  14.  
  15. public class MainActivity extends Activity {
  16.  
  17. private Button mBarcodeOnButton;
  18. private Button mBarcodeOffButton;
  19. private BarcodeScanner mBarcodeScanner;
  20. private TextView barcode_result;
  21.  
  22. private BroadcastReceiver barcodeReceiver = new BroadcastReceiver() {
  23. @Override
  24. public void onReceive(Context context, Intent intent) {
  25. BarcodeResult barcodeResult = new BarcodeResult(intent);
  26.  
  27. if (barcodeResult.isBarcodeAction()) {
  28. String barcode = barcodeResult.getBarcode();
  29. barcode_result.setText("Result: " + barcode);
  30. }
  31. }
  32. };
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. mBarcodeScanner = new BarcodeScanner(this);
  39. }
  40.  
  41. @Override
  42. protected void onResume() {
  43. super.onResume();
  44. registerBarcodeScanner();
  45. barcode_result = (TextView) findViewById(R.id.barcode_result);
  46.  
  47. mBarcodeOnButton = (Button) findViewById(R.id.barcode_scan_on_button);
  48. mBarcodeOnButton.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. Bundle extras = new Bundle();
  52. extras.putBoolean(Intents.EXTRA_SCAN_QR_CODE, true); # Checks for QR codes
  53. extras.putBoolean(Intents.EXTRA_SCAN_1D_CODE, true); # Checks for a standard barcode
  54. mBarcodeScanner.executeStartScan(extras);
  55. }
  56. });
  57.  
  58. mBarcodeOffButton = (Button) findViewById(R.id.barcode_scan_off_button);
  59. mBarcodeOffButton.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. mBarcodeScanner.executeStopScan(null);
  63. }
  64. });
  65. }
  66.  
  67. @Override
  68. protected void onPause() {
  69. super.onPause();
  70. unregisterBarcodeScanner();
  71. }
  72.  
  73. private void registerBarcodeScanner() {
  74. registerReceiver(barcodeReceiver, new IntentFilter(BarcodeResult.INTENT_ACTION));
  75. }
  76.  
  77. private void unregisterBarcodeScanner() {
  78. unregisterReceiver(barcodeReceiver);
  79. }
  80. }
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