|
|
|
อยากทราบวิธีแก้ปัญหาดึงภาพมาจากกล้องแล้วรีโซลูชั่นไม่ตรงกันครับ Aforge.net c#.net |
|
|
|
|
|
|
|
คือผมกำลังเขียนโค้ดให้ตัวโปรแกรมสามารถแสดงภาพจากกล้องได้ครับ ทีนี้ตอนตั้งค่า resolution ของกล้องจากภายนอกมันเป็น1920*1080i 59.94(out 29.97) ครับ แต่ว่าภาพที่เรียกมาในคอมโดยใช้ Aforge มันได้ ออกมาเป็น 720*486 ครับ ล็อกอยู่อย่างนั้นไม่สามารถที่จะปรับเปลี่ยนได้ครับทำให้ภาพไม่แสดงผลที่จอคอม กลายเป็นภาพสีดำครับ
ทางนี้เป็นโค้ดบางส่วนที่ใช้งานกับโปรแกรมครับ เอามาเฉพาะส่วนแสดงผลภาพครับ
Code (C#)
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Accord.Video.FFMPEG;
using AForge.Video;
using AForge.Video.DirectShow;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using Microsoft.Win32;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using Microsoft.Kinect;
using System.Configuration;
using System.IO;
using System.ComponentModel;
using System.Threading;
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
StopCameraPtz();
Dispose();
Thread.Sleep(1000);
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
StartCameraPTz();
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
if (_recording)
{
if (_firstFrameTime != null)
{
_writer.WriteVideoFrame(eventArgs.Frame, DateTime.Now - _firstFrameTime.Value);
}
else
{
_writer.WriteVideoFrame(eventArgs.Frame);
_firstFrameTime = DateTime.Now;
Console.WriteLine(_firstFrameTime.Value);
}//
}
using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
{
Console.WriteLine (bitmap.Size.Width);
Console.WriteLine(bitmap.Size.Height);
bi = bitmap.ToBitmapImage();
bitmap.Dispose();
}
Console.WriteLine(bi.Width+"x"+bi.Height);
bi.Freeze(); // avoid cross thread operations and prevents leaks
Dispatcher.BeginInvoke(new ThreadStart(delegate { ImagePtz.Source = bi; }));
}
catch (Exception exc)
{
MessageBox.Show("Error on _videoSource_NewFrame:\n" + exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
StopCameraPtz();
}
Thread.Sleep(50);
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
StopCameraPtz();
}
private void btnStoprec_Click(object sender, RoutedEventArgs e)
{
_recording = false;
_writer.Close();
_writer.Dispose();
}
private void btnStartrec_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.FileName = "Video1";
dialog.DefaultExt = ".mp4";
dialog.AddExtension = true;
var dialogresult = dialog.ShowDialog();
if (dialogresult != true)
{
return;
}
_firstFrameTime = null;
_writer = new VideoFileWriter();
Console.WriteLine(bi.Format);
_writer.Open(dialog.FileName, (int)Math.Round(bi.Width, 0), (int)Math.Round(bi.Height, 0), 30, VideoCodec.H264 );
// _writer.Open(dialog.FileName, 1920 , 1080, 60, VideoCodec.H264, 100000);
_recording = true;
}
private void btnSavesnp_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.FileName = "Snapshot1";
dialog.DefaultExt = ".png";
var dialogresult = dialog.ShowDialog();
if (dialogresult != true)
{
return;
}
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bi));
using (var filestream = new FileStream(dialog.FileName, FileMode.Create))
{
encoder.Save(filestream);
}
}
public void Dispose()
{
if (_videoSource != null && _videoSource.IsRunning)
{
_videoSource.SignalToStop();
_videoSource.WaitForStop();
_videoSource = null;
}
_writer?.Dispose();
}
private void GetVideoDevices()
{
var devies = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo filterInfo in devies)
{
Console.WriteLine(filterInfo.Name);
VideoDevices.Add(filterInfo);
}
if (VideoDevices.Any())
{
CurrentDevice = VideoDevices[0];
}
else
{
MessageBox.Show("No video sources found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void StartCameraPTz()
{
if (CurrentDevice != null)
{
if (_videoSource != null && _videoSource.IsRunning)
{
_videoSource.SignalToStop();
_videoSource.WaitForStop();
}
videodevice = new VideoCaptureDevice(CurrentDevice.MonikerString);
Thread.Sleep(200);
_videoSource = videodevice;
_videoSource.NewFrame += video_NewFrame;
_videoSource.Start();
}
}
private void StopCameraPtz()
{
if (_videoSource != null && _videoSource.IsRunning)
{
_videoSource.SignalToStop();
_videoSource.NewFrame -= new NewFrameEventHandler(video_NewFrame);
_videoSource = null;
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
Tag : .NET, Class Library, C#, VS 2015 (.NET 4.x), VS 2017 (.NET 4.x)
|
|
|
|
|
|
Date :
2017-05-09 18:27:16 |
By :
sarun52 |
View :
1337 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|