通过URL从IP Camera获取快照图像

时间:2015-11-15 12:44:57

标签: android android-image android-bitmap

我正在尝试从海康威视网络摄像机网址获取一张图片,该网址的网址或多或少是这样形成的:
http://IPaddress:port#/Streaming/channels/1/picture
但是,您注意到该文件夹​​只是命名图片,没有直接链接到图像及其扩展名。当我使用Picasso库或只是普通的HttpConnection时,它无法获取图像/位图。我怎么能检索图像?当我在网络浏览器中输入URL时,它会完美地加载并显示图片为.jpeg格式。

编辑
每当我尝试使用if($num1==$num2) { $message = "Incorrect input"; echo "<script type='text/javascript'>alert('$message');window.location = ('my_contr/first') </script>"; } InputStream访问快照图片时,我都会收到Bitmap错误

1 个答案:

答案 0 :(得分:1)

Dario指出的问题是身份验证。状态401正在返回,这就是FileNotFoundException被抛出的原因。 answer he gave me有很多弃用的方法,我的Gradle甚至无法获得某些类。
但是我在以下论坛12的帮助下找到了另一种方法,因此我将其他页面与我无法找到的身份验证结合起来并具有以下内容:

boolean isSaved = false;
         try {
             HttpClient client = new DefaultHttpClient();
             HttpGet request = new HttpGet();
             request.setURI(new URI(AppConst.IMAGE_URL));

             UsernamePasswordCredentials credentials =
                     new UsernamePasswordCredentials(AppConst.USERNAME, AppConst.PASSWORD);
             BasicScheme scheme = new BasicScheme();
             Header authorizationHeader = scheme.authenticate(credentials, request);
             request.addHeader(authorizationHeader);

             HttpResponse response = client.execute(request);
             Log.d("responseType",response!=null? response.getEntity().getContentType().toString():"emptyType");
             Log.d("responseCode", response != null ? String.valueOf(response.getStatusLine().getStatusCode()) : "emptyCode");
             if((response.getStatusLine().getStatusCode()==200) && (response.getEntity()!=null)){//status OK and not empty
                 //save stuff
                 HttpEntity entity = response.getEntity();
                 if (entity != null) {
                     InputStream instream = entity.getContent();
                     String path = APP_DIR+System.currentTimeMillis()+".jpg";
                     FileOutputStream output = new FileOutputStream(path);
                     int bufferSize = 1024;
                     byte[] buffer = new byte[bufferSize];
                     int len = 0;
                     while ((len = instream.read(buffer)) != -1) {
                         output.write(buffer, 0, len);
                     }
                     output.close();
                     Log.d("Snapshot Filename: ", path);
                     isSaved = true;
                 }

             }else{
                 isSaved= false;
             }
         }catch (Exception ex){
             Log.e("saveImageError",ex!=null? ex.getMessage():"error");
         }



必须在android部分的gradle中添加以下行:
useLibrary 'org.apache.http.legacy'