如何在Flutter中从IP摄像机下载图像

时间:2019-12-26 01:25:43

标签: flutter dart ip-camera onvif

当我向IP摄像机(Onvif)发送http请求时

    "<GetSnapshotUri xmlns=\"http://www.onvif.org/ver20/media/wsdl\">" +
          "<ProfileToken>PROFILE_000</ProfileToken></GetSnapshotUri>";

我收到包含网址的响应:

....
 <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')小部件

查看

我遇到错误

════════ 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);

在获得正确答案后进行了更新。我可以看到创建Digest键所需的参数,但是由于无法通过dart代码返回Response对象 Response header in http sniffer that I can't get by Dart code

1 个答案:

答案 0 :(得分:0)

不使用图片下载器怎么办

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>");