【问题标题】:Send OpenCV image in JSON from Python to Android将 JSON 格式的 OpenCV 图像从 Python 发送到 Android
【发布时间】:2016-04-23 18:27:26
【问题描述】:

我有一个 Python 服务器,它使用 OpenCV 在图像上绘制一些东西。之后,我想通过之前打开的socketserver 将生成的图像发送到 Android 设备。对于图像,我还想发送一个字符串,因此我构建了一个要发送的 JSON 对象。 Android 应用程序接收到的对象包含这两个元素,但图像是 OpenCV Mat 对象的字符串表示形式。 我找不到任何关于如何在 byte 数组中转换这种图像表示的方法,以便我可以在 Android 应用程序中重建图像并显示它。

如何正确发送图像,然后在 Android 应用中进行转换?

这是我用于构建图像、JSON 并发送它的 Python 脚本:

imgou = numpy.zeros((numpy.size(mat_image, 0), numpy.size(mat_image, 1), 3), numpy.uint8)
cv2.drawContours(imgou, cont, idx, (0,255,0), 3)

print "build json"
outjson = {}
outjson['img'] = numpy.array_str(imgou)
outjson['leaf'] = leaf
json_data = json.dumps(outjson)

self.request.sendall(json_data)

这是处理 JSON 的 Android 应用的代码:

StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
...
JSONObject json = new JSONObject(sb.toString());
String leaf_name = json.getString("leaf");
String mat_string = json.getString("img");

byte[] raw_data = mat_string.getBytes("utf-8");
Mat mat_img = new Mat();
mat_img.put(0, 0, raw_data);

此时我应该在mat_img 中准备好将Mat 对象写入文件然后显示。

【问题讨论】:

    标签: android python json opencv


    【解决方案1】:

    从您的服务器中,您可以将图像发送到字节数组中 现在在您的 android 应用程序中,您可以接收字节数组并将其转换为位图,然后转换为 Mat

    byte[] bitmapdata = Base64.decode("your byte string", Base64.DEFAULT);
    //here the data coming from server is assumed in Base64
    //if you are sending bytes in plain string you can directly convert it to byte array
        Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0,        bitmapdata.length);
    
    //android OpenCv function
    org.opencv.android.Utils.bitmapToMat(bitmap,mat);
    

    【讨论】:

    • 我的问题更多是关于:如何将图像转换为字节数组并将其打包成 JSON?否则,这将是您提出的完美方式
    【解决方案2】:

    基于answer of Avinash,我设法将图像打包到 JSON 对象中,然后从 Android 应用程序中检索它。由于OpenCV提供了BitmapMat转换的方法,发送图像而不是Mat对象更方便。

    这里是 Python 服务器的工作代码:

    img_file = open("image1.png", "r")
    
    # read the image file
    data = img_file.read()        
    
    # build JSON object
    outjson = {}
    outjson['img'] = data.encode('base64')   # data has to be encoded base64 and decode back in the Android app base64 as well
    outjson['leaf'] = "leaf"
    json_data = json.dumps(outjson)
    
    # close file pointer and send data
    img_file.close()
    self.request.sendall(json_data)
    

    Android 应用的工作代码如下:

    // Read JSON from socket inputstream and rebuild the string
    InputStream in = so.getInputStream();
    if (in == null) return false;
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    sb = new StringBuilder();
    
    String line;
    while ((line = br.readLine()) != null) {
       sb.append(line);
    }
    
    ...
    // Convert string in JSON and retrieve raw data
    JSONObject json = new JSONObject(sb.toString());
    String leaf_name = json.getString("leaf");
    String mat_string = json.getString("img");
    
    // Convert raw byte data of image into Bitmap image
    FileOutputStream byteBuffer = new FileOutputStream();
    
    byte[] raw_data = Base64.decode(mat_string, Base64.DEFAULT);
    Bitmap img = BitmapFactory.decodeByteArray(raw_data, 0, raw_data.length);
    img.compress(Bitmap.CompressFormat.PNG, 100, byteBuffer);
    

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2018-09-01
      • 2018-01-26
      相关资源
      最近更新 更多