读取b3dm文件


1,java读取b3d文件,拆分为gltf和json文件

import java.io.*;

// https://www.cnblogs.com/onsummer/p/13252896.html
// byteLength = 28 + featureTableJSONByteLength + featureTableBinaryByteLength + batchTableJSONByteLength + batchTableBinaryByteLength + glb的字节长度

public class ReadB3DM {

    public static void main(String[] args) throws Exception {
//        String dirname = "/IdeaProjects/demo/ddd/";
//        getFiles(dirname);

        String dirname = "/IdeaProjects/demo/json/";
        getJson(dirname);
    }

// json 合并
    public static void getJson(String dirname) throws IOException {
        File file = new File(dirname);
        File[] list = file.listFiles();
        if (list != null) {
            int index = 1;
            StringBuilder sb = new StringBuilder("[");
            for (File f : list) {
                if(!f.isDirectory() && f.getPath().contains("b3dm")){
                    FileInputStream fit = new FileInputStream(f.getPath());
                    byte[] bytes = new byte[8192];
                    int len = 0;
                    while ((len = fit.read(bytes)) != -1){
                        sb.append(new String(bytes,0 ,len));
                    }
                    index++;
                    if(index != list.length){
                        sb.append(",");
                    }else {
                        sb.append("]");
                    }
                }
            }
            File json = new File("/IdeaProjects/demo/json/000.all.json");
            BufferedWriter out = new BufferedWriter(new FileWriter(json,true));
            out.write(sb.toString());
            out.flush();
            out.close();
        }
    }

    public static void getFiles(String dirname) throws Exception {
        File file = new File(dirname);
        File[] list = file.listFiles();
        if (list != null) {
            for (File f : list) {
                if (f.isDirectory()) {
                    getFiles(f.getPath());
                } else if(!f.getPath().contains("json") && !f.getPath().contains("gltf")){
                    System.out.println(f.getPath());
                    run(f.getPath());
                }
            }
        }
    }

    public static void run(String path) throws Exception {
        // 读取所有的字节数据
        String gltfPath = path.replace("b3dm", "gltf");
        File file = new File(path);
        File file2 = new File(gltfPath);
        long len = file.length();
        FileInputStream in = new FileInputStream(file);
        FileOutputStream out = new FileOutputStream(file2);
        byte[] bytes = new byte[1024];
        byte[] data = new byte[(int) len];
        int c;
        int num = 0;
        while ((c = in.read(bytes)) != -1) {
            for (int i = 0; i < bytes.length; i++) {
                if (num * 1024L + i < len) {
                    data[num * 1024 + i] = bytes[i];
                }
            }
            num++;
        }

        // 解析header
        String magic = new String(subBytes(data, 0, 4));
        int version = bytesToInt(subBytes(data, 4, 4), 0);
        int byteLen = bytesToInt(subBytes(data, 8, 4), 0);
        int featureTableJsonByteLen = bytesToInt(subBytes(data, 12, 4), 0);
        int featureTableBinByteLen = bytesToInt(subBytes(data, 16, 4), 0);
        int batchTableJsonByteLen = bytesToInt(subBytes(data, 20, 4), 0);
        int batchTableBinByteLen = bytesToInt(subBytes(data, 24, 4), 0);
        System.out.println("magic: " + magic);
        System.out.println("version: " + version);
        System.out.println("byteLen: " + byteLen);
        System.out.println("featureTableJsonByteLen: " + featureTableJsonByteLen);
        System.out.println("featureTableBinByteLen: " + featureTableBinByteLen);
        System.out.println("batchTableJsonByteLen: " + batchTableJsonByteLen);
        System.out.println("batchTableBinByteLen: " + batchTableBinByteLen);
        System.out.println("---------------------------------------------------");

        // 解析featureTable表
        String featureTableJson = new String(subBytes(data, 28, featureTableJsonByteLen));
        System.out.println(featureTableJson);

        System.out.println("---------------------------------------------------");
        int offset = 28 + featureTableJsonByteLen + featureTableBinByteLen;
        String batchTableJson = new String(subBytes(data, offset, batchTableJsonByteLen));
        String name = path.replace("/IdeaProjects/demo/ddd/", "").replace("/", "");
        FileOutputStream json = new FileOutputStream("/IdeaProjects/demo/json/" + name + ".json");
        System.out.println(batchTableJson);
        json.write(batchTableJson.getBytes());
        json.close();

        System.out.println("-----------------------生成gltf----------------------------");
        offset = 28 + featureTableJsonByteLen + featureTableBinByteLen + batchTableJsonByteLen + batchTableBinByteLen;
        out.write(subBytes(data, offset, (int) len - offset));
        out.close();

        in.close();
    }


    // 截取byte数组
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, begin + count - begin);
        return bs;
    }

    // byte数组转整型
    public static int bytesToInt(byte[] src, int offset) {
        return (int) ((src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24));
    }
}

2,由于这里使用java 操作json比较麻烦,所以我使用js操作

import bigData from './json/000.all.json';

let bigArr = [];

bigData.map(data => {
    const arr = [];
    const keys = Object.keys(data);
    data[keys[0]].map((_, i) => {
        arr[i] = {};
        keys.map(k => {
            arr[i][k] = data[k][i]
        })
    });
    arr.map(d => bigArr.push(d));
})
console.log(bigArr)

let blob = new Blob([JSON.stringify(bigArr)])
let link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = 'all.json';
link.click();

// function setArr(path) {
//    return  import(path).then(data => {
//         const arr = [];
//         const keys = Object.keys(data);
//         data[keys[0]].map((_, i) => {
//             arr[i] = {};
//             keys.map(k => {
//                 arr[i][k] = data[k][i]
//             })
//         });
//         return arr;
//     })
//
// }
//
// setArr('./json/000.b3dm.json').then(d => console.log(d));

3,以上代码来源与网上查阅与本人修改所得