我目前正在开发一个应用程序,它必须将IP摄像机的图像存储到文件中。我不想存储视频流。我只想每百毫秒拍摄一张图像。
我使用特定网址从制造商提供的IP摄像头获取JPEG图像。我在BitmapImage中保存了从IP摄像头获取的图像,但是当我想将BitmapImage保存到文件时,它会在我指定的目录中保存一个空的.jpg文件。
我不明白的是,当我想在Image控件中显示BitmapImage时,它会显示实际图像,但是当我想保存它时,它会保存一个空文件。
有人可以告诉我如何解决这个问题,或者可以找到解决方案吗?
我已经尝试过JPEGBitmapEncoder,但没有成功。
以下是我目前使用的代码:
private void captureButton_Click(object sender,RoutedEventArgs e) { string photosLocation,newPhotos;
photosLocation = Directory.GetCurrentDirectory() + "\\IP Cam Photos";
newPhotos = Guid.NewGuid().ToString();
Directory.CreateDirectory(photosLocation);
camLocation = photosLocation + "\\" + newPhotos;
Directory.CreateDirectory(camLocation);
captureStatusLabel.Content = "Photo capturing started!";
for (int i = 0; i < 10; i++)
{
camImage = new BitmapImage();
camImage.BeginInit();
camImage.UriSource = new Uri("http://172.16.4.14/image?res=full&x0=0&y0=0&x1=1600&y1=1200&quality=12&doublescan=0", UriKind.RelativeOrAbsolute);
while (camImage.IsDownloading)
{
captureStatusLabel.Content = "Capture Busy";
}
camImage.DownloadCompleted += camImage_DownloadCompleted;
camImage.EndInit();
captureStatusLabel.Content = "Photo " + (i + 1) + " captured!";
}
}
void camImage_DownloadCompleted(object sender, EventArgs e)
{
a++;
camImgLoc = camLocation + "\\Image " + a + ".jpg";
FileStream camFiles = new FileStream(camImgLoc, FileMode.Create);
JpegBitmapEncoder camEncoder = new JpegBitmapEncoder();
MessageBox.Show(camImage.IsDownloading.ToString() + "\n" + camEncoder.ToString());
camEncoder.Frames.Add(BitmapFrame.Create(camImage));
camEncoder.Save(camFiles);
camFiles.Close();
}
答案 0 :(得分:0)
我找到了答案。我只需要在请求中使用正确的URL,然后它就能完美运行。
public void ReceiveImage(string imageURL)
{
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageURL);
imageRequest.Method = "GET";
HttpWebResponse imageResponse = (HttpWebResponse)imageRequest.GetResponse();
using (Stream inputStream = imageResponse.GetResponseStream())
{
using (Stream outputStream = File.OpenWrite("SonyCamera.jpg"))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
outputStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}
}
}