二维码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(); } }