C# 从相机寄存器获取图像

C# 从相机寄存器获取图像,c#,image,camera,C#,Image,Camera,我正在为我的大学开发一个简单的成像软件,在从相机获取图像时遇到了一个恼人的问题。 这里有一个用于相机远地点Alta U57库的.dll COM库和文档:如果图像准备就绪,有两种可能的方法从相机获取图像: 使用ICamera2 camera.image,返回 返回一个2D SAFEARRAY,类型为长4字节/秒 元素或整数每个元素2字节,其中包含 图像数据。数据类型为LONG或INTEGER 返回的由的关联属性控制 塔隆 使用ICamera2.GetImageint pImageBuffer,其描

我正在为我的大学开发一个简单的成像软件,在从相机获取图像时遇到了一个恼人的问题。 这里有一个用于相机远地点Alta U57库的.dll COM库和文档:如果图像准备就绪,有两种可能的方法从相机获取图像:

使用ICamera2 camera.image,返回

返回一个2D SAFEARRAY,类型为长4字节/秒 元素或整数每个元素2字节,其中包含 图像数据。数据类型为LONG或INTEGER 返回的由的关联属性控制 塔隆

使用ICamera2.GetImageint pImageBuffer,其描述如下:

返回指向位于的16位无符号短数据的指针 在记忆中。图像数据区域应由 在调用此方法之前应用程序

在使用第二种方法时,我非常困惑,因为int!=int*, 我真的不知道如何将指针传递到16位USHORT。 我获取图像的简化方法如下所示:

        public unsafe uint[] getImage(int width, int height)
    {
        // Allocating array of image size (width * height)
        // where pixel is size of unsigned int (4 BYTES)
        // possible values: 0 to 4,294,967,295 
        uint[] pixels = new uint[width * height];

        // Gets pointer to allocated array and fixes it, 
        // so that it won't be moved by Garbage Collector
        fixed (uint* ptr = pixels)
        {
            camera.GetImage(ptr);
        }
        return pixels;
    }
有人能解释吗?在过去的10个小时里,我真的很累了,也许我遗漏了什么:

好的,看起来GetImage函数希望您为图像像素分配内存,并传入一个指向分配内存的指针。你也有责任在完成后释放内存

我认为您需要做的唯一更改是将指针转换为long-GetImage函数需要一个long,而不是32位平台上的指针,指针将是32位;在64位平台上,它应该是64位的

// Gets pointer to allocated array and fixes it, 
// so that it won't be moved by Garbage Collector
fixed (uint* ptr = pixels)
{
    long ptrValue = (long) ptr;

    camera.GetImage(ptrValue);
}

这个cast不是非常好,但是它是安全的,因为比特数对于两种平台都是足够的。我怀疑您已经必须以32位或64位平台为目标。

文档中说它需要Uint16。我觉得你应该这样做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("Apogee.dll.dll")]
        extern static int CreateInstance(out IntPtr ICamera2Ptr);

        [DllImport("Apogee.dll.dll")]
        extern static void GetImage(ref IntPtr pImageBuffer);

        static void Main(string[] args)
        {
            IntPtr ICamera2Ptr;
            int results = CreateInstance(out ICamera2Ptr);

        }
        static UInt16[] getImage(int width, int height)
        {
            // Allocating array of image size (width * height)
            // where pixel is size of unsigned int (4 BYTES)
            // possible values: 0 to 4,294,967,295 

            // Allocate memory and calculate a byte count 
            //unsigned short *pBuffer = new unsigned short[ ImgXSize * ImgYSize ];
            //unsigned long ImgSizeBytes = ImgXSize * ImgYSize * 2; 

            UInt16[] pixels = new UInt16[width * height];
            IntPtr unmanaged_pPixels = Marshal.AllocHGlobal(Marshal.SizeOf(pixels));

            //// External operations ApogeeCamera->ExternalShutter = true; 
            //ApogeeCamera->ExternalIoReadout = false; 
            //ApogeeCamera->IoPortAssignment = 0x08; 
            // Even though the exposure time will not be used, still call Expose 
            //ApogeeCamera->Expose( 0.001, true ); 
            // Check camera status to make sure image data is ready 
            //while (ApogeeCamera->ImagingStatus != Apn_Status_ImageReady ); 
            // Get the image data from the camera ApogeeCamera->GetImage( (long)pBuffer );


            GetImage(ref unmanaged_pPixels);
            pixels = (UInt16[])Marshal.PtrToStructure(unmanaged_pPixels, typeof(UInt16[]));
            return pixels;


        }
    }
}
​

你有什么问题?你能不能拍到照片?您还应该发布这些函数的实际签名,而不仅仅是文档。@xxbbcc的问题是,我的编译器拒绝camera.GetImageptr,因为对于函数的签名,只有.dll库,其中包含接口:[DispId6]void GetImageint pImageBuffer;[DispId7]如果使用类型库查看器打开DLL会发生什么情况@如果我错了,请纠正我,但我使用Lib Viewer类型和路径查看了它:APOGEELib->coclass Camera2->ICamera2->Methods->GetImage我得到了:[id0x00000006,helpstringmethod GetImage]HRESULT GetImage[in]long pImageBuffer;好的-我添加了一个答案来投射你的指针。耶,它工作了!无论如何,GetImage接受ptr的int值,所以我将long改为int。编译器对此没有问题。非常感谢:@Badylu我很高兴它能工作,但你说的“ptr的int值”是什么意思?你的目标是什么平台?32位还是64位?我的目标是32位平台,我的VS2013确定GetImage接受int值,虽然文档中很长:@Badylu好的,只要你瞄准32位平台,你就没事。只要你。。。