自定义zbar扫描仪覆盖

时间:2015-05-28 12:52:15

标签: android zbar

我正在使用https://github.com/dm77/barcodescanner中的库条形码扫描器。

目前扫描仪在中间显示一个矩形和一个激光。

看起来类似于:

enter image description here

我希望将此视图放入正方形,因为我只扫描QR码并移除激光,并希望得到一些帮助。

到目前为止,我为扫描器片段编写的代码位于

之下

public class ScannerFragment扩展了Fragment工具         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();

}

我想我需要从库中扩展一个类并重写其中的一些方法,但我不知道如何做到这一点并且在我的扫描器类中使用了重写的方法。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

  1. 在项目中添加库模块(不作为依赖项)。 Here是一个如何在Android Studio中添加库模块的教程。
  2. 转到图书馆模块并找到ViewFinderView.javadrawLaser(Canvas canvas)方法(在中间画线)。