2

我目前有一个 python 套接字服务器,它使用 opencv 从网络摄像头捕获。我已经使用 image.tostring(); 转换了捕获的图像。

我正在尝试将此图像发送到 android 应用程序并在 Web 视图中打开它。我可以打开文本字符串,但无法将其恢复为图像。

这是我可以在 Web 视图中显示任何内容的唯一方法:

webv.loadData(html, "text/html", "utf-8");

数据正在传输到 android 设备,所以我知道套接字工作正常,我只是无法弄清楚翻译。

有什么建议么?

根据以下评论进行编辑:

好的。所以我现在在发送之前将字符串转换为 python 中的 base64。

我尝试使用 bitmapfactory 将图像添加到 ImageView 使用:

byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

但我收到错误 SkImageDecoder:: Factory returned null

我还尝试了一个带有以下内容的 webview,这给了我一个损坏的图像图标

String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "utf-8");

这些给了我一个空白的网络视图

String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "iamge/jpeg", "base64");

String html = "<img src=\"data:image/jpeg;base64,"+result+"\" />";
webv.loadData(html, "text/html", "base64");

webv.loadData(result, "image/jpeg","base64");

似乎问题出在我的python代码中,因为导出到文本文件的base64字符串无法重新转换为图像,它“包含错误”适用的代码片段:

img = cv.QueryFrame(cam)
imgstr = base64.b64encode(img.tostring())

conn = s.accept()
conn.send(imgstr)
4

0 回答 0