我找AndroidZbar的代码找了好久,兄弟们有福了,这是github上的一个基于Zbar的开源库BGAQRCode(这个库封装的很棒),简书不能添加文件(反正我没找到),所以代码就不提供了,自己去github下
代码结构:
你可以先运行,看下得到的app,反正我没用过zbar,但是看完demoApp就会改代码了,你把它镶嵌入自己的app最多2小时
主要用到的东西:
MainActivity.java
其实就是跳转界面
TestScanActivity
public class TestScanActivity extends AppCompatActivity implements QRCodeView.Delegate {
private static final String TAG = TestScanActivity.class.getSimpleName();
private QRCodeView mQRCodeView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_scan);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
mQRCodeView.setDelegate(this);
}
@Override
protected void onStart() {
super.onStart();
mQRCodeView.startCamera();
}
@Override
protected void onStop() {
mQRCodeView.stopCamera();
super.onStop();
}
@Override
protected void onDestroy() {
mQRCodeView.onDestroy();
super.onDestroy();
}
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(200);
}
//下面这个方法就是返回扫描结果的
@Override
public void onScanQRCodeSuccess(String result) {
Log.i(TAG, "result:" + result);
Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
vibrate();
mQRCodeView.startSpot();
}
@Override
public void onScanQRCodeOpenCameraError() {
Log.e(TAG, "打开相机出错");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_spot:
mQRCodeView.startSpot();
break;
case R.id.stop_spot:
mQRCodeView.stopSpot();
break;
case R.id.start_spot_showrect:
mQRCodeView.startSpotAndShowRect();
break;
case R.id.stop_spot_hiddenrect:
mQRCodeView.stopSpotAndHiddenRect();
break;
case R.id.show_rect:
mQRCodeView.showScanRect();
break;
case R.id.hidden_rect:
mQRCodeView.hiddenScanRect();
break;
case R.id.start_preview:
mQRCodeView.startCamera();
break;
case R.id.stop_preview:
mQRCodeView.stopCamera();
break;
case R.id.open_flashlight:
mQRCodeView.openFlashlight();
break;
case R.id.close_flashlight:
mQRCodeView.closeFlashlight();
break;
case R.id.scan_barcode:
mQRCodeView.changeToScanBarcodeStyle();
break;
case R.id.scan_qrcode:
mQRCodeView.changeToScanQRCodeStyle();
break;
}
} ```
###R.layout.activity_test_scan
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/MatchMatch">
<cn.bingoogolapple.qrcode.zbar.ZBarView
android:id="@+id/zbarview"
style="@style/MatchMatch"
app:qrcv_animTime="1000"
app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
app:qrcv_barcodeRectHeight="140dp"
app:qrcv_borderColor="@android:color/white"
app:qrcv_borderSize="1dp"
app:qrcv_cornerColor="@color/colorPrimaryDark"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerSize="3dp"
app:qrcv_customGridScanLineDrawable="@mipmap/custom_grid_scan_line"
app:qrcv_isBarcode="false"
app:qrcv_isOnlyDecodeScanBoxArea="false"
app:qrcv_isShowDefaultGridScanLineDrawable="true"
app:qrcv_isShowDefaultScanLineDrawable="true"
app:qrcv_isShowTipBackground="true"
app:qrcv_isShowTipTextAsSingleLine="false"
app:qrcv_isTipTextBelowRect="false"
app:qrcv_maskColor="#33FFFFFF"
app:qrcv_qrCodeTipText="将二维码/条码放入框内,即可自动扫描"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="@color/colorPrimaryDark"
app:qrcv_toolbarHeight="56dp"
app:qrcv_topOffset="90dp" />
<include layout="@layout/toolbar"/>
<include layout="@layout/view_control"/>
</RelativeLayout>```