| | |
| | | package com.dayu.recharge.activity; |
| | | |
| | | import android.Manifest; |
| | | import android.app.Dialog; |
| | | import android.app.ProgressDialog; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.text.InputFilter; |
| | | import android.text.Spanned; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | |
| | | import com.dayu.recharge.utils.DeviceNumberUtils; |
| | | import com.msd.ocr.idcard.LibraryInitOCR; |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import com.dayu.recharge.MyApplication; |
| | | import com.dayu.recharge.card.UserCard; |
| | | import com.dayu.recharge.databinding.ActivityNewCardBinding; |
| | |
| | | import com.dayu.recharge.tools.HexUtil; |
| | | import com.dayu.recharge.tools.Utils; |
| | | import com.dayu.recharge.utils.CRC8; |
| | | import com.dayu.recharge.utils.DeviceNumberUtils; |
| | | import com.dayu.recharge.utils.SocketUtil; |
| | | import com.dayu.recharge.utils.TipUtil; |
| | | import com.permissionx.guolindev.PermissionX; |
| | | |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | import com.dayu.recharge.utils.ToastUtil; |
| | | import com.dayu.recharge.view.ProgressDialog; |
| | | import com.hjq.permissions.OnPermissionCallback; |
| | | import com.hjq.permissions.Permission; |
| | | import com.hjq.permissions.XXPermissions; |
| | | import com.kernal.passportreader.sdk.CardsCameraActivity; |
| | | import com.kernal.passportreader.sdk.utils.DefaultPicSavePath; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import kernal.idcard.android.ResultMessage; |
| | | import kernal.idcard.camera.CardOcrRecogConfigure; |
| | | import kernal.idcard.camera.SharedPreferencesHelper; |
| | | |
| | | /** |
| | | * Copyright (C), 2023, |
| | |
| | | |
| | | public static final int SCAN_IDCARD_REQUEST = 1; |
| | | UserCardBean userCardBean; |
| | | Dialog dialog; |
| | | |
| | | ActivityNewCardBinding newCardBinding; |
| | | AdminDataBean adminData; |
| | |
| | | @Override |
| | | public void onClick(View v) { |
| | | |
| | | getPermission(); |
| | | } |
| | | }); |
| | | |
| | | newCardBinding.newCardRegistBtn.setOnClickListener(v -> { |
| | | |
| | | String userName = newCardBinding.newCardName.getText().toString(); |
| | | long date = System.currentTimeMillis(); |
| | | String phone = newCardBinding.newCardPhone.getText().toString(); |
| | | String userID = newCardBinding.newCardId.getText().toString().toUpperCase(); |
| | | |
| | | |
| | | if (!TextUtils.isEmpty(userName) |
| | | && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(userID) |
| | | ) { |
| | | if (userName.length() <= 1 || !validateName(userName)) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确姓名"); |
| | | } else if (phone.length() < 11 || !isValidPhoneNumber(phone)) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确手机号"); |
| | | } else if (!Utils.check(userID)) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确身份证号"); |
| | | } else { |
| | | userCardBean = new UserCardBean(); |
| | | userCardBean.setUserName(userName); |
| | | userCardBean.setDate(date); |
| | | userCardBean.setSerial(adminData.getSerial()); |
| | | userCardBean.setPhone(phone); |
| | | userCardBean.setUserID(userID); |
| | | initCard(); |
| | | } |
| | | |
| | | } else { |
| | | TipUtil.show(NewCardActivity.this, "请输入完整内容"); |
| | | } |
| | | }); |
| | | |
| | | //身份证识别 |
| | | newCardBinding.newCardScanBtn.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | |
| | | rxPermission(); |
| | | } |
| | | }); |
| | | |
| | | newCardBinding.newCardRegistBtn.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | |
| | | String userName = newCardBinding.newCardName.getText().toString(); |
| | | long date = System.currentTimeMillis(); |
| | | String phone = newCardBinding.newCardPhone.getText().toString(); |
| | | String userID = newCardBinding.newCardId.getText().toString().toUpperCase(); |
| | | |
| | | |
| | | if (!TextUtils.isEmpty(userName) |
| | | && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(userID) |
| | | ) { |
| | | if (userName.length() <= 1 || !validateName(userName)) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确姓名"); |
| | | return; |
| | | } else if (phone.length() < 11) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确手机号"); |
| | | return; |
| | | } else if (!Utils.check(userID)) { |
| | | TipUtil.show(NewCardActivity.this, "请输入正确身份证号"); |
| | | return; |
| | | } else { |
| | | dialog = ProgressDialog.show(NewCardActivity.this, "", "正在通信请稍后", true, true); |
| | | userCardBean = new UserCardBean(); |
| | | userCardBean.setUserName(userName); |
| | | userCardBean.setDate(date); |
| | | userCardBean.setSerial(adminData.getSerial()); |
| | | userCardBean.setPhone(phone); |
| | | userCardBean.setUserID(userID); |
| | | initCard(); |
| | | } |
| | | |
| | | } else { |
| | | TipUtil.show(NewCardActivity.this, "请输入完整内容"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void rxPermission() { |
| | | PermissionX.init(NewCardActivity.this).permissions(Manifest.permission.CAMERA) |
| | | .onExplainRequestReason((scope, deniedList) -> { |
| | | XXPermissions.with(this) |
| | | // 申请单个权限 |
| | | // .permission(Permission.RECORD_AUDIO) |
| | | // 申请多个权限 |
| | | .permission(Permission.CAMERA) |
| | | .request(new OnPermissionCallback() { |
| | | |
| | | // val message = "PermissionX需要您同意以下权限才能正常使用" |
| | | }) |
| | | .request((allGranted, grantedList, deniedList) -> { |
| | | if (allGranted) {//所有申请的权限都已通过 |
| | | startCamera(); |
| | | } else {//您拒绝了如下权限:$deniedList |
| | | @Override |
| | | public void onGranted(@NonNull List<String> permissions, boolean allGranted) { |
| | | if (allGranted) {//所有申请的权限都已通过 |
| | | startCamera(); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) { |
| | | if (doNotAskAgain) { |
| | | // 如果是被永久拒绝就跳转到应用权限系统设置页面 |
| | | } else { |
| | | // toast("获取录音和日历权限失败"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | private void startCamera() { |
| | | |
| | | Bundle bundle = new Bundle(); |
| | | bundle.putBoolean("saveImage", false); |
| | | bundle.putInt("requestCode", SCAN_IDCARD_REQUEST); |
| | | bundle.putInt("type", 0); //0身份证, 1驾驶证 |
| | | LibraryInitOCR.startScan(NewCardActivity.this, bundle); |
| | | CardOcrRecogConfigure.getInstance() |
| | | .initLanguage(getApplicationContext()) |
| | | .setSaveCut(true) |
| | | .setOpenIDCopyFuction(true) |
| | | .setnMainId(getSharedPreferencesStoreMainId()) |
| | | .setnSubID(getSharedPreferencesStoreSubId()) |
| | | .setFlag(0) |
| | | .setnCropType(0) |
| | | .setSavePath(new DefaultPicSavePath(this, true)); |
| | | Intent intent = new Intent(this, CardsCameraActivity.class); |
| | | startActivityForResult(intent, SCAN_IDCARD_REQUEST); |
| | | } |
| | | |
| | | private int defValueMainId = 2; |
| | | private int defValueSubId = 0; |
| | | |
| | | public int getSharedPreferencesStoreMainId() { |
| | | return SharedPreferencesHelper.getInt( |
| | | getApplicationContext(), "nMainId", defValueMainId); |
| | | } |
| | | |
| | | public int getSharedPreferencesStoreSubId() { |
| | | return SharedPreferencesHelper.getInt( |
| | | getApplicationContext(), "nSubID", defValueSubId); |
| | | } |
| | | |
| | | private boolean isValidPhoneNumber(String phoneNumber) { |
| | | // 定义手机号的正则表达式,确保数字部分没有连续6位相同的数字 |
| | | String phoneRegex = "^1[0-9]{10}$"; |
| | | |
| | | // 创建 Pattern 对象 |
| | | Pattern pattern = Pattern.compile(phoneRegex); |
| | | |
| | | // 创建 matcher 对象 |
| | | Matcher matcher = pattern.matcher(phoneNumber); |
| | | |
| | | // 判断手机号是否匹配正则表达式 |
| | | return matcher.matches() && !hasSixConsecutiveSameDigits(phoneNumber); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否有6个相同的连续数字 |
| | | * |
| | | * @param input |
| | | * @return |
| | | */ |
| | | public static boolean hasSixConsecutiveSameDigits(String input) { |
| | | char[] digits = input.toCharArray(); |
| | | |
| | | for (int i = 0; i <= digits.length - 6; i++) { |
| | | boolean consecutiveSame = true; |
| | | for (int j = 1; j < 6; j++) { |
| | | if (digits[i + j] != digits[i + j - 1]) { |
| | | consecutiveSame = false; |
| | | break; |
| | | } |
| | | } |
| | | if (consecutiveSame) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | private void getPermission() { |
| | | try { |
| | | XXPermissions.with(this) |
| | | // 申请单个权限 |
| | | // .permission(Permission.RECORD_AUDIO) |
| | | // 申请多个权限 |
| | | .permission(Permission.CAMERA) |
| | | // 设置权限请求拦截器(局部设置) |
| | | //.interceptor(new PermissionInterceptor()) |
| | | // 设置不触发错误检测机制(局部设置) |
| | | //.unchecked() |
| | | .request(new OnPermissionCallback() { |
| | | |
| | | @Override |
| | | public void onGranted(@NonNull List<String> permissions, boolean allGranted) { |
| | | if (allGranted) { |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) { |
| | | if (doNotAskAgain) { |
| | | // 如果是被永久拒绝就跳转到应用权限系统设置页面 |
| | | } else { |
| | | // toast("获取录音和日历权限失败"); |
| | | } |
| | | } |
| | | }); |
| | | } catch ( |
| | | Throwable e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| | |
| | | if (resultCode == RESULT_OK) { |
| | | switch (requestCode) { |
| | | case SCAN_IDCARD_REQUEST: |
| | | String result = data.getStringExtra("OCRResult"); |
| | | try { |
| | | JSONObject jo = new JSONObject(result); |
| | | //跳转扫描界面识别完成之后,数据回传 |
| | | if (data != null) { |
| | | //数据回传的获取 |
| | | Bundle bundle = data.getBundleExtra("resultbundle"); |
| | | //bundle不为null,代表这识别成功 |
| | | if (bundle != null) { |
| | | ResultMessage resultMessage = (ResultMessage) bundle.getSerializable("resultMessage"); |
| | | newCardBinding.newCardName.setText(resultMessage.GetRecogResult[1]); |
| | | newCardBinding.newCardId.setText(resultMessage.GetRecogResult[6]); |
| | | newCardBinding.newCardIdTip.setVisibility(View.VISIBLE); |
| | | } else { |
| | | String error = data.getStringExtra("error"); |
| | | String StrPath = data.getStringExtra("strpicpath"); |
| | | ToastUtil.show(error); |
| | | } |
| | | |
| | | // sb.append(String.format("正面 = %s\n", jo.opt("type"))); |
| | | // sb.append(String.format("姓名 = %s\n", jo.opt("name"))); |
| | | // sb.append(String.format("性别 = %s\n", jo.opt("sex"))); |
| | | // sb.append(String.format("民族 = %s\n", jo.opt("folk"))); |
| | | // sb.append(String.format("日期 = %s\n", jo.opt("birt"))); |
| | | // sb.append(String.format("号码 = %s\n", jo.opt("num"))); |
| | | // sb.append(String.format("住址 = %s\n", jo.opt("addr"))); |
| | | // sb.append(String.format("签发机关 = %s\n", jo.opt("issue"))); |
| | | // sb.append(String.format("有效期限 = %s\n", jo.opt("valid"))); |
| | | // sb.append(String.format("整体照片 = %s\n", jo.opt("imgPath"))); |
| | | // sb.append(String.format("头像路径 = %s\n", jo.opt("headPath"))); |
| | | |
| | | newCardBinding.newCardName.setText(jo.opt("name").toString()); |
| | | newCardBinding.newCardId.setText(jo.opt("num").toString()); |
| | | newCardBinding.newCardIdTip.setVisibility(View.VISIBLE); |
| | | |
| | | } catch (JSONException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | |
| | | * 开卡报文 |
| | | */ |
| | | private void initCard() { |
| | | |
| | | ProgressDialog.show(this); |
| | | String initCode = DeviceNumberUtils.getDeviceNumber(); |
| | | if (TextUtils.isEmpty(initCode)) { |
| | | TipUtil.show(this, "设备注册号为空,请先设置IP和管理员地址"); |
| | |
| | | initCardRequestBean.setPhone(phoneBuffer.toString()); |
| | | String userId = userCardBean.getUserID(); |
| | | initCardRequestBean.setIdCard(SocketUtil.str2HexStr(userId)); |
| | | SocketNet.getInstance().sendTestMessage(initCardRequestBean.pack(), new SocketCallBack() { |
| | | SocketNet.getInstance().sendTestMessage(initCardRequestBean.pack(), "93", new SocketCallBack() { |
| | | |
| | | @Override |
| | | public void onSocketResponse(SocketData readData) { |
| | | dialog.dismiss(); |
| | | ProgressDialog.dismiss(); |
| | | if (CRC8.isCRC8(readData.getOriginDataBytes())) { |
| | | |
| | | String state = BcdUtil.bcdToStr(readData.getBodyBytes()[26]); //BCD码00成功01身份证已注册02其他原因失败 |
| | | Log.i("initCard", "state:" + state); |
| | | if (state.equalsIgnoreCase("00")) { |
| | | String initPeasantCode = HexUtil.bytesToHex(Arrays.copyOfRange(readData.getBodyBytes(), 10, 26)); |
| | | |
| | | String initPeasantCode = HexUtil.bytesToHex(Arrays.copyOfRange(readData.getBodyBytes(), 10, 26)); |
| | | String serial = baseDao.adminDao().findFirst().getSerial(); |
| | | UserCard userCard = new UserCard(); |
| | | userCard.setRechargeTimes(0); |
| | |
| | | |
| | | @Override |
| | | public void onSocketError(int code, String msg) { |
| | | dialog.dismiss(); |
| | | TipUtil.show(NewCardActivity.this, msg); |
| | | |
| | | runOnUiThread(() -> { |
| | | ProgressDialog.dismiss(); |
| | | TipUtil.show(NewCardActivity.this, msg); |
| | | }); |
| | | } |
| | | }); |
| | | |
| | |
| | | // 汉字的Unicode范围是:0x4e00 - 0x9fa5 |
| | | return (c >= 0x4e00 && c <= 0x9fa5); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | newCardActivity = null; |
| | | } |
| | | |
| | | } |