Browse Source

解决语音发送编码不对问题

vinson 5 years ago
parent
commit
82210b7e63
1 changed files with 112 additions and 150 deletions
  1. 112 150
      src/main/java/com/tidecloud/dataacceptance/web/JIngweiCarController.java

+ 112 - 150
src/main/java/com/tidecloud/dataacceptance/web/JIngweiCarController.java

@@ -1,150 +1,112 @@
-//package com.tidecloud.dataacceptance.web;
-//
-//import com.tidecloud.dataacceptance.common.HexStringUtils;
-//import com.tidecloud.dataacceptance.service.impl.JingWeiCardServerHandler;
-//import org.apache.commons.io.IOUtils;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import org.springframework.web.bind.annotation.PathVariable;
-//import org.springframework.web.bind.annotation.RequestMapping;
-//import org.springframework.web.bind.annotation.RestController;
-//
-//import java.io.IOException;
-//import java.io.InputStream;
-//
-//@RestController
-//public class JIngweiCarController {
-//
-//    @Autowired
-//    private JingWeiCardServerHandler jingWeiCardServerHandler;
-//
-//    @RequestMapping("device/{deviceId}/sendVoice")
-//    public String sendVoice(@PathVariable String deviceId) throws IOException {
-//        try {
-//
-//            String name = "jingwei_zhuangyi.amr";
-////            String name = "voice.amr";
-//
-//
-//            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(name);
-//            byte[] bs = IOUtils.toByteArray(resourceAsStream);
-//
-//
-////            String name = "/tmp/voice.amr";
-////            File f = new File(name);
-////            FileOutputStream fileOutputStream = new FileOutputStream(f);
-////            byte[] bs = new byte[(int) f.length()];
-////            fileOutputStream.write(bs);
-//            String s = HexStringUtils.toHexString(bs);
-//            String replace = s.toUpperCase()
-//                    .replace("7D", "7D01")
-//                    .replace("5B", "7D02")
-//                    .replace("5D", "7D03")
-//                    .replace("2C", "7D04")
-//                    .replace("2A", "7D05");
-////            byte[] s1 = BitOperator.hexToBytes(replace, replace.length());
-//            byte[] s1 = hexToByteArray(replace);
-////            byte[] s1 = convert2(bs);
-//
-//            jingWeiCardServerHandler.sendVoiceToDevice(deviceId, bs);
-//
-//            return "success";
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return "faile";
-//        }
-//    }
-//
-//    public static byte[] convert(byte[] bs) {
-//        byte[] resultHolder = new byte[bs.length * 2];
-//        int index = 0;
-//        for (int i = 0; i < bs.length; i = i + 2) {
-//            byte firstBit = bs[i];
-//            if (i == bs.length -1) {
-//                resultHolder[index++] = firstBit;
-//            } else {
-//                byte secondBit = bs[i + 1];
-//                if (firstBit == 0X7D) {
-//                    if (secondBit == 0X01) {
-//                        resultHolder[index++] = 0X7D;
-//                    } else if (secondBit == 0X02) {
-//                        resultHolder[index++] = 0X5B;
-//                    } else if (secondBit == 0X03) {
-//                        resultHolder[index++] = 0X5D;
-//                    } else if (secondBit == 0X04) {
-//                        resultHolder[index++] = 0X2C;
-//                    } else if (secondBit == 0X05) {
-//                        resultHolder[index++] = 0X2A;
-//                    } else {
-//                        resultHolder[index++] = firstBit;
-//                        resultHolder[index++] = secondBit;
-//                    }
-//                } else {
-//                    resultHolder[index++] = firstBit;
-//                    resultHolder[index++] = secondBit;
-//                }
-//            }
-//        }
-//
-//        byte[] result = new byte[index];
-//        for (int i = 0; i < index; i++) {
-//            result[i] = resultHolder[i];
-//        }
-//        return result;
-//    }
-//
-//    public static byte[] convert2(byte[] bs) {
-//        byte[] resultHolder = new byte[bs.length * 2];
-//        int index = 0;
-//        for (int i = 0; i < bs.length; i++) {
-//            byte b = bs[i];
-//            if (b == 0X7D) {
-//                resultHolder[index++] = 0X7D;
-//                resultHolder[index++] = 0X01;
-//            } else if (b == 0X5B) {
-//                resultHolder[index++] = 0X7D;
-//                resultHolder[index++] = 0X02;
-//            } else if (b == 0X5D) {
-//                resultHolder[index++] = 0X7D;
-//                resultHolder[index++] = 0X03;
-//            } else if (b == 0X2C) {
-//                resultHolder[index++] = 0X7D;
-//                resultHolder[index++] = 0X04;
-//            } else if (b == 0X2A) {
-//                resultHolder[index++] = 0X7D;
-//                resultHolder[index++] = 0X05;
-//            } else {
-//                resultHolder[index++] = b;
-//            }
-//        }
-//
-//        byte[] result = new byte[index];
-//        for (int i = 0; i < index; i++) {
-//            result[i] = resultHolder[i];
-//        }
-//        return result;
-//    }
-//
-//
-//    public static byte[] hexToByteArray(String inHex){
-//        int hexlen = inHex.length();
-//        byte[] result;
-//        if (hexlen % 2 == 1){
-//            //奇数
-//            hexlen++;
-//            result = new byte[(hexlen/2)];
-//            inHex="0"+inHex;
-//        }else {
-//            //偶数
-//            result = new byte[(hexlen/2)];
-//        }
-//        int j=0;
-//        for (int i = 0; i < hexlen; i+=2){
-//            result[j]=hexToByte(inHex.substring(i,i+2));
-//            j++;
-//        }
-//        return result;
-//    }
-//    public static byte hexToByte(String inHex){
-//        return (byte)Integer.parseInt(inHex,16);
-//    }
-//}
+package com.tidecloud.dataacceptance.web;
+
+import com.tidecloud.dataacceptance.service.impl.JingWeiCardServerHandler;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+@RestController
+public class JIngweiCarController {
+
+    @Autowired
+    private JingWeiCardServerHandler jingWeiCardServerHandler;
+
+    @RequestMapping("device/{deviceId}/sendVoice/")
+    public String sendVoice(@PathVariable String deviceId, @RequestParam(defaultValue = "0") int type)
+            throws IOException {
+        try {
+            byte[] bs = convertFromAMR(getBytesByName("no_network_channel_1.amr"));
+
+            jingWeiCardServerHandler.sendVoiceToDevice(deviceId, bs);
+
+            return "success";
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "faile";
+        }
+    }
+
+    private byte[] getBytesByName(String name) throws IOException {
+        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(name);
+        return IOUtils.toByteArray(resourceAsStream);
+    }
+
+    public static byte[] convertToAMR(byte[] bs) {
+        byte[] resultHolder = new byte[bs.length * 2];
+        int index = 0;
+        for (int i = 0; i < bs.length; i++) {
+            byte firstBit = bs[i];
+            if (i == bs.length - 1) {
+                resultHolder[index++] = firstBit;
+            } else {
+                byte secondBit = bs[i + 1];
+                if (firstBit == 0X7D) {
+                    boolean bingo = true;
+                    if (secondBit == 0X01) {
+                        resultHolder[index++] = 0X7D;
+                    } else if (secondBit == 0X02) {
+                        resultHolder[index++] = 0X5B;
+                    } else if (secondBit == 0X03) {
+                        resultHolder[index++] = 0X5D;
+                    } else if (secondBit == 0X04) {
+                        resultHolder[index++] = 0X2C;
+                    } else if (secondBit == 0X05) {
+                        resultHolder[index++] = 0X2A;
+                    } else {
+                        bingo = false;
+                        resultHolder[index++] = firstBit;
+                    }
+                    if (bingo) {
+                        i++;
+                    }
+                } else {
+                    resultHolder[index++] = firstBit;
+                }
+            }
+        }
+
+        byte[] result = new byte[index];
+        for (int i = 0; i < index; i++) {
+            result[i] = resultHolder[i];
+        }
+        return result;
+    }
+
+    public static byte[] convertFromAMR(byte[] bs) {
+        byte[] resultHolder = new byte[bs.length * 2];
+        int index = 0;
+        for (int i = 0; i < bs.length; i++) {
+            byte b = bs[i];
+            if (b == 0X7D) {
+                resultHolder[index++] = 0X7D;
+                resultHolder[index++] = 0X01;
+            } else if (b == 0X5B) {
+                resultHolder[index++] = 0X7D;
+                resultHolder[index++] = 0X02;
+            } else if (b == 0X5D) {
+                resultHolder[index++] = 0X7D;
+                resultHolder[index++] = 0X03;
+            } else if (b == 0X2C) {
+                resultHolder[index++] = 0X7D;
+                resultHolder[index++] = 0X04;
+            } else if (b == 0X2A) {
+                resultHolder[index++] = 0X7D;
+                resultHolder[index++] = 0X05;
+            } else {
+                resultHolder[index++] = b;
+            }
+        }
+
+        byte[] result = new byte[index];
+        for (int i = 0; i < index; i++) {
+            result[i] = resultHolder[i];
+        }
+        return result;
+    }
+}