简体   繁体   中英

CameraCaptureDialog fails with “An unknown error occurred”

I'm working on a Windows Mobile 6.1 app written in C#. I have recently added the ability to take photos. The device is an Intermec CN50. This code works fine if I open my app and go directly to the bit that takes photos. However if I go through a few other screens and then open the photo bit it fails with the incredibly useful message "An unknown error occurred". There is no other useful information in the exception that I can see. The code fails on this line:

cameraResult = cameraDialog.ShowDialog();

Here's the stack trace:

at Microsoft.WindowsMobile.Forms.CameraCaptureDialog.LaunchCameraCaptureDialog(IntPtr ptrStruct)
at Microsoft.WindowsMobile.Forms.CameraCaptureDialog.ShowDialog()
at MicronetMobileUi.Controls.Camera.ShowDialog(Form owner, String& fileName)
at MicronetMobileUi.FieldService.JobImagesForm.LoadCameraScreen()
at MicronetMobileUi.FieldService.JobImagesForm.footerToolbar_ItemEntered(Object sender, EventArgs e)
at Resco.Controls.CommonControls.ToolbarControl.OnItemEntered()
at Resco.Controls.CommonControls.ToolbarControl.MouseClickUp(MouseEventArgs e)
at Resco.Controls.CommonControls.ToolbarControl.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at MicronetMobileUi.ApplicationManager.StartApp()
at MicronetMobileUi.Program.Main()

Here's the code:

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
var fileName = "temp.jpg";  
var pathAndFileName = Path.Combine(path, fileName);

var cameraDialog = new CameraCaptureDialog();
cameraDialog.Owner = this;
cameraDialog.InitialDirectory = path;
cameraDialog.DefaultFileName = fileName;
cameraDialog.Mode = CameraCaptureMode.Still;
cameraDialog.StillQuality = CameraCaptureStillQuality.Low;
cameraDialog.VideoTimeLimit = new TimeSpan(0, 0, 0);
cameraDialog.VideoTypes = CameraCaptureVideoTypes.Messaging;
cameraDialog.Resolution = new Size(640, 480); 

// open camera dialog to take a photo
DialogResult cameraResult;
try
{
    cameraResult = cameraDialog.ShowDialog();
}
catch (Exception ex)
{
    MessageBox.Show("Grrrr!!");
}       
finally
{
    cameraDialog.Dispose();
    cameraDialog = null;
}

if (cameraResult != DialogResult.OK)
    return; // no photo was taken

// save photo using this value: pathAndFileName ...

I guess this is something to do with running low on memory but I don't really know. So far I've tried:

1) Instatiating CameraCaptureDialog when the app first starts up so it can get some memory.

2) Instatiating CameraCaptureDialog and also calling ShowDialog() when the app first starts up.

3) Closing my connection to the local SQL CE database before instantiating CameraCaptureDialog (this was suggested on a forum).

4) Running GC.Collect() before instantiating (yes I know this is not normally a good idea).

5) Reducing cameraDialog.Resolution to just 320 by 240.

Nothing works. I spent ages adding this functionality to my app and now I find it only actually works sometimes. Windows Mobile can be seriously annoying. Help!!

While not on the CN50, I've hit a similar problem on a different Intermec device. It turned out that because the barcode scanner / decoder and the camera shared a common infrastructure that they must not be accessed at the same time.

Try commenting out or mocking out your barcode objects and see if you can then utilize the camera after navigating your app.

I think that I've found some kind of answer.

1) I added code to close my global connection to the local SQL CE database. This was suggested on a forum.

2) I also added code to dispose my bar code object as suggested by tcarvin. However I was still experiencing the error occasionally.

3) Then I noticed that the error only seems to come up when I am debugging with the device connected to Visual Studio. If I'm using it in the way a normal user would be using it then it seems OK. I certainly hope so anyway ...

UPDATE

Now the app has been deployed to customers it seems that the problem is still happening. Also, sometimes they are able to load the camera screen and take a photo but the Windows comes up with a message saying "error cannot load file" or something similar. It they restart the application then it works but I can't expect them to do that each time they want to take a photo. I will see if Intermec can help...

FURTHER UPDATE:

This happened a while ago so I'm a bit vague about the solution I found now. I've actually left that job and can't look at the code now. In the end I got a compononet (I think a DLL) from Intermec that solved the problem.

The reason why this is breaking is because of this line

cameraDialog.Resolution = new Size(640, 480);

Dont set resolution the camera does not support - it will fail when you do ShowDialog()

The supported resolutions can be found by running Picture & video App > menu > options> Resolutions drop down.

If you set one of those resolutions it works fine. (I am testing on CN51 and used 112x160).

-- The supported resolutions of a device is some registry setting somewhere and its different from device to device. If your code suppose to run on multiple devices the best way to handle this is to not set the resolution (It will use whatever was used last or is considered default for the device).

if you want the images to always be some size; Dont set resolution -> take the picture -> then post-process the image to something smaller. Using something like this some re-size example

Hope this helps someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM