0

这是我尝试编写的代码。我首先告诉“LinkSprite JPEG 相机”拍照,然后读取 JPEG 文件内容。

没有错误,代码正在运行,但问题是结果来得很慢,我看到“完成”,即在 2 多分钟后到达 FFD9。过了许久,消息框的消息才出现。

我想在发送“拍照”命令后获取存储在相机缓冲区中的图像并以表格形式显示。

我的代码是:

// public static byte[] ReceivedMsg = new byte[256];
// public static int factor = 0x0000;
// public static UInt16 M = 0;
// public static byte MH = (byte)((M >> 8) & 0xff);
// public static byte ML = (byte)((M >> 0) & 0xff);

private void TakeSnap_Click(object sender, EventArgs e)
{
    // First set the size of the image //
    byte[] sendCmd = new byte[9] { 0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, 0x11 };

    try
    {
        if (!(CamPort.IsOpen)) CamPort.Open(); // If the port is closed!,Open it again //
        CamPort.Write(sendCmd, 0, 9);
        Thread.Sleep(100);// Wait for the camera //
        for (int i = 0; i < 5; i++)
        {
            ReceivedMsg[i] = (byte)(CamPort.ReadByte()); // Store the returned msg //
        }
        if (ReceivedMsg[0].ToString("x") == "76" && ReceivedMsg[1].ToString("x") ==    "0" && ReceivedMsg[2].ToString("x") == "31" && ReceivedMsg[3].ToString("x") == "0" && ReceivedMsg[4].ToString("x") == "0")
        {
            try
            {
                CamPort.DiscardOutBuffer();
                CamPort.DiscardInBuffer();
                byte[] TakePicCmd = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x00 };
                CamPort.Write(TakePicCmd, 0, 5);
                Thread.Sleep(100);// Wait for the camera //
                for (int i = 0; i < 5; i++)
                {
                    ReceivedMsg[i] = (byte)(CamPort.ReadByte());
                }
                if (ReceivedMsg[0].ToString("x") == "76" && ReceivedMsg[1].ToString("x") == "0" && ReceivedMsg[2].ToString("x") == "36" && ReceivedMsg[3].ToString("x") == "0" && ReceivedMsg[4].ToString("x") == "0")
                { 
                    try
                    {
                        CamPort.DiscardOutBuffer();
                        CamPort.DiscardInBuffer();
                        byte[] ReadSizeCommand = new byte[5] { 0x56, 0x00, 0x34, 0x01, 0x00 };
                        CamPort.Write(ReadSizeCommand, 0, 5);
                        Thread.Sleep(100);// Wait for the camera //
                        for (int i = 0; i < 9; i++)
                        {
                            ReceivedMsg[i] = (byte)(CamPort.ReadByte());
                        }
                        if (ReceivedMsg[0].ToString("x") == "76" && ReceivedMsg[1].ToString("x") == "0" && ReceivedMsg[2].ToString("x") == "34" && ReceivedMsg[3].ToString("x") == "0" && ReceivedMsg[4].ToString("x") == "4" && ReceivedMsg[5].ToString("x") == "0" && ReceivedMsg[6].ToString("x") == "0")
                        {   
                            int ImageSize=(((ReceivedMsg[7] & 0x00ff) << 8) | (ReceivedMsg[8] & 0x00ff));
                            try
                            {
                                CamPort.DiscardOutBuffer();
                                CamPort.DiscardInBuffer();
                                byte[] body = new byte[32];
                                byte[] header = new byte[5];
                                bool EndFlag = false;    
                                while (!EndFlag)
                                {
                                    ReadJpegFileCommand();
                                    Thread.Sleep(25);// Wait for the camera //
                                    for (int i = 0; i < 5; i++) header[i] = (byte)CamPort.ReadByte();
                                    ////// If the first 5 bytes matching [76 00 32 00 00] then read the body and display the image //////
                                    if (((int)header[0] == 0x76) && (header[1] == 0x00) && (header[2] == 0x32) && (header[3] == 0x00) && (header[4] == 0x00))
                                    {
                                        for (int i = 0; i < 32; i++)
                                            body[i] = (byte)CamPort.ReadByte();

                                        for (int i = 1; i < body.Length; i++)// check if reached to the last two bytes(FF D9) of the body //
                                        {
                                            if ((body[i - 1] == 0xFF) && (body[i - 0] == 0xD9))
                                            {
                                                EndFlag = true;
                                                MessageBox.Show("done");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Unexpted Returned value "); // the first 5 bytes does not match the header 
                                    }
                                }
                            }

                            catch (System.Exception ex) { MessageBox.Show(ex.Message); }

                        }
                        else { MessageBox.Show("Reset the camera and start again0 !!"); }
                    }
                    catch (System.Exception ex) { MessageBox.Show(ex.Message); }
                }
                else { MessageBox.Show("Reset the camera and start again1 !!"); }
              }
            catch (System.Exception ex) { MessageBox.Show(ex.Message); }
        }
        else { MessageBox.Show("Reset the camera and try again2 !!"); }
    }
    catch (System.Exception ex) { MessageBox.Show(ex.Message); }
}

//------------------------------------------------------------------------------------//

public void ReadJpegFileCommand()
{
    try
    {
        MH = (byte)(factor / 0x100);
        ML = (byte)(factor % 0x100);
        byte[] ReadImageCommand = new byte[16] { 0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, MH, ML, 0x00, 0x00, 0x00, 0x20, 0x00, 0x0a };
        CamPort.Write(ReadImageCommand, 0, 16);
        factor += 0x20;
    }

    catch (System.Exception ex) { MessageBox.Show(ex.Message); }
}

摄像头是:LinkSprite JPEG 摄像头

相机手册

Mbed 示例(测试程序)

如何“快速”到达 FFD9,然后如何将存储在数据数组中的字节显示为picturebox1已添加到表单中的图像?

CamPort是串口控件的名称。

4

1 回答 1

0

你的串口运行的波特率是多少?串行端口传输数据的速度不是很快,即使在您的情况下以 115200 波特的最高速度也是如此。即使这样,如果波特率设置为高,或者两端不匹配,这样的串行通信也可能容易丢弃随机字符。

至于让它显示在图片框中,您需要将字节数组转换为某种类型的 Image 类对象。如果您的输入数组正确且没有丢失任何字符,这并不难。有很多关于如何做到这一点的教程。

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/a20fcf5f-9d32-4d32-85d0-c8dd4d978e15

http://forums.codeguru.com/showthread.php?496495-Manually-created-byte-array-to-picture-box.

于 2013-01-25T17:00:04.757 回答