Cameron Stobie
Cameron Stobie

Reputation: 59

Customising zbar scanner overlay

I am using the library barcodescanner from https://github.com/dm77/barcodescanner.

Currently the scanner displays a rectangle and a laser across the middle.

that looks similar to:

enter image description here

I am looking to make this view into a square since I am only scanning QR codes and to remove the laser and would appreciate some help.

My code that I have written so far for the scanner fragment is below

public class ScannerFragment extends Fragment implements ZBarScannerView.ResultHandler {

private static final String FLASH_STATE = "FLASH_STATE";
private static final String AUTO_FOCUS_STATE = "AUTO_FOCUS_STATE";
private static final String SELECTED_FORMATS = "SELECTED_FORMATS";
private static final String CAMERA_ID = "CAMERA_ID";
private ZBarScannerView mScannerView;
private boolean mFlash;
private boolean mAutoFocus;
private ArrayList<Integer> mSelectedIndices;
private int mCameraId = -1;
Communicator messenger;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    messenger=(Communicator) getActivity();
}

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


    mScannerView = new ZBarScannerView(getActivity());

    if (state != null) {
        mFlash = state.getBoolean(FLASH_STATE, false);
        mAutoFocus = state.getBoolean(AUTO_FOCUS_STATE, true);
        mSelectedIndices = state.getIntegerArrayList(SELECTED_FORMATS);
        mCameraId = state.getInt(CAMERA_ID, -1);
    } else {
        mFlash = false;
        mAutoFocus = true;
        mSelectedIndices = null;
        mCameraId = -1;
    }
    setupFormats();
    return mScannerView;

}

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    setHasOptionsMenu(true);

}


public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem menuItem;
    if (mFlash) {
        menuItem = menu.add(Menu.NONE, R.id.menu_flash, 0, R.string.flash_on);
    } else {
        menuItem = menu.add(Menu.NONE, R.id.menu_flash, 0, R.string.flash_off);
    }
    MenuItemCompat.setShowAsAction(menuItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.menu_flash:
            mFlash = !mFlash;
            if (mFlash) {
                item.setTitle(R.string.flash_on);
            } else {
                item.setTitle(R.string.flash_off);
            }
            mScannerView.setFlash(mFlash);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
    mScannerView.setFlash(mFlash);
    mScannerView.setAutoFocus(mAutoFocus);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(FLASH_STATE, mFlash);
    outState.putBoolean(AUTO_FOCUS_STATE, mAutoFocus);
    outState.putIntegerArrayList(SELECTED_FORMATS, mSelectedIndices);
    outState.putInt(CAMERA_ID, mCameraId);
}

@Override
public void handleResult(Result rawResult) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone(getActivity().getApplicationContext(), notification);
                r.play();
            }
            catch (Exception e) {
            }
        }
    }).start();


    QRResults scanResult = QRResults.getInstance();
    scanResult.Extract(rawResult.getContents());
    if (scanResult.dataExtractedOK)
    {
        if (scanResult.canPerformElectricityComparison() || scanResult.canPerformGasComparison())
        {
            messenger.updateTextView("Found Code");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    Intent i = new Intent(getActivity(), SearchResult.class);
                    startActivity(i);
                }
            }, 2000);
        }
    }
    else
    {
        messenger.updateTextView("Invalid Code");
        // Restart Camera after 2 second delay
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                mScannerView.startCamera();
                mScannerView.setFlash(mFlash);
                mScannerView.setAutoFocus(mAutoFocus);
                messenger.updateTextView("Finding code...");
            }
        }, 2000);

    }
}

public void onFormatsSaved(ArrayList<Integer> selectedIndices) {
    mSelectedIndices = selectedIndices;
    setupFormats();
}


public void setupFormats() {
    List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();
    formats.add(BarcodeFormat.QRCODE);
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();

}

I think I need to extend a class from the library and override some of the methods in it but I'm not sure how to do this and have the overridden methods used in my scanner class.

Thank you for your time.

Upvotes: 0

Views: 3187

Answers (1)

Veaceslav Gaidarji
Veaceslav Gaidarji

Reputation: 4301

  1. Add library module in a project (not as a dependency). Here is a tutorial how to add a library module in Android Studio.
  2. Go to library module and find in ViewFinderView.java class drawLaser(Canvas canvas) method (draws the line in the middle).

Upvotes: 1

Related Questions