Click here to Skip to main content
16,016,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i am using this code given below i having some issues.
1. when i am running this code it runs as i want capturing 3 photos one by one and doing some processing on the captured image in the function in CanonCaptureEvent()
2. when i click start button second time right after the first time process it creating some issue that the images are not capturing and not storing. it happens after every second time start button click
3.and somehow if images are captured then 3rd image is being captured corrupted and now able to do process even in 1st image too.
>> and while 2nd or 3rd time (or after 2nd click every time)clicking image it gives me error message that : SDK Error code: DEVICE_BUSY (81)

here the functions that are in the MainWindow.cs are
Start_Button_Click()
CanonCaptureEvent()

here the functions that are in the CameraSettings.cs are
public async Task TakePhoto(int photoIndex = 1)
MainCamera_DownloadReady()

here the functions that are in the Camera.cs are(canon sdk class)
public void TakePhoto()
public void DownloadFile(DownloadInfo Info, string directory)


What I have tried:

C#
public async void Start_Button_Click(object sender, EventArgs e)
        {
            try
            {

             if(iscamera == "canon camera")
                {
                    try
                    {

                    
                        curDateTime = DateTime.Now.ToString("dd-MM-yyyy(HH-mm-ss)");
                        var defaultDelay = 1500;
                        MessageBox.Show("camset :" + curDateTime);
                        Todaysdate = curpath;
                    
    if (!Directory.Exists(Todaysdate))
                        {
                            Directory.CreateDirectory(Todaysdate);
                        }

                        cameraSettings.directory = Todaysdate;

                        await cameraSettings.TakePhoto();

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


public async Task TakePhoto(int photoIndex = 1)
        {
            it = photoIndex;
          

            var defaultDelay = 3000;

  await Task.Delay(defaultDelay);

            if (it == 1)
            {
                await Task.Delay(defaultDelay);
            }
            else if (it == 2)
            {
                
                await Task.Delay(defaultDelay); // red light default delay
                                                       }
            else if (it == 3)
            {
                await Task.Delay(defaultDelay); // white light delay
                                           
            }

            MainCamera.TakePhoto();
        }


 public void TakePhoto()
        {
            CheckState();
            SendCommand(CameraCommand.TakePicture);
        }


 private void MainCamera_DownloadReady(Camera sender, DownloadInfo Info)
        {
            if(Info.IsRAW)
            {
                return;
            }
            try
            {
               
                string path = directory;
                Info.FileName = it.ToString() + ".jpg";
                sender.DownloadFile(Info, path);
                Invoke((Action)delegate { MainProgressBar.Value = 0; });

                if (it >= 3)
                {
                    var table = this.Parent.CanonCaptureEvent(null);
                }
                else if (it < 3)
                {
                    this.TakePhoto(++it);
                }
                
            }
            catch (Exception ex) { ReportError(ex.Message, false); }
        }

public void DownloadFile(DownloadInfo Info, string directory)
        {
            CheckState();
            if (Info == null) throw new ArgumentNullException(nameof(Info));
            if (directory == null || string.IsNullOrEmpty(directory.Trim())) directory = ".";

            string currentFile = Path.Combine(directory, Info.FileName);
            if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
            DownloadToFile(Info, currentFile);
        }


public async Task<DataTable> CanonCaptureEvent(object obj)
        {
         
            if (mr.Read("savedpath") != null)
            {
                string Savedpath = mr.Read("savedpath");
                Todaysdate = Savedpath;
            }


         
            if (!Directory.Exists(Todaysdate))
            {
                Directory.CreateDirectory(Todaysdate);

            }

         
            await Task.Delay(defaultDelay);
         
            try
            {

                imgInput = new Image<Bgr, byte>(Todaysdate + "\\" + "1.jpg");   //white image process

                       // some process i am doing
                    }

                }

         
                RefreshGridView();
                return table;

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


        }
Posted
Updated 16-Jun-23 22:46pm
v2
Comments
Richard MacCutchan 17-Jun-23 4:08am    
There is something approaching 400 lines of code there that no one here has ever seen before. You need to remove all the lines that are not related to the problem, and indicate exactly where you are performing the actions that are causing problems.
sahil ajmeri 2022 17-Jun-23 4:47am    
okay i have removed the lines that are not related to the problem i am facing.

1 solution

I have little idea what is happening in detail, but wonder if there are some places where the return values should not be checked.
For example here:
C#
public void TakePhoto()
{
   CheckState();
   SendCommand(CameraCommand.TakePicture);
}
 
Share this answer
 
Comments
sahil ajmeri 2022 17-Jun-23 3:17am    
so what should i do or change in my code ?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900