Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
This is the code that I've tried to write. I first tell the "LinkSprite JPEG Camera" to take a picture and then I read the JPEG file contents. There are no errors and the code is working, but the problem is the result comes was very slowly, I've seen "done" i.e. reached to FFD9 after minute. The message of the message box emerged after that much time. I want get the image which is stored in the camera buffer after sending the "take picture" command and to display it in the form. The camera is: LinkSprite JPEG Camera The Manual Mbed Example ( Test program ) My code is:
//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); }
}
Posted 24-Jan-13 10:20am
Edited 30-Jan-13 9:11am
v16
Comments
Sergey Alexandrovich Kryukov at 24-Jan-13 16:23pm
   
Do you really need the file content of JPEG on the file level (but why?), or you really need to just read the metadata and/or pixels (highly recommended)?
—SA
Sergey Alexandrovich Kryukov at 24-Jan-13 16:25pm
   
Also, 1) you absolutely don't need to read file on the file level to display in in PictureBox, 2) you absolutely don't need PictureBox to display image.
—SA
Sergey Alexandrovich Kryukov at 24-Jan-13 19:08pm
   
Who will explain you something if you don't explain why are you doing it and what you ultimately need to achieve?
I cannot believe you really need those bytes. What are you going to do with them? If you need the data about the images itself (all pixels), this is quite trivial, but you are not saying it. And showing the image is also quite trivial, but you keep talking about some bytes...
—SA
Sergey Alexandrovich Kryukov at 24-Jan-13 22:54pm
   
How did you improve it?! Added a code dump, thank you.
Are you going to explain why are you doing it and what do you want to achieve?
—SA
Sergey Alexandrovich Kryukov at 24-Jan-13 23:22pm
   
That's better.
 
First, you need to understand what this camera puts to the stream. Is it a standard USB Web camera, GIGE, or what?
 
Who told you that the camera sends anything in JPEG?
Where did you get this ReadJpegFileContents, why? .NET libraries already read JPEG, in either WPF or System.Drawing.
 
—SA
Zoltán Zörgő at 24-Jan-13 16:43pm
   
Forget the picturebox for a second, and save the desired portion of the content of the stream from to a file, and name it jpg. If a viewer can't open it, than you have the wrong data captured.
You might find these interesting:
http://stackoverflow.com/questions/11072306/taking-picture-with-linksprite-jpeg-color-camera
http://www.codeproject.com/Questions/492245/Communicatepluswithpluslinkspritepluscamera
lovely phantom at 24-Jan-13 17:43pm
   
Thanks :)))))))))))
valery possoz at 24-Jan-13 18:18pm
   
Hello,
 
Here is a C example:
http://www.linksprite.com/upload/file/1286079786.txt
 
As said before save the data in a file and bob's ur uncle done.
Sergey Alexandrovich Kryukov at 24-Jan-13 19:10pm
   
Why?!
—SA
Marcus Kramer at 30-Jan-13 15:11pm
   
Please don't remove the content of the question once answered.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Verify that the baud rate and/or serial timeouts are reasonable.
 
Dump a log file with a timestamp for each serial port event.
 
Find the spot where nothing happens for extended periods of time and fix it.
 
Done.
  Permalink  
Comments
lovely phantom at 25-Jan-13 12:46pm
   
Thanks :))))))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100