二維碼Zxing&Zbar


二維碼Zxing&Zbar

 

前言該項目主要介紹了二維碼掃描、閃光燈開啟、本地二維碼圖片識別、二維碼生成。分別是zxing和zbar(網格二維碼)分別實現,具體效果運行項目apk...

 

開發環境:AndroidStudio2.2.1+gradle-2.14.1

 

涉及知識:
    1.Zxing和Zbar(網格)二維碼掃描
    2.閃光燈開啟與關閉
    3.本地二維碼識別
    4.二維碼生成
    5.Handler機制
    6.butterknife注解式開發

 

引入依賴:

 

    compile 'com.android.support:appcompat-v7:22.+'
    compile 'com.google.zxing:core:3.2.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile files('libs/zbar.jar')

 

部分代碼:

/**
 * Zbar二維碼掃描+閃光燈+本地二維碼識別
 */
public class ZbarActivity extends AppCompatActivity implements QRCodeView.Delegate {

    @Bind(R.id.zbarview)
    ZBarView mQRCodeView;

    @Bind(R.id.scancode_lamplight)
    ToggleButton toggleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zbartest_scan_layout);
        ButterKnife.bind(this);
        initLayout();
    }

    private void initLayout() {
        mQRCodeView.setDelegate(this);
        mQRCodeView.startSpotAndShowRect();//顯示掃描框,並且延遲1.5秒后開始識別
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mQRCodeView.openFlashlight();
                } else {
                    mQRCodeView.closeFlashlight();
                }
            }
        });
    }

    @OnClick({R.id.line_back, R.id.scancode_localimg})
    protected void onClickBtn(View view) {
        switch (view.getId()) {
            case R.id.line_back:
                finish();
                break;
            case R.id.scancode_localimg:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 0x11);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 0x11) {
            Uri uri = data.getData();
            String path = null;
            if (!TextUtils.isEmpty(uri.getAuthority())) {
                Cursor cursor = getContentResolver().query(uri,
                        new String[]{MediaStore.Images.Media.DATA}, null, null, null);
                if (null == cursor) {
                    Toast.makeText(ZbarActivity.this, "圖片沒找到", Toast.LENGTH_SHORT).show();
                    return;
                }
                cursor.moveToFirst();
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                cursor.close();
            } else {
                path = uri.getPath();
            }
            if (null != path) {
                codeDiscriminate(path);
            } else {
                Toast.makeText(ZbarActivity.this, "圖片路徑為空", Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    @Override
    protected void onRestart() {
        mQRCodeView.startCamera();
        super.onRestart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mQRCodeView.startSpotAndShowRect();//顯示掃描框,並且延遲1.5秒后開始識別
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
//        mQRCodeView.closeFlashlight();
        super.onStop();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    private void codeDiscriminate(final String path) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Looper.prepare();
                String result = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    result = QRCodeDecoder.syncDecodeQRCode(path);
                } else {
                    result = QRCodeDecoder.syncDecodeQRCode2(path);
                }
                Log.i("zbar_result", Build.VERSION.SDK_INT + "--->" + result);
                Message msg = mHandler.obtainMessage();
                //封裝消息id
                msg.what = 1;//作為標示,便於接收消息
                msg.obj = result;
                mHandler.sendMessage(msg);//發送消息
            }
        }).start();
    }


    //創建一個Hander局部類對象,通過handleMessage()鈎子方法來更新UI控件
    Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            //得到封裝消息的id進行匹配
            if (1 == msg.what) {
                if (null != msg.obj)
                    onScanQRCodeSuccess(msg.obj.toString());
            }
        }

    };


    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        ButterKnife.unbind(this);
        super.onDestroy();

    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i("zbar_result", "result:" + result);
        Toast.makeText(this, "二維碼的數據:" + result, Toast.LENGTH_SHORT).show();
        vibrate();
        mQRCodeView.startSpot();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e("zbar_result", "打開相機出錯");
        Toast.makeText(this, "打開相機出錯", Toast.LENGTH_SHORT).show();
    }

}

 

 

源碼下載...

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM