我通过python连接到IP Cam并在Java servlet中运行该脚本。然后我用Java加载每个图像并将其转换为带有base64编码的字节数组。最后,我通过AJAX通过JSON对象请求该数据。我没有收到任何错误,但图片也不会出现。
JavaScript的:
byte[] imgData = PythonRunner.grabPhotoBytes();//loads single image and convert to byte[]
JSONObject jsonResponse = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject img = new JSONObject();
String encodedImg = Base64.encodeBase64String(imgData);
img.put("imageToDisplay", encodedImg);
jsonArray.add(img);
jsonResponse.put("imgData", jsonArray);
out.println( jsonResponse.toJSONString() ); // send the WeatherData collection in JSON format
Java片段:
module utils {
export class Singleton {
private _initialized: boolean;
private _setSingleton(): void {
if (this._initialized) throw Error('Singleton is already initialized.');
this._initialized = true;
}
get setSingleton() { return this._setSingleton; }
}
}
答案 0 :(得分:0)
将图像作为Base64发送将是最糟糕的事情,您只需使用ImageIO
类获取图像并将其保存在服务器本地系统上,当然还可以作为名称(可能是随机的)。
首先,您在服务器系统上拥有如下保存的数据
/root/image_folks/trejon_house7222015-0342.jpeg
名称将是随机的或任何东西。
然后通过json响应将图像名称作为简单字符串响应客户端,客户端只需要/get-image
cgi / servlet来获取图像(如/get-image?id=trejon_house7222015-0342
)。
现在有一个简单的servlet,它加载服务器本地文件并将其响应给客户端,但不要忘记指定MIME类型