From 1b4835badb3ca32402b6544c668a700c5334d90f Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 26 三月 2024 16:48:31 +0800
Subject: [PATCH] 添加中安身份证识别模块 把IP设置移动到参数设置界面

---
 app/src/main/java/com/dayu/recharge/card/DomainCard.java |   92 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 83 insertions(+), 9 deletions(-)

diff --git a/app/src/main/java/com/dayu/recharge/card/DomainCard.java b/app/src/main/java/com/dayu/recharge/card/DomainCard.java
index 55d8718..04e6ca7 100644
--- a/app/src/main/java/com/dayu/recharge/card/DomainCard.java
+++ b/app/src/main/java/com/dayu/recharge/card/DomainCard.java
@@ -3,10 +3,14 @@
 import com.dayu.recharge.tools.BcdUtil;
 import com.dayu.recharge.tools.HexUtil;
 import com.dayu.recharge.utils.MornyUtil;
+import com.dayu.recharge.utils.MyCommon;
+import com.tencent.bugly.crashreport.CrashReport;
 
 import java.io.Serializable;
 import java.net.IDN;
+import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
+import java.util.List;
 
 /**
  * Copyright (C), 2023,
@@ -17,13 +21,37 @@
 public class DomainCard implements Serializable {
 
 
-    public String cardType = "C1";//鍗$被鍨�
+    public String cardType = MyCommon.DOMAIN_CARD_TYPE;//鍗$被鍨�
     public String domainNumber;//鍩熷悕搴忓彿 (BCD鏍煎紡)
     public int domainLength;//鍩熷悕闀垮害
 
     //绔彛鍙蜂负5涓瓧鑺傦紝涓嶈冻5浣嶉珮浣嶈ˉ0銆傜鍙e彿涓庡煙鍚嶄箣闂寸敤鈥�,鈥濋殧寮�銆傜鍙e彿鏈�鍚庝互鈥�#鈥濈粨鏉�
     public String domainName;//鍩熷悕
-    public String port;//绔彛
+    public int port;//绔彛
+
+    public String getDomainName() {
+        return domainName;
+    }
+
+    public void setDomainName(String domainName) {
+        this.domainName = domainName;
+    }
+
+    public String getDomainNumber() {
+        return domainNumber;
+    }
+
+    public void setDomainNumber(String domainNumber) {
+        this.domainNumber = domainNumber;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
 
     private byte[] asciiByte;//褰撳墠鍩熷悕鍜岀鍙e彿鏁扮粍
 
@@ -43,6 +71,44 @@
     }
 
 
+    public static DomainCard toBean(List<byte[]> data) {
+        try {
+            DomainCard domainCard = new DomainCard();
+            String domainNumber = BcdUtil.bcdToStr(data.get(0)[1]);
+            domainCard.domainNumber = domainNumber;
+            int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[2]));
+            byte[] domainData = new byte[domainLength + 7];
+            int index = 0;
+            for (int i = 0; i < 3; i++) {
+                int jIndex;
+                if (i == 0) {
+                    jIndex = 3;
+                } else {
+                    jIndex = 0;
+                }
+                for (int j = jIndex; j < 15; j++) {
+                    if (index < domainData.length) {
+                        domainData[index] = data.get(i)[j];
+                        index++;
+                    }
+
+                }
+            }
+            String domainStr = new String(domainData, StandardCharsets.US_ASCII);
+            String domainName = domainStr.split(",")[0];
+            String port = domainStr.split(",")[1].replace("#", "");
+            domainCard.setDomainName(domainName);
+            domainCard.setPort(Integer.valueOf(port));
+
+            return domainCard;
+        } catch (Exception e) {
+            e.printStackTrace();
+            CrashReport.postCatchedException(e);
+        }
+        return null;
+    }
+
+
     /**
      * 鐢ㄦ埛鍗�0鍧�
      */
@@ -50,10 +116,18 @@
         public byte[] toByte() {
             byte[] data = new byte[16];
             data[0] = HexUtil.hexToByte(cardType);
-            byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
-            data[1] = domainNumbers[0];
+            if (domainNumber != null) {
+                byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
+                data[1] = domainNumbers[0];
+            }
+            domainLength = domainName.length();
             data[2] = HexUtil.hexToByte(HexUtil.get10to16(domainLength));
-            String ascii = IDN.toASCII(domainName + "," + port + "#");
+            String portStr = String.valueOf(port);
+            int lenght = portStr.length();
+            for (int i = 0; i < 5 - lenght; i++) {
+                portStr = "0" + portStr;
+            }
+            String ascii = IDN.toASCII(domainName + "," + portStr + "#");
             asciiByte = ascii.getBytes();
             if (asciiByte.length < 12) {
                 System.arraycopy(asciiByte, 0, data, 3, asciiByte.length);
@@ -75,9 +149,9 @@
             byte[] data = new byte[16];
             if (asciiByte.length > 12) {
                 if (asciiByte.length - 12 < 15) {
-                    System.arraycopy(asciiByte, 12, data, 3, asciiByte.length - 12);
+                    System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - 12);
                 } else {
-                    System.arraycopy(asciiByte, 12, data, 3, 15);
+                    System.arraycopy(asciiByte, 12, data, 0, 15);
                 }
             }
             data[15] = getByteSum(data);
@@ -95,9 +169,9 @@
             byte[] data = new byte[16];
             if (asciiByte.length > (12 + 15)) {
                 if (asciiByte.length - (12 + 15) < 15) {
-                    System.arraycopy(asciiByte, 12, data, 3, asciiByte.length - (12 + 15));
+                    System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - (12 + 15));
                 } else {
-                    System.arraycopy(asciiByte, 12, data, 3, 15);
+                    System.arraycopy(asciiByte, 12, data, 0, 15);
                 }
             }
             data[15] = getByteSum(data);

--
Gitblit v1.8.0