添加了界面及逻辑
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package com.sw.plate.utils;
|
||||
|
||||
import static android.content.Context.INPUT_SERVICE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.input.InputManager;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
/**
|
||||
* 扫描枪事件处理
|
||||
*/
|
||||
public class ScanGunKeyEventHelper {
|
||||
private final static long MESSAGE_DELAY = 500; //延迟500ms,判断扫码是否完成。
|
||||
private final StringBuffer mStringBufferResult; //扫码内容
|
||||
private boolean mCaps; //大小写区分
|
||||
private final Handler mHandler;
|
||||
private final Runnable mScanningFishedRunnable;
|
||||
private OnScanSuccessListener mOnScanSuccessListener;
|
||||
private final Context mContext;
|
||||
|
||||
// private String mDeviceName = "TMC HIDKeyBoard";
|
||||
private String mDeviceName = "Linux 3.4.35 with ak-hsudc Composite Gadget (ACM + HID)";
|
||||
|
||||
public ScanGunKeyEventHelper(Context context, OnScanSuccessListener onScanSuccessListener) {
|
||||
|
||||
mContext = context;
|
||||
mOnScanSuccessListener = onScanSuccessListener;
|
||||
mStringBufferResult = new StringBuffer();
|
||||
mHandler = new Handler();
|
||||
mScanningFishedRunnable = this::performScanSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回扫码成功后的结果
|
||||
*/
|
||||
private void performScanSuccess() {
|
||||
String barcode = mStringBufferResult.toString();
|
||||
if (mOnScanSuccessListener != null && !TextUtils.isEmpty(barcode))
|
||||
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,若500ms内,有其他事件
|
||||
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) {
|
||||
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
||||
//按着shift键,表示大写
|
||||
mCaps = true;
|
||||
} else {
|
||||
//松开shift键,表示小写
|
||||
mCaps = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取扫描内容
|
||||
*
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入设备是否存在
|
||||
*
|
||||
* @param deviceName
|
||||
* @return
|
||||
*/
|
||||
public boolean isInputDeviceExist(String deviceName) {
|
||||
|
||||
InputManager inputManager = (InputManager) mContext.getSystemService(INPUT_SERVICE);
|
||||
int[] deviceIds = inputManager.getInputDeviceIds();
|
||||
for (int id : deviceIds) {
|
||||
if (inputManager.getInputDevice(id).getName().equals(deviceName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为扫码枪事件(部分机型KeyEvent获取的名字错误)
|
||||
*
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
public boolean isScanGunEvent(KeyEvent event) {
|
||||
// L.e("event===" + event.getDevice().getName() +
|
||||
// "===Char===" + event.getCharacters() +
|
||||
// "===Action===" + event.getAction());
|
||||
// return event.getDevice().getName().equals(mDeviceName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.sw.plate.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.sw.plate.App;
|
||||
import com.sw.plate.R;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
|
||||
/**
|
||||
* The type Toast utils.
|
||||
*/
|
||||
public class ToastUtils {
|
||||
|
||||
private static Toast toast;
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static TextView textCenterView;
|
||||
|
||||
/**
|
||||
* Show center toast.
|
||||
*
|
||||
* @param text the text
|
||||
*/
|
||||
public static void showToast(String text) {
|
||||
Context context = App.getContext();
|
||||
if (toast == null) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.toast_bg, null);
|
||||
textCenterView = view.findViewById(R.id.toast_tv);
|
||||
toast = new Toast(context);
|
||||
toast.setGravity(Gravity.CENTER, 0, 20);
|
||||
toast.setDuration(Toast.LENGTH_SHORT);
|
||||
toast.setView(view);
|
||||
}
|
||||
|
||||
textCenterView.setText(text);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show center toast.
|
||||
*
|
||||
* @param text the text
|
||||
*/
|
||||
public static void showToast(String text, int duration) {
|
||||
Context context = App.getContext();
|
||||
if (toast == null) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.toast_bg, null);
|
||||
textCenterView = view.findViewById(R.id.toast_tv);
|
||||
toast = new Toast(context);
|
||||
toast.setGravity(Gravity.CENTER, 0, 20);
|
||||
toast.setView(view);
|
||||
}
|
||||
|
||||
textCenterView.setText(text);
|
||||
toast.setDuration(duration);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
public static void showLongToast(String text) {
|
||||
Context context = App.getContext();
|
||||
if (toast == null) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.toast_bg, null);
|
||||
textCenterView = view.findViewById(R.id.toast_tv);
|
||||
toast = new Toast(context);
|
||||
toast.setGravity(Gravity.TOP, 0, 20);
|
||||
toast.setDuration(Toast.LENGTH_LONG);
|
||||
toast.setView(view);
|
||||
}
|
||||
textCenterView.setText(text);
|
||||
showMyToast(toast, 1000000 * 30);
|
||||
}
|
||||
|
||||
|
||||
//自定义Toast控件
|
||||
private static void showMyToast(final Toast toast, final int cnt) {
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
toast.show();
|
||||
}
|
||||
}, 0, Toast.LENGTH_LONG);
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
toast.cancel();
|
||||
timer.cancel();
|
||||
}
|
||||
}, cnt);
|
||||
}
|
||||
|
||||
public static void showSystemToast(String text) {
|
||||
Context context = App.getContext();
|
||||
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.sw.plate.utils.arcface;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arcsoft.face.FaceEngine;
|
||||
import com.arcsoft.face.enums.RuntimeABI;
|
||||
import com.sw.plate.App;
|
||||
import com.sw.plate.utils.L;
|
||||
import com.sw.plate.utils.arcface.face.model.FacePreviewInfo;
|
||||
import com.sw.plate.utils.arcface.facedb.FaceDatabase;
|
||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FaceApi {
|
||||
public interface ActiveCallback {
|
||||
void onSuccess(int code);
|
||||
|
||||
void onFail(Exception e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新人脸数据
|
||||
*
|
||||
* @param index
|
||||
* @param list
|
||||
*/
|
||||
public void updateFaceData(int index, List<FaceEntity> list) {
|
||||
if (index == 0) {
|
||||
FaceDatabase.getInstance(App.getContext()).faceDao().deleteAll();
|
||||
FaceDatabase.getInstance(App.getContext()).faceDao().resetId();
|
||||
}
|
||||
|
||||
List<FaceEntity> faceEntities = new ArrayList<>();
|
||||
FaceDatabase.getInstance(App.getContext()).faceDao().insert(faceEntities);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活arcsoft 人脸
|
||||
*
|
||||
* @param context
|
||||
* @param arcsoftAppId
|
||||
* @param arcsoftSdkKey
|
||||
* @param arcsoftActiveKey
|
||||
* @param callback
|
||||
*/
|
||||
public void activeEngine(Context context, String arcsoftAppId,
|
||||
String arcsoftSdkKey,
|
||||
String arcsoftActiveKey, ActiveCallback callback) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
RuntimeABI runtimeABI = FaceEngine.getRuntimeABI();
|
||||
L.e("subscribe: getRuntimeABI() " + runtimeABI);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
int activeCode = FaceEngine.activeOnline(context, arcsoftActiveKey,
|
||||
arcsoftAppId, arcsoftSdkKey);
|
||||
L.e("subscribe cost: " + (System.currentTimeMillis() - start));
|
||||
callback.onSuccess(activeCode);
|
||||
} catch (Exception e) {
|
||||
callback.onFail(e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入可见光相机预览数据
|
||||
*
|
||||
* @param nv21 可见光相机预览数据
|
||||
* @param doRecognize 是否进行识别
|
||||
* @return 当前帧的检测结果信息
|
||||
*/
|
||||
public List<FacePreviewInfo> onPreviewFrame(byte[] nv21, boolean doRecognize) {
|
||||
|
||||
// List<FacePreviewInfo> facePreviewInfoList = recognizeViewModel.onPreviewFrame(nv21, true);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
app:cardCornerRadius="5dp"
|
||||
app:cardPreventCornerOverlap="true">
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/toast_tv"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:maxLines="1"
|
||||
android:paddingStart="15dp"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingEnd="15dp"
|
||||
android:paddingBottom="5dp"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="28sp" />
|
||||
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
@@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="color_bg_notification">#80000000</color>
|
||||
<color name="black">#000000</color>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user