一、序言
二维码扫描是移动开发一定会遇到的功能需求,这里采用的是谷歌开源二维码扫描框架;并根据自己的需求做了一些修改,以满足项目的需求,各位有需求的小伙伴也可以根据自己的需求来参考下
二、准备工作
1.根据自己开发项目添加相应的平台的框架:https://github.com/zxing/zxing(这里以Android移动开发为例)
2.在android 中添加项目依赖
implementation 'com.google.zxing:core:3.3.1'
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
三、添加自定义代码
在initView()部分主要是自定义扫码框的大小和标语提示,在onCallBack方法中则添加对于扫码结果的回调避免在onActivityResul回调
- 如果在onActivityResult回调:
if (resultCode == Activity.RESULT_OK && data != null) {
try {
// 救助体系扫码签到二维码解析
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (intentResult != null && !TextUtils.isEmpty(intentResult.getContents())) {
// 解析扫码字符串
if (intentResult.getContents().contains("tysos") && intentResult.getContents().contains("ns=")) {
Intent intent = null;
// 扫码签到
intent = new Intent(this, ScanMoreActivity.class);
intent.putExtra(Final.SCAN_CODE, intentResult.getContents());
startActivity(intent);
} else {
ToastUtil.showMessage("不是设备二维码,请重新扫码!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 不再onActivityresult回调
@Override
public void onCallBack(Intent data) {
// 救助体系扫码签到二维码解析
IntentResult intentResult = IntentIntegrator.parseActivityResult(REQUEST_CODE, Activity.RESULT_OK, data);
if (intentResult != null && !TextUtils.isEmpty(intentResult.getContents())) {
// 解析扫码字符串
if (intentResult.getContents().contains("tysos") && intentResult.getContents().contains("ns=")) {
Intent intent = new Intent(this, EquipCheckInfoActivity.class);
intent.putExtra(Final.SCAN_CODE, intentResult.getContents());
this.startActivityForResult(intent, Final.REQUEST_EQUIP_CHICK_INFO_ACTIVITY);
this.setResult(RESULT_OK);
} else {
ToastUtil.showMessage("不是设备二维码,请重新扫码!");
}
}
}
package com.gzjiequan.rescuewithyou.navigation.checkequip;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.gzjiequan.rescuewithyou.R;
import com.gzjiequan.rescuewithyou.base.BaseActivity;
import com.gzjiequan.rescuewithyou.base.Final;
import com.gzjiequan.rescuewithyou.customwidget.CustomCaptureManager;
import com.gzjiequan.rescuewithyou.util.DensityUtils;
import com.gzjiequan.rescuewithyou.util.ToastUtil;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.journeyapps.barcodescanner.Size;
import butterknife.BindView;
import butterknife.OnClick;
import static com.google.zxing.integration.android.IntentIntegrator.REQUEST_CODE;
public class EquipCheckActivity extends BaseActivity implements CustomCaptureManager.OnScanResultCallBack {
@BindView(R.id.btnBack)
TextView btnBack;
@BindView(R.id.tvTitle)
TextView tvTitle;
@BindView(R.id.tvOther)
TextView tvOther;
@BindView(R.id.ivEquipCheckBorder)
ImageView ivEquipCheckBorder;
@BindView(R.id.dbvEquipCheck)
DecoratedBarcodeView dbvEquipCheck;
@BindView(R.id.ivEquipCheckPress)
ImageView ivEquipCheckPress;
@BindView(R.id.ivEquipCheckNor)
ImageView ivEquipCheckNor;
@BindView(R.id.cbEquipCheckLight)
RelativeLayout cbEquipCheckLight;
@Override
protected int getContentViewId() {
return R.layout.activity_equip_check;
}
private CustomCaptureManager captureManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
captureManager.onResume();
}
@Override
protected void onPause() {
super.onPause();
captureManager.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
captureManager.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return dbvEquipCheck.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
@Override
public void initView(Bundle savedInstanceState) {
this.mNeedPermissions = new String[]{
Manifest.permission.CAMERA
};
this.checkPermissions(mNeedPermissions);
tvTitle.setText(getString(R.string.equip_check_title));
this.dbvEquipCheck.setStatusText("");
this.dbvEquipCheck.getStatusView().setGravity(Gravity.CENTER);
this.dbvEquipCheck.getViewFinder();
this.dbvEquipCheck.getBarcodeView().setFramingRectSize(new Size(DensityUtils.dp2px(this, 200.0F), DensityUtils.dp2px(this, 200.0F)));
ViewGroup.LayoutParams localLayoutParams = ivEquipCheckBorder.getLayoutParams();
localLayoutParams.height = DensityUtils.dp2px(this, 200.0F);
localLayoutParams.width = DensityUtils.dp2px(this, 200.0F);
ivEquipCheckBorder.setLayoutParams(localLayoutParams);
// 初始化 扫描
dbvEquipCheck.getStatusView().setText("");
captureManager = new CustomCaptureManager(this, dbvEquipCheck);
captureManager.setOnScanResultCallBack(this);
captureManager.initializeFromIntent(getIntent(), savedInstanceState);
captureManager.decode();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
captureManager.onSaveInstanceState(outState);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Final.REQUEST_EQUIP_CHICK_INFO_ACTIVITY) {
// 重新恢复扫码
captureManager.onResume();
captureManager.decode();
}
}
@OnClick({R.id.btnBack, R.id.ivEquipCheckPress, R.id.ivEquipCheckNor})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btnBack:
dbvEquipCheck.getBarcodeView().isCameraClosed();
dbvEquipCheck.pause();
finish();
break;
case R.id.ivEquipCheckPress: {
this.dbvEquipCheck.setTorchOff();
ivEquipCheckNor.setVisibility(View.VISIBLE);
ivEquipCheckPress.setVisibility(View.GONE);
break;
}
case R.id.ivEquipCheckNor: {
this.dbvEquipCheck.setTorchOn();
ivEquipCheckNor.setVisibility(View.GONE);
ivEquipCheckPress.setVisibility(View.VISIBLE);
break;
}
default:
break;
}
}
@Override
public void onCallBack(Intent data) {
// 救助体系扫码签到二维码解析
IntentResult intentResult = IntentIntegrator.parseActivityResult(REQUEST_CODE, Activity.RESULT_OK, data);
if (intentResult != null && !TextUtils.isEmpty(intentResult.getContents())) {
// 解析扫码字符串
if (intentResult.getContents().contains("tysos") && intentResult.getContents().contains("ns=")) {
Intent intent = new Intent(this, EquipCheckInfoActivity.class);
intent.putExtra(Final.SCAN_CODE, intentResult.getContents());
this.startActivityForResult(intent, Final.REQUEST_EQUIP_CHICK_INFO_ACTIVITY);
this.setResult(RESULT_OK);
} else {
ToastUtil.showMessage("不是设备二维码,请重新扫码!");
}
}
}
}
- 布局代码(参考)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<include
android:id="@+id/lyEquipCheck"
layout="@layout/top_bar" />
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/dbvEquipCheck"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/lyEquipCheck">
<ImageView
android:id="@+id/ivEquipCheckBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/scan_box" />
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="200px"
android:text="@string/scan_tip"
android:textColor="@color/white"
android:textSize="30px" />
<RelativeLayout
android:id="@+id/cbEquipCheckLight"
android:layout_width="23dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46px">
<ImageView
android:id="@+id/ivEquipCheckPress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/scan_flashlight_nor"
android:visibility="gone" />
<ImageView
android:id="@+id/ivEquipCheckNor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:src="@drawable/scan_flashlight_press" />
</RelativeLayout>
</RelativeLayout>
效果图
Screenshot_20181210-134110.jpg
网友评论