栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中的OpenCV Mat对象序列化

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的OpenCV Mat对象序列化

我从这里做了一些改进。在这里测试和工作SerializationUtils类

public static String matToJson(Mat mat){    JsonObject obj = new JsonObject();    if(mat.isContinuous()){        int cols = mat.cols();        int rows = mat.rows();        int elemSize = (int) mat.elemSize();        int type = mat.type();        obj.addProperty("rows", rows);        obj.addProperty("cols", cols);        obj.addProperty("type", type);        // We cannot set binary data to a json object, so:        // Encoding data byte array to base64.        String dataString;        if( type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S) { int[] data = new int[cols * rows * elemSize]; mat.get(0, 0, data); dataString = new String(base64.enprebase64(SerializationUtils.toByteArray(data)));        }        else if( type == CvType.CV_32F || type == CvType.CV_32FC2) { float[] data = new float[cols * rows * elemSize]; mat.get(0, 0, data); dataString = new String(base64.enprebase64(SerializationUtils.toByteArray(data)));        }        else if( type == CvType.CV_64F || type == CvType.CV_64FC2) { double[] data = new double[cols * rows * elemSize]; mat.get(0, 0, data); dataString = new String(base64.enprebase64(SerializationUtils.toByteArray(data)));        }        else if( type == CvType.CV_8U ) { byte[] data = new byte[cols * rows * elemSize]; mat.get(0, 0, data); dataString = new String(base64.enprebase64(data));        }        else { throw new UnsupportedOperationException("unknown type");        }        obj.addProperty("data", dataString);        Gson gson = new Gson();        String json = gson.toJson(obj);        return json;    } else {        System.out.println("Mat not continuous.");    }    return "{}";}public static Mat matFromJson(String json){    JsonParser parser = new JsonParser();    JsonObject JsonObject = parser.parse(json).getAsJsonObject();    int rows = JsonObject.get("rows").getAsInt();    int cols = JsonObject.get("cols").getAsInt();    int type = JsonObject.get("type").getAsInt();    Mat mat = new Mat(rows, cols, type);    String dataString = JsonObject.get("data").getAsString();    if( type == CvType.CV_32S || type == CvType.CV_32SC2 || type == CvType.CV_32SC3 || type == CvType.CV_16S) {        int[] data = SerializationUtils.toIntArray(base64.deprebase64(dataString.getBytes()));        mat.put(0, 0, data);    }    else if( type == CvType.CV_32F || type == CvType.CV_32FC2) {        float[] data = SerializationUtils.toFloatArray(base64.deprebase64(dataString.getBytes()));        mat.put(0, 0, data);    }    else if( type == CvType.CV_64F || type == CvType.CV_64FC2) {        double[] data = SerializationUtils.toDoubleArray(base64.deprebase64(dataString.getBytes()));        mat.put(0, 0, data);    }    else if( type == CvType.CV_8U ) {        byte[] data = base64.deprebase64(dataString.getBytes());        mat.put(0, 0, data);    }    else {        throw new UnsupportedOperationException("unknown type");    }    return mat;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号