如何在 Flutter 中从网络摄像机下载图像
How to download image from ip camera in Flutter
当我向 IP 摄像头 (Onvif) 发送 http 请求时
"<GetSnapshotUri xmlns=\"http://www.onvif.org/ver20/media/wsdl\">" +
"<ProfileToken>PROFILE_000</ProfileToken></GetSnapshotUri>";
我收到包含 Url 的响应:
....
<s:Body><trt:GetSnapshotUriResponse><trt:MediaUri>
<tt:Uri>http://192.168.1.102:13237/snapshot.cgi</tt:Uri>
<tt:InvalidAfterConnect>false</tt:InvalidAfterConnect>
<tt:InvalidAfterReboot>false</tt:InvalidAfterReboot><tt:Timeout>PT5S</tt:Timeout></trt:MediaUri>
</trt:GetSnapshotUriResponse></s:Body></s:Envelope>
我尝试使用 image_downloader 插件,但没有用。
我可以在浏览器中看到它 (Chrome) 但我无法通过 Image.network('http://192.168.1.102:13237/snapshot.cgi') Widget
查看它
我收到错误
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following NetworkImageLoadException was thrown resolving an image codec:
HTTP request failed, statusCode: 401, http://192.168.1.102:13237/snapshot.cgi
When the exception was thrown, this was the stack:
#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
<asynchronous suspension>
#1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
#2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
#3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
...
Image provider: NetworkImage("http://192.168.1.102:13237/snapshot.cgi", scale: 1.0)
Image key: NetworkImage("http://192.168.1.102:13237/snapshot.cgi", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
我在 Java
上找到了答案
URL url = new URL("http://webacm-ip-adr:8084/snapshot.cgi");
InputStream input = url.openStream();
String jpg = "sample.jpg";
FileOutputStream output = new FileOutputStream(jpg);
IOUtils.copy(input, output);
得到正确答案后更新。我可以看到创建摘要键所需的参数,但由于我无法 return 通过飞镖代码响应对象
Response header in http sniffer that I can't get by Dart code
不如使用图片下载器怎么样
import 'package:http/http.dart' as http;
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';
Future<File> _downloadFile(String authEncoded,String url, String param) async {
final response = await http.post(url,
headers: {
HttpHeaders.authorizationHeader: 'Basic ' + authEncoded
// Do same with your authentication requirement
},
body: param,
);
response.headers.forEach((k,v) => print (k+" : "+v));
//Your response headers here
if(response.statusCode==200){
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/image.jpg');
await file.writeAsBytes(response.bodyBytes);
return file;
} else {
print("Error : " + response.statusCode.toString());
}
}
现在打电话
File file = await _downloadFile("http://192.168.1.102:13237/snapshot.cgi", "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver20/media/wsdl\"><ProfileToken>PROFILE_000</ProfileToken></GetSnapshotUri>");
当我向 IP 摄像头 (Onvif) 发送 http 请求时
"<GetSnapshotUri xmlns=\"http://www.onvif.org/ver20/media/wsdl\">" +
"<ProfileToken>PROFILE_000</ProfileToken></GetSnapshotUri>";
我收到包含 Url 的响应:
....
<s:Body><trt:GetSnapshotUriResponse><trt:MediaUri>
<tt:Uri>http://192.168.1.102:13237/snapshot.cgi</tt:Uri>
<tt:InvalidAfterConnect>false</tt:InvalidAfterConnect>
<tt:InvalidAfterReboot>false</tt:InvalidAfterReboot><tt:Timeout>PT5S</tt:Timeout></trt:MediaUri>
</trt:GetSnapshotUriResponse></s:Body></s:Envelope>
我尝试使用 image_downloader 插件,但没有用。 我可以在浏览器中看到它 (Chrome) 但我无法通过 Image.network('http://192.168.1.102:13237/snapshot.cgi') Widget
查看它我收到错误
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following NetworkImageLoadException was thrown resolving an image codec:
HTTP request failed, statusCode: 401, http://192.168.1.102:13237/snapshot.cgi
When the exception was thrown, this was the stack:
#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
<asynchronous suspension>
#1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
#2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
#3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
...
Image provider: NetworkImage("http://192.168.1.102:13237/snapshot.cgi", scale: 1.0)
Image key: NetworkImage("http://192.168.1.102:13237/snapshot.cgi", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
我在 Java
上找到了答案URL url = new URL("http://webacm-ip-adr:8084/snapshot.cgi");
InputStream input = url.openStream();
String jpg = "sample.jpg";
FileOutputStream output = new FileOutputStream(jpg);
IOUtils.copy(input, output);
得到正确答案后更新。我可以看到创建摘要键所需的参数,但由于我无法 return 通过飞镖代码响应对象 Response header in http sniffer that I can't get by Dart code
不如使用图片下载器怎么样
import 'package:http/http.dart' as http;
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';
Future<File> _downloadFile(String authEncoded,String url, String param) async {
final response = await http.post(url,
headers: {
HttpHeaders.authorizationHeader: 'Basic ' + authEncoded
// Do same with your authentication requirement
},
body: param,
);
response.headers.forEach((k,v) => print (k+" : "+v));
//Your response headers here
if(response.statusCode==200){
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/image.jpg');
await file.writeAsBytes(response.bodyBytes);
return file;
} else {
print("Error : " + response.statusCode.toString());
}
}
现在打电话
File file = await _downloadFile("http://192.168.1.102:13237/snapshot.cgi", "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver20/media/wsdl\"><ProfileToken>PROFILE_000</ProfileToken></GetSnapshotUri>");