|  |  | 
 |  |  | package com.dayu.qihealonelibrary.activity; | 
 |  |  |  | 
 |  |  | import android.app.Dialog; | 
 |  |  | import android.content.Intent; | 
 |  |  | import android.os.Bundle; | 
 |  |  | import android.text.InputFilter; | 
 |  |  | 
 |  |  |  | 
 |  |  | import com.dayu.baselibrary.tools.Utils; | 
 |  |  | import com.dayu.baselibrary.utils.TipUtil; | 
 |  |  | import com.dayu.baselibrary.utils.ToastUtil; | 
 |  |  | import com.dayu.qihealonelibrary.card.UserCard; | 
 |  |  | import com.dayu.qihealonelibrary.databinding.ActivityNewCardBinding; | 
 |  |  | import com.dayu.qihealonelibrary.databinding.ActivityNewCardQhaBinding; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.AdminDataBean; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.IpBean; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.UserCardBean; | 
 |  |  |  | 
 |  |  | import com.dayu.qihealonelibrary.utils.DeviceNumberUtils; | 
 |  |  |  | 
 |  |  | import com.dayu.qihealonelibrary.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 org.json.JSONException; | 
 |  |  | import org.json.JSONObject; | 
 |  |  |  | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.Calendar; | 
 |  |  | 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, | 
 |  |  | 
 |  |  |  * Date: 2023-11-10 19:52 | 
 |  |  |  * Description: 新卡注册 | 
 |  |  |  */ | 
 |  |  | public class NewCardActivity extends QHAloneBaseActivity { | 
 |  |  | public class NewCardActivity extends BaseActivity { | 
 |  |  |  | 
 |  |  |     public static final int SCAN_IDCARD_REQUEST = 1; | 
 |  |  |     UserCardBean userCardBean; | 
 |  |  |     Dialog dialog; | 
 |  |  |  | 
 |  |  |     ActivityNewCardBinding newCardBinding; | 
 |  |  |     ActivityNewCardQhaBinding newCardBinding; | 
 |  |  |     AdminDataBean adminData; | 
 |  |  |  | 
 |  |  |     static NewCardActivity newCardActivity; | 
 |  |  |     private int defValueMainId = 2; | 
 |  |  |     private int defValueSubId = 0; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     protected void onCreate(Bundle savedInstanceState) { | 
 |  |  |         super.onCreate(savedInstanceState); | 
 |  |  |         newCardActivity = this; | 
 |  |  |         newCardBinding = ActivityNewCardBinding.inflate(LayoutInflater.from(this)); | 
 |  |  |         newCardBinding = ActivityNewCardQhaBinding.inflate(LayoutInflater.from(this)); | 
 |  |  |         setContentView(newCardBinding.getRoot()); | 
 |  |  |         setData(); | 
 |  |  |         initView(); | 
 |  |  | //        LibraryInitOCR.initOCR(NewCardActivity.this); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void initView() { | 
 |  |  | 
 |  |  |         newCardBinding.newCardScanBtn.setOnClickListener(new View.OnClickListener() { | 
 |  |  |             @Override | 
 |  |  |             public void onClick(View v) { | 
 |  |  |  | 
 |  |  |                 getPermission(); | 
 |  |  |                 rxPermission(); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |  | 
 |  |  |         //开户 | 
 |  |  |         newCardBinding.newCardRegistBtn.setOnClickListener(v -> { | 
 |  |  |  | 
 |  |  |             String userName = newCardBinding.newCardName.getText().toString(); | 
 |  |  | 
 |  |  |             ) { | 
 |  |  |                 if (userName.length() <= 1 || !validateName(userName)) { | 
 |  |  |                     TipUtil.show(NewCardActivity.this, "请输入正确姓名"); | 
 |  |  |                     return; | 
 |  |  |                 } else if (phone.length() < 11 || !isValidPhoneNumber(phone)) { | 
 |  |  |                     TipUtil.show(NewCardActivity.this, "请输入正确手机号"); | 
 |  |  |                     return; | 
 |  |  |                 } else if (!Utils.check(userID)) { | 
 |  |  |                     TipUtil.show(NewCardActivity.this, "请输入正确身份证号"); | 
 |  |  |                     return; | 
 |  |  |                 } else { | 
 |  |  |                     userCardBean = new UserCardBean(); | 
 |  |  |                     userCardBean.setUserName(userName); | 
 |  |  | 
 |  |  |                     userCardBean.setSerial(adminData.getSerial()); | 
 |  |  |                     userCardBean.setPhone(phone); | 
 |  |  |                     userCardBean.setUserID(userID); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                     UserCard userCard = new UserCard(); | 
 |  |  |                     userCard.setArerNumber(Integer.valueOf(adminData.getSerial())); | 
 |  |  |                     userCard.setDeviceNumberl(Integer.valueOf(adminData.getAddressCode())); | 
 |  |  |                     userCard.setRechargeTimes((byte) 0); | 
 |  |  |                     userCard.setTotalWater(0); | 
 |  |  |                     userCard.setTotalElectric(0); | 
 |  |  |                     userCard.setBalance(0); | 
 |  |  |                     userCard.setSurplusWater(0); | 
 |  |  |                     userCard.setRechargeDate(Calendar.getInstance()); | 
 |  |  |  | 
 |  |  |                     Intent intent = new Intent(NewCardActivity.this, NFCWreatActivity.class); | 
 |  |  |                     intent.putExtra("userCard", userCard); | 
 |  |  |                     intent.putExtra("dbUserCard", userCardBean); | 
 |  |  |                     startActivity(intent); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } else { | 
 |  |  | 
 |  |  |         return false; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private void getPermission() { | 
 |  |  |         try { | 
 |  |  |             XXPermissions.with(this) | 
 |  |  |                     // 申请单个权限 | 
 |  |  |     private void rxPermission() { | 
 |  |  |         XXPermissions.with(this) | 
 |  |  |                 // 申请单个权限 | 
 |  |  | //                    .permission(Permission.RECORD_AUDIO) | 
 |  |  |                     // 申请多个权限 | 
 |  |  |                     .permission(Permission.CAMERA) | 
 |  |  |                     // 设置权限请求拦截器(局部设置) | 
 |  |  |                     //.interceptor(new PermissionInterceptor()) | 
 |  |  |                     // 设置不触发错误检测机制(局部设置) | 
 |  |  |                     //.unchecked() | 
 |  |  |                     .request(new OnPermissionCallback() { | 
 |  |  |                 // 申请多个权限 | 
 |  |  |                 .permission(Permission.CAMERA) | 
 |  |  |                 .request(new OnPermissionCallback() { | 
 |  |  |  | 
 |  |  |                         @Override | 
 |  |  |                         public void onGranted(@NonNull List<String> permissions, boolean allGranted) { | 
 |  |  |                             if (allGranted) { | 
 |  |  |  | 
 |  |  |                             } | 
 |  |  |                     @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 { | 
 |  |  |                     @Override | 
 |  |  |                     public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) { | 
 |  |  |                         if (doNotAskAgain) { | 
 |  |  |                             // 如果是被永久拒绝就跳转到应用权限系统设置页面 | 
 |  |  |                         } else { | 
 |  |  | //                                toast("获取录音和日历权限失败"); | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     }); | 
 |  |  |         } catch ( | 
 |  |  |                 Throwable e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |                     } | 
 |  |  |                 }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | 
 |  |  |         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 startCamera() { | 
 |  |  |         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); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public int getSharedPreferencesStoreMainId() { | 
 |  |  |         return SharedPreferencesHelper.getInt( | 
 |  |  |                 getApplicationContext(), "nMainId", defValueMainId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public int getSharedPreferencesStoreSubId() { | 
 |  |  |         return SharedPreferencesHelper.getInt( | 
 |  |  |                 getApplicationContext(), "nSubID", defValueSubId); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private void setData() { | 
 |  |  |  | 
 |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public boolean validateName(String name) { |