通过 URL 从网络摄像机获取快照图像
Getting snapshot image from IP Camera via URL
我正在尝试从 Hikvision IP 摄像机 URL 获取照片,它有一个 URL 或多或少是这样形成的:
http://IPaddress:port#/Streaming/channels/1/picture
但是,正如您注意到的那样,该文件夹只是命名为图片,没有直接 link 到图像及其扩展名。当我使用 Picasso 库或只是普通的 HttpConnection 时,它无法获取图像/位图。我还能如何检索图像?当我在网络浏览器中输入 URL 时,它会完美加载并显示图片为 .jpeg 格式。
EDIT
每当我尝试使用 InputStream
和 Bitmap
访问快照图像时,我都会收到 FileNotFoundException
错误
达里奥指出的问题是身份验证。状态 401 被返回,这就是 FileNotFoundException
被抛出的原因。
had a lot of deprecated methods my Gradle couldnt even get some of the classes.
However I found another way with help from the following forums 1, 2 所以我结合另一个我找不到的身份验证页面并具有以下内容:
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'
我正在尝试从 Hikvision IP 摄像机 URL 获取照片,它有一个 URL 或多或少是这样形成的:
http://IPaddress:port#/Streaming/channels/1/picture
但是,正如您注意到的那样,该文件夹只是命名为图片,没有直接 link 到图像及其扩展名。当我使用 Picasso 库或只是普通的 HttpConnection 时,它无法获取图像/位图。我还能如何检索图像?当我在网络浏览器中输入 URL 时,它会完美加载并显示图片为 .jpeg 格式。
EDIT
每当我尝试使用 InputStream
和 Bitmap
访问快照图像时,我都会收到 FileNotFoundException
错误
达里奥指出的问题是身份验证。状态 401 被返回,这就是 FileNotFoundException
被抛出的原因。
However I found another way with help from the following forums 1, 2 所以我结合另一个我找不到的身份验证页面并具有以下内容:
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'