繁体   English   中英

将JSON中的OpenCV图像从Python发送到Android

[英]Send OpenCV image in JSON from Python to Android

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

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

这是我的Python脚本,用于构建图像,JSON并将其发送:

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)

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

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对象,可以将其写入文件中,然后显示出来。

基于Avinash答案 ,我设法将图像打包到JSON对象中,然后从Android应用程序中检索它。 由于OpenCV提供了一种将Bitmap转换为Mat的方法,因此发送图像而不是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);

干杯!

形成服务器后,您可以将图像发送到字节数组中,现在在您的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM