AVT Firewrap.net相机即使没有编码也会放大

时间:2017-05-03 10:55:10

标签: c# camera

对于我的实习,我必须使用prosillica相机创建照片,我一直在寻找能够使用相机的不同api。现在我发现了一个有效的,我写了一个前任实习生写的代码(“猜测”他说)。我得到的图像,但他们都真的放大了。在官方的Firegrab程序中,图片看起来很好,根本没有放大。您可以查看图片here。我写的连接相机的代码如下:

Ctrl = FireWrap_CtrlCenter.GetInstance();
            Ctrl.OnFrameReady += OnFrameReady;
            Result = Ctrl.FGInitModule();
            if (Result == enFireWrapResult.E_NOERROR)
            {
                Result = InfoContainer.FGGetNodeList();
                var NodeCnt = InfoContainer.Size();

                InfoContainer.GetAt(NodeInfo, 0);
                Result = Cam.Connect(NodeInfo.Guid);

                cCamera.Items.Add(Cam.DeviceAll);

                if (Result == enFireWrapResult.E_NOERROR)
                {
                    Cam.m_Guid = NodeInfo.Guid;
                }

                if (Result == enFireWrapResult.E_NOERROR)
                {
                    Result = Cam.SetParameter(enFGParameter.E_IMAGEFORMAT,
                        (((uint)enFGResolution.E_RES_SCALABLE << 16) |
                        ((uint)enColorMode.E_CCOLORMODE_Y8 << 8) |
                        0));
                }

                if (Result == enFireWrapResult.E_NOERROR)
                    Result = Cam.OpenCapture();

                // Print device settings
                Result = Cam.GetParameter(enFGParameter.E_XSIZE, ref XSize);
                Result = Cam.GetParameter(enFGParameter.E_YSIZE, ref YSize);
                width = XSize;
                height = YSize;
                // Start camera
                if (Result == enFireWrapResult.E_NOERROR)
                {
                    Result = Cam.StartDevice();
                }
            }

当我连接相机时,我也告诉它立即开始录制。我在相机打开时获得的帧在OnFrameReady中处理,我使用以下代码:

Debug.WriteLine("OnFrameReady is called");
            FGEventArgs args = (FGEventArgs)__p2;
            FGFrame Frame;
            Guid.High = args.High;
            Guid.Low = args.Low;

            if (Guid.Low == Cam.m_Guid.Low)
            {
                Result = Cam.GetFrame(Frame, 0);
                // Process frame, skip FrameStart notification
                if (Result == enFireWrapResult.E_NOERROR & Frame.Length > 0)
                {
                    byte[] data = new byte[Frame.Length];

                    // Access to frame data
                    if (Frame.CloneData(data))
                    {
                        //DisplayImage(data.Clone());
                        SaveImageFromByteArray(data);
                        // Here you can start your image processsing logic on data
                        string debug = String.Format("[{6}] Frame #{0} length:{1}byte [ {2} {3} {4} {5} ... ]",
                            Frame.Id, Frame.Length, data[0], data[1], data[2], data[3], Cam.m_Guid.Low);
                        Debug.WriteLine(debug);
                    }
                    // Return frame to module as fast as posible after this the Frame is not valid 
                    Result = Cam.PutFrame(Frame);
                }
            }

所以在这个函数中我得到了帧并将其放在byte []中,然后我调用函数SaveImageFromByteArray();我把byte []放在一个列表中。所以我可以稍后访问我的所有图片以保存它们。 SaveImageFromByteArray的代码如下:

 public void SaveImageFromByteArray(byte[] byteArray)
        {
            try
            {
                //bytearray size determined
                byte[] data = new byte[width * height * 4];
                int o = 0;
                //bytearray size filled
                for (int io = 0; io < width * height; io++)
                {
                    byte value = byteArray[io];
                    data[o++] = value;
                    data[o++] = value;
                    data[o++] = value;
                    data[o++] = 0;
                }
                bytearrayList.Add(data);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

我完成了对所有帧的重新编码后,单击“保存”,停止相机,然后调用以下函数将其保存到位图文件中:

public void SaveData()
        {
            try
            {
                foreach (byte[] data1 in bytearrayList)
                {
                    byte[] data = Save(data1);
                    lock (this)
                    {
                        unsafe
                        {
                            fixed (byte* ptr = data)
                            {
                                try
                                {
                                    using (image = new Bitmap((int) width, (int) height, (int) width * 4,
                                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb, new IntPtr(ptr)))
                                    {
                                        image.Save(path + nextpicture + ".bmp", ImageFormat.Bmp);
                                        Debug.WriteLine("Image saved at " + path + nextpicture + ".bmp");
                                        nextpicture++;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.Write(ex.ToString());
                                }
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {

                Debug.Write(ex.ToString());
            }
        }

上述函数中调用的save函数编写如下:

private byte[] Save(byte[] data1)
        {
            //bytearray size determined
            byte[] data = new byte[width * height * 4];
            int o = 0;
            //bytearray size filled
            for (int io = 0; io < width * height; io++)
            {
                byte value = data1[io];
                data[o++] = value;
                data[o++] = value;
                data[o++] = value;
                data[o++] = 0;
            }
            return data;
        }

我认为当我们连接到相机并执行这行代码时会发生缩放问题:

 if (Result == enFireWrapResult.E_NOERROR)
            {
                Result = Cam.SetParameter(enFGParameter.E_IMAGEFORMAT,
                    (((uint)enFGResolution.E_RES_SCALABLE << 16) |
                    ((uint)enColorMode.E_CCOLORMODE_Y8 << 8)| 
                    0));
            }

但问题是没有关于Firewrap.net或其api的文档。即使我们尝试将16编辑为15,相机也不会启动

1 个答案:

答案 0 :(得分:0)

发现问题了!像素被水平拉伸成4个像素,这是因为我们做了两次:

byte[] data = new byte[width * height * 4];
            int o = 0;
            //bytearray size filled
            for (int io = 0; io < width * height; io++)
            {
                byte value = byteArray[io];
                data[o++] = value;
                data[o++] = value;
                data[o++] = value;
                data[o++] = 0;
            }