title: How to restore base64 encoded opencv java matrix to C++ cv::Mat?
body: <p>I'm extracting ORB descriptors of an image in android using opencv which is then sent to a C++ server using a socket. To send it via the socket I'm encoding the opencv mat of descriptors to base64 using the below java code.</p>
  
  <pre><code>    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();    
  
                 byte[] data = new byte[cols * rows * elemSize];
  
                 mat.get(0, 0, data);
  
                 obj.addProperty("rows", mat.rows()); 
                 obj.addProperty("cols", mat.cols()); 
                 obj.addProperty("type", mat.type());
  
                 // We cannot set binary data to a json object, so:
                 // Encoding data byte array to Base64.
                 String dataString = new String(Base64.encode(data, Base64.DEFAULT));
  
                 obj.addProperty("data", dataString);            
  
                 Gson gson = new Gson();
                 String json = gson.toJson(obj);
  
                 return json;
             } else {
                 Log.e(TAG, "Mat not continuous.");
             }
  
  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();
  
      String dataString = JsonObject.get("data").getAsString();       
      byte[] data = Base64.decode(dataString.getBytes(), Base64.DEFAULT); 
  
      Mat mat = new Mat(rows, cols, type);
      mat.put(0, 0, data);
  
      return mat;
  }
  </code></pre>
  
  <p>The output from matToJson() looks like below:</p>
  
  <pre><code>{"rows":26,"cols":32,"type":0,"data":"aSbt5TQabzJ9qv7s3ymQchrEfSyp8OWO5v8nkG6oUiFAMLFkAExBEGCgJggSF0AyAFRoCAlgsADJ\niAUwQCBGImQwuWSATEEQYrAiCNY3QToBVGgYAWDxAMmIRTTAYEYibDS4YqltU5o7v68Y3/9JOpHX\n/RAaZvsiy991f8JzxnNJJ33lNB5vMn3ofkzfP5BwlkR/LKnw4c7n/yOQTuhTcUAwOWAATFEQYCAg\nCBYXADCAFFAIAWCwAMGJADBAAEYyYCAxQABNQQBgpCQIFhdAMgBUQAgAYLAAwYgBMEACRiJgMLFk\nAEhBAGKkJAgWF0AyAVRIGABgsADJiAUwwABGImwwuXW4bVGaM/+mGJ+3QTqV3v0YK3b5As/fNX3A\n4EZzQTCRZABMRQBgoiIIVjdAMgBUaBgBYLEAyagFMMBgRiJJPj33NJ57N33ofky/O5Bwhi5+DKm4\n4e7n/zKQSojDKXw0vHW4b1Gas7+2GNc3QzuF33wYKGT5AMvbdTTA8EZzQCCRZABIAQBgoCIIFhdA\nMgBUaBgBYKAAiYAFMEBgRiBpLv33NI9pN30p/1z/KZBwgqZ8DLn44S7G/zKQSoDDKUAhEUAQSEEA\nYAAgCBYXADAQVEBAASCgACGIABBAAEYgdDC8dahsURLiv7YYlzdAOofWfRiLRfsAy8t1NODgRnNA\nIJFEAEgAAGCgIAgWE0AyAFRIAABgoACJgAUwQCBGIEAikEQgCAACYKggaBITwDIAVEwAAGCAAImA\nJTBAIAIhYDC5dGhtURJzvzYYl7dAOofWfRgLePMA34t1cODARnNAIBFAEEgAAGAAIAgSEwAwEFxA\nYEEggAwhiAAQQAJGIHEy1WQoDEEQ4KiyTJcTwDIExGwMiWHRAMCpIbBogEIhcapb+iytSRfzGbNY\nt4PQeEuubByba8Ix2vkwWOiIwil4oLhwie1RjqK3rhiXtUIagdbsEBpr4wDb5XV54EDGYnAwuXSI\nbVEb4rekGBe3QDoB1mwYGm/6EtundT3g4MZicaBb+qytSZb+GbFct8PYeKGObBybbdox2ekyePiB\nwij1oBl6rKxJm/IRoVi300g4gYpsHDtr2jPZqzR42MDCIA\u003d\u003d\n"}
  </code></pre>
  
  <p>the encoded base64 string in the "data" key is the Matrix data.</p>
  
  <p>The above json string is sent to the server via socket.</p>
  
  <p>Now, I need to convert the "data" value of the json string back to cv::Mat in C++, so how do i get back the matrix from the above base64 string ?</p>