From 238e5d0384db7a01d782093db2c823b2456f0518 Mon Sep 17 00:00:00 2001 From: nieziyan Date: Wed, 18 Oct 2023 11:32:13 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9AReadSCP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/modules/controller/Demo.java | 107 ++++++++++++------ 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/Demo.java b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/Demo.java index fcbb39bd..4522ff26 100644 --- a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/Demo.java +++ b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/controller/Demo.java @@ -1,22 +1,22 @@ package org.jeecg.modules.controller; -import cn.hutool.core.util.CharUtil; -import cn.hutool.core.util.CharsetUtil; import org.jeecg.common.constant.DateConstant; import org.jeecg.modules.base.entity.SpcHead1; import org.jeecg.modules.base.entity.SpcHead2; import org.jeecg.modules.entity.vo.FileData; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; +import java.nio.*; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import static java.lang.Math.pow; @@ -24,20 +24,16 @@ public class Demo { public static void main(String[] args) throws IOException { FileData fileData = new FileData(); - String filePath = "C:\\\\Users\\\\a\\\\Desktop\\\\shift\\\\standard.SPC"; + String filePath = "C:\\Users\\a\\Desktop\\shift\\standard.SPC"; // r:只读 rw:读写 RandomAccessFile SpcFile = new RandomAccessFile(filePath, "r"); short Inftyp; short Filtyp; byte[] buffer = new byte[2]; SpcFile.read(buffer); - ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); - byteBuffer.order(ByteOrder.LITTLE_ENDIAN); - Inftyp = byteBuffer.getShort(); + Inftyp = byteToShort(buffer); SpcFile.read(buffer); - byteBuffer = ByteBuffer.wrap(buffer); - byteBuffer.order(ByteOrder.LITTLE_ENDIAN); - Filtyp = byteBuffer.getShort(); + Filtyp = byteToShort(buffer); if (Inftyp != 1 || Filtyp != 1) return; // 读入整形spc文件的头记录(第一个记录)-128字节 SpcHead1 head1 = new SpcHead1(); @@ -74,6 +70,7 @@ public class Demo { AcqY = (int) (AcqY + temchar[i] * pow(10, 10 - i)); } + AcqM = 0; if (temchar[3] == 'J' && temchar[4] == 'a' && temchar[5] == 'n') AcqM = 1; if (temchar[3] == 'F' && temchar[4] == 'e' && temchar[5] == 'b') AcqM = 2; if (temchar[3] == 'M' && temchar[4] == 'a' && temchar[5] == 'r') AcqM = 3; @@ -193,6 +190,7 @@ public class Demo { CalD = (int) (CalD + temchar[i] * pow(10, 1 - i)); } + CalM = 0; SpcFile.read(temByte, 0, 10 * 2); temchar = byteToChar(temByte); temchar[9] = '\0'; @@ -228,18 +226,18 @@ public class Demo { byte[] fWHMByte = new byte[32 * 4]; lOffset = (head1.EngPrRP - 1) * 128; - SpcFile.seek(lOffset * 2); + SpcFile.seek(lOffset * 2L); SpcFile.read(cerByte, 0, 128 * 2); - float Cer[] = byteToFloat(cerByte); + float[] Cer = byteToFloats(cerByte); SpcFile.read(engByte, 0, 128 * 2); - float Eng[] = byteToFloat(engByte); + float[] Eng = byteToFloats(engByte); SpcFile.read(fWHMByte, 0, 128 * 2); - float FWHM[] = byteToFloat(fWHMByte); + float[] FWHM = byteToFloats(fWHMByte); short EngNum; byte[] engNumByte = new byte[4]; lOffset = (head1.CalRP1 - 1) * 128; - SpcFile.seek((lOffset + 74) * 2); + SpcFile.seek((lOffset + 74) * 2L); SpcFile.read(engNumByte, 0, 2 * 2); EngNum = byteToShort(engNumByte); @@ -273,7 +271,7 @@ public class Demo { if (head1.EffRP > 0) { // 存在效率刻度 byte[] effNumByte = new byte[4]; lOffset = (head1.CalRP1 - 1) * 128; - SpcFile.seek((lOffset + 4) * 2); + SpcFile.seek((lOffset + 4) * 2L); SpcFile.read(effNumByte,0,2); short EffNum = byteToShort(effNumByte); @@ -282,9 +280,9 @@ public class Demo { float EffUnc = 0.5F; lOffset = (head1.EffRP - 1) * 128; - SpcFile.seek(lOffset * 2); + SpcFile.seek(lOffset * 2L); SpcFile.read(effByte, 0, 128 * 2); - float Eff[] = byteToFloat(effByte); + float[] Eff = byteToFloats(effByte); for (int i = 0; i < EffNum; i++) { fileData.getVvEffi().get(0).add((double) Eff[i * 2]); @@ -294,35 +292,55 @@ public class Demo { } // Spectrum - int[] SpcData = new int[100000]; + byte[] SpcDataByte = new byte[100000]; fileData.setNum_count(head1.SpcChnNum); fileData.setStart_chan(head2.Chnsrt); float EngA, EngB, EngC; + byte[] engAByte = new byte[8]; + byte[] engBByte = new byte[8]; + byte[] engCByte = new byte[8]; lOffset = (head1.CalRP1 - 1) * 128; - SpcFile.seek(lOffset + 20); - SpcFile.read(( char*)&EngA, 4); - SpcFile.read(( char*)&EngB, 4); - SpcFile.read(( char*)&EngC, 4); - fileData.max_energy = EngA + EngB * fileData.num_count + EngC * pow(fileData.num_count, 2); + SpcFile.seek((lOffset + 20) * 2L); + SpcFile.read(engAByte,0, 4 * 2); + SpcFile.read(engBByte,0, 4 * 2); + SpcFile.read(engCByte,0, 4 * 2); + EngA = byteToFloat(engAByte); + EngB = byteToFloat(engBByte); + EngC = byteToFloat(engCByte); + int numCount = fileData.getNum_count(); + int max_energy = (int) (EngA + EngB * numCount + + EngC * pow(numCount, 2)); + fileData.setMax_energy(max_energy); lOffset = (head1.SpcFirstRP - 1) * 128; - SpcFile.seek(lOffset); - SpcFile.read(( char*)SpcData, fileData.num_count * 4); + SpcFile.seek(lOffset * 2L); + SpcFile.read(SpcDataByte, 0, numCount * 4 * 2); + int[] SpcData = byteToInts(SpcDataByte); - for (int i = 0; i < fileData.num_count; i++) { - fileData.vCounts.push_back(SpcData[i]); + for (int i = 0; i < numCount; i++) { + fileData.getVCounts().add((long) SpcData[i]); } SpcFile.close(); } - public static char[] byteToChar(byte[] data) { - return new String(data, StandardCharsets.UTF_8).toCharArray(); + + public static char[] byteToChar(byte[] data) throws CharacterCodingException { + /*return new String(data, StandardCharsets.UTF_16).toCharArray();*/ + /*CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); + String str = decoder.decode(ByteBuffer.wrap(data)).toString(); + return str.toCharArray();*/ + ByteBuffer bb = ByteBuffer.allocate (data.length); + bb.put (data); + bb.flip (); + Charset cs = StandardCharsets.UTF_8; + CharBuffer cb = cs.decode (bb); + return cb.array(); } - public static float[] byteToFloat(byte[] data) { + public static float[] byteToFloats(byte[] data) { // 每个浮点数占用4个字节 int floatCount = data.length / 4; float[] floatArray = new float[floatCount]; @@ -335,8 +353,27 @@ public class Demo { return floatArray; } - public static short byteToShort(byte[] byteData) { - ByteBuffer buffer = ByteBuffer.wrap(byteData); + public static float byteToFloat(byte[] data) { + ByteBuffer buffer = ByteBuffer.wrap(data); + // 设置字节顺序为小端序 + buffer.order(ByteOrder.LITTLE_ENDIAN); + FloatBuffer floatBuffer = buffer.asFloatBuffer(); + return floatBuffer.get(); + } + + public static int[] byteToInts(byte[] data) { + ByteBuffer buffer = ByteBuffer.wrap(data); + // 设置字节顺序为小端序 + buffer.order(ByteOrder.LITTLE_ENDIAN); + IntBuffer intBuffer = buffer.asIntBuffer(); + int[] intArray = new int[intBuffer.remaining()]; + intBuffer.get(intArray); + return intArray; + } + + public static short byteToShort(byte[] data) { + ByteBuffer buffer = ByteBuffer.wrap(data); + // 设置字节顺序为小端序 buffer.order(ByteOrder.LITTLE_ENDIAN); return buffer.getShort(); }