扫码枪扫码的问题

今天来总结下,扫码枪遇到的一些问题。(ps:参考了http://blog.csdn.net/czhpxl007/article/details/50363766 博主的文章)

刚开始扫码用的是EditText

EditText editText = (EditText) findViewById(R.id.edittext);
editText.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        //在这个实现扫码
        //固定s的长度,局限了扫码,而且还有一个问题就是扫的太快,条形码乱码了~之后百度了很久,好不容易找到一篇参考的

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};

因为我不需要用到蓝牙 ,就参考博主的修改了~~
/*扫码抢解析类/

public class ScanGunKeyEventHelper {

    private final static long MESSAGE_DELAY = 50; // 延迟50ms,判断扫码是否完成。
    private StringBuffer mStringBufferResult; // 扫码内容
    private boolean mCaps; // 大小写区分
    private final Handler mHandler;
    private final Runnable mScanningFishedRunnable;
    private OnScanSuccessListener mOnScanSuccessListener;

    public ScanGunKeyEventHelper(OnScanSuccessListener onScanSuccessListener) {
        mOnScanSuccessListener = onScanSuccessListener;
        mStringBufferResult = new StringBuffer();
        mHandler = new Handler();

        mScanningFishedRunnable = new Runnable() {
            @Override
            public void run() {
                performScanSuccess();
            }
        };
    }

    /**
     * 返回扫码成功后的结果
     */
    private void performScanSuccess() {
        String barcode = mStringBufferResult.toString();
        if (mOnScanSuccessListener != null) {
            mOnScanSuccessListener.onScanSuccess(barcode);
        }
        mStringBufferResult.setLength(0);
    }

    /**
     * 扫码枪事件解析
     *
     * @param event
     */
    public void analysisKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        // 字母大小写判断
        checkLetterStatus(event);

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            char aChar = getInputCode(event);
            if (aChar != 0) {
                mStringBufferResult.append(aChar);
            }
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                // 若为回车键,直接返回
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.post(mScanningFishedRunnable);
            } else {
                // 延迟post,若50ms内,有其他事件
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.postDelayed(mScanningFishedRunnable, MESSAGE_DELAY);
            }
        }
    }

    // 检查shift键
    private void checkLetterStatus(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
            // 按着shift键,表示大写
            // 松开shift键,表示小写
            mCaps = (event.getAction() == KeyEvent.ACTION_DOWN);
        }
    }

    // 获取扫描内容
    private char getInputCode(KeyEvent event) {
        int keyCode = event.getKeyCode();
        char aChar;
        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            // 字母
            aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            // 数字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        } else {
            // 其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = '.';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = mCaps ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = '/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = mCaps ? '|' : '\\';
                    break;
                default:
                    aChar = 0;
                    break;
            }
        }
        return aChar;
    }

    public interface OnScanSuccessListener {
        void onScanSuccess(String barcode);
    }

    public void onDestroy() {
        mHandler.removeCallbacks(mScanningFishedRunnable);
        mOnScanSuccessListener = null;
    }
}

在activity

public class MainActivity extends Activity implements ScanGunKeyEventHelper.OnScanSuccessListener {

 private ScanGunKeyEventHelper scanGunKeyEventHelper = null;//扫码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        scanGunKeyEventHelper = new ScanGunKeyEventHelper(this);
    }

     @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        scanGunKeyEventHelper.analysisKeyEvent(event);
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scanGunKeyEventHelper.onDestroy();
    }

     @Override
    public void onScanSuccess(String barcode) {
        //barcode 条形码
        //在这里实现相关操作
    }
}

你可能感兴趣的:(android)