admin管理员组

文章数量:1022434

I have created a .NET Web API project and am trying to display the Allied Vision camera feed on the browser.

I have added VimbaNET.dll to the references. When I run the application, it successfully retrieves the camera name, model, and ID. However, the camera feed does not display in the browser.

Here’s my implementation:

VimbaService.cs (Inside Models folder):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics; // Added for debugging
using AVT.VmbAPINET;

namespace testapplicationvimba.Models
{
    public class VimbaService
    {
        private readonly Vimba _vimba;

        public VimbaService()
        {
            _vimba = new Vimba();
        }

        public void Initialize()
        {
            _vimba.Startup();
            Console.WriteLine("Vimba Initialized"); // Debugging line
        }

        public List<CameraInfo> GetCamerasInfo()
        {
            var cameraList = new List<CameraInfo>();
            try
            {
                var cameras = _vimba.Cameras;

                if (cameras.Count == 0)
                {
                    Console.WriteLine("No cameras detected.");
                    return cameraList;
                }

                foreach (Camera camera in cameras)
                {
                    cameraList.Add(new CameraInfo
                    {
                        Id = camera.Id,
                        Model = camera.Model,
                        Name = camera.Name
                    });
                    Console.WriteLine($"Camera found: {camera.Name} ({camera.Model})"); // Debugging line
                }
            }
            catch (VimbaException ve)
            {
                Console.WriteLine("Vimba error: " + ve.Message); // Debugging line
            }

            return cameraList;
        }

        public void CaptureSingleImage(string cameraId, out byte[] imageBytes)
        {
            imageBytes = null; // Initialize the output to null

            try
            {
                var camera = _vimba.Cameras[cameraId];
                camera.Open(VmbAccessModeType.VmbAccessModeFull);

                Frame frame = new Frame(800000); // Buffer size, adjust as necessary
                camera.AcquireSingleImage(ref frame, 2000);

                Bitmap bitmap = new Bitmap((int)frame.Width, (int)frame.Height);
                frame.Fill(ref bitmap);

                using (var ms = new System.IO.MemoryStream())
                {
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageBytes = ms.ToArray();
                }

                frame = null;
                camera.Close();
            }
            catch (VimbaException ve)
            {
                Console.WriteLine("Error during image acquisition: " + ve.Message);
            }
            catch (TimeoutException te)
            {
                Console.WriteLine("Image acquisition timed out: " + te.Message);
            }
        }

        public void Shutdown()
        {
            _vimba.Shutdown();
        }
    }
}

HomeController.cs (Inside Controllers folder):

using System;
using System.Web.Mvc;
using testapplicationvimba.Models;

namespace testapplicationvimba.Controllers
{
    public class HomeController : Controller
    {
        private readonly VimbaService _vimbaService;

        public HomeController()
        {
            _vimbaService = new VimbaService();
        }

        public ActionResult Index()
        {
            _vimbaService.Initialize();
            var camerasInfo = _vimbaService.GetCamerasInfo();
            ViewBag.Cameras = camerasInfo;
            return View();
        }

        [HttpPost]
        public ActionResult Capture(string cameraId)
        {
            if (string.IsNullOrEmpty(cameraId))
            {
                ViewBag.ErrorMessage = "Please select a camera.";
                return RedirectToAction("Index");
            }

            byte[] imageBytes = null;

            _vimbaService.CaptureSingleImage(cameraId, out imageBytes);

            string base64Image = imageBytes != null ? Convert.ToBase64String(imageBytes) : string.Empty;

            ViewBag.CapturedImageBase64 = base64Image;

            var camerasInfo = _vimbaService.GetCamerasInfo();
            ViewBag.Cameras = camerasInfo;

            return View("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _vimbaService.Shutdown();
            base.Dispose(disposing);
        }
    }
}

Index.cshtml (Inside Views/Home folder):

<h2>Camera Dashboard</h2>

@if (ViewBag.Cameras != null && ViewBag.Cameras.Count > 0)
{
    <p>Cameras detected. Feed will start displaying automatically:</p>
    foreach (var camera in ViewBag.Cameras)
    {
        <div>
            <h4>@camera.Name (ID: @camera.Id) - Model: @camera.Model</h4>
            <div style="width: 300px; height: 200px; border: 1px solid black; margin-top: 20px;">
                <h3>Camera Feed:</h3>
                <img id="[email protected]" src="@Url.Action("CameraFeed", new { cameraId = camera.Id })" alt="Camera Feed" width="300" height="200" />
            </div>
        </div>

        <script type="text/javascript">
            setInterval(function () {
                var cameraId = '@camera.Id';
                document.getElementById("[email protected]").src = '@Url.Action("CameraFeed", new { cameraId = camera.Id })?' + new Date().getTime();
            }, 100);
        </script>
    }
}
else
{
    <p>No cameras detected.</p>
}

@if (!string.IsNullOrEmpty(ViewBag.SuccessMessage))
{
    <div style="color:green;">
        <p>@ViewBag.SuccessMessage</p>
    </div>
}

@if (!string.IsNullOrEmpty(ViewBag.ErrorMessage))
{
    <div style="color:red;">
        <p>@ViewBag.ErrorMessage</p>
    </div>
}

The problem is that I cannot get the camera feed to display in the browser. Instead, the feed box shows an error indicating that the image cannot be fetched.

What might I be missing? Any help would be appreciated.

I have created a .NET Web API project and am trying to display the Allied Vision camera feed on the browser.

I have added VimbaNET.dll to the references. When I run the application, it successfully retrieves the camera name, model, and ID. However, the camera feed does not display in the browser.

Here’s my implementation:

VimbaService.cs (Inside Models folder):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics; // Added for debugging
using AVT.VmbAPINET;

namespace testapplicationvimba.Models
{
    public class VimbaService
    {
        private readonly Vimba _vimba;

        public VimbaService()
        {
            _vimba = new Vimba();
        }

        public void Initialize()
        {
            _vimba.Startup();
            Console.WriteLine("Vimba Initialized"); // Debugging line
        }

        public List<CameraInfo> GetCamerasInfo()
        {
            var cameraList = new List<CameraInfo>();
            try
            {
                var cameras = _vimba.Cameras;

                if (cameras.Count == 0)
                {
                    Console.WriteLine("No cameras detected.");
                    return cameraList;
                }

                foreach (Camera camera in cameras)
                {
                    cameraList.Add(new CameraInfo
                    {
                        Id = camera.Id,
                        Model = camera.Model,
                        Name = camera.Name
                    });
                    Console.WriteLine($"Camera found: {camera.Name} ({camera.Model})"); // Debugging line
                }
            }
            catch (VimbaException ve)
            {
                Console.WriteLine("Vimba error: " + ve.Message); // Debugging line
            }

            return cameraList;
        }

        public void CaptureSingleImage(string cameraId, out byte[] imageBytes)
        {
            imageBytes = null; // Initialize the output to null

            try
            {
                var camera = _vimba.Cameras[cameraId];
                camera.Open(VmbAccessModeType.VmbAccessModeFull);

                Frame frame = new Frame(800000); // Buffer size, adjust as necessary
                camera.AcquireSingleImage(ref frame, 2000);

                Bitmap bitmap = new Bitmap((int)frame.Width, (int)frame.Height);
                frame.Fill(ref bitmap);

                using (var ms = new System.IO.MemoryStream())
                {
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageBytes = ms.ToArray();
                }

                frame = null;
                camera.Close();
            }
            catch (VimbaException ve)
            {
                Console.WriteLine("Error during image acquisition: " + ve.Message);
            }
            catch (TimeoutException te)
            {
                Console.WriteLine("Image acquisition timed out: " + te.Message);
            }
        }

        public void Shutdown()
        {
            _vimba.Shutdown();
        }
    }
}

HomeController.cs (Inside Controllers folder):

using System;
using System.Web.Mvc;
using testapplicationvimba.Models;

namespace testapplicationvimba.Controllers
{
    public class HomeController : Controller
    {
        private readonly VimbaService _vimbaService;

        public HomeController()
        {
            _vimbaService = new VimbaService();
        }

        public ActionResult Index()
        {
            _vimbaService.Initialize();
            var camerasInfo = _vimbaService.GetCamerasInfo();
            ViewBag.Cameras = camerasInfo;
            return View();
        }

        [HttpPost]
        public ActionResult Capture(string cameraId)
        {
            if (string.IsNullOrEmpty(cameraId))
            {
                ViewBag.ErrorMessage = "Please select a camera.";
                return RedirectToAction("Index");
            }

            byte[] imageBytes = null;

            _vimbaService.CaptureSingleImage(cameraId, out imageBytes);

            string base64Image = imageBytes != null ? Convert.ToBase64String(imageBytes) : string.Empty;

            ViewBag.CapturedImageBase64 = base64Image;

            var camerasInfo = _vimbaService.GetCamerasInfo();
            ViewBag.Cameras = camerasInfo;

            return View("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _vimbaService.Shutdown();
            base.Dispose(disposing);
        }
    }
}

Index.cshtml (Inside Views/Home folder):

<h2>Camera Dashboard</h2>

@if (ViewBag.Cameras != null && ViewBag.Cameras.Count > 0)
{
    <p>Cameras detected. Feed will start displaying automatically:</p>
    foreach (var camera in ViewBag.Cameras)
    {
        <div>
            <h4>@camera.Name (ID: @camera.Id) - Model: @camera.Model</h4>
            <div style="width: 300px; height: 200px; border: 1px solid black; margin-top: 20px;">
                <h3>Camera Feed:</h3>
                <img id="[email protected]" src="@Url.Action("CameraFeed", new { cameraId = camera.Id })" alt="Camera Feed" width="300" height="200" />
            </div>
        </div>

        <script type="text/javascript">
            setInterval(function () {
                var cameraId = '@camera.Id';
                document.getElementById("[email protected]").src = '@Url.Action("CameraFeed", new { cameraId = camera.Id })?' + new Date().getTime();
            }, 100);
        </script>
    }
}
else
{
    <p>No cameras detected.</p>
}

@if (!string.IsNullOrEmpty(ViewBag.SuccessMessage))
{
    <div style="color:green;">
        <p>@ViewBag.SuccessMessage</p>
    </div>
}

@if (!string.IsNullOrEmpty(ViewBag.ErrorMessage))
{
    <div style="color:red;">
        <p>@ViewBag.ErrorMessage</p>
    </div>
}

The problem is that I cannot get the camera feed to display in the browser. Instead, the feed box shows an error indicating that the image cannot be fetched.

What might I be missing? Any help would be appreciated.

本文标签: alliedvisionVimba SDK display allied vision camera in Aspnet web application (net framework)Stack Overflow