简体   繁体   中英

PhotoCamera API: how to properly dispose it?

I'm using the PhotoCamera API to build a QR code scanning page (using ZXing). However, this page is only a small part of the app, and therefore it is not always shown. Thus, the application navigates between this page and some other pages with common controls.

The issue is that sometimes, after a scan, the whole application is slowed down to 30fps instead of 60fps with no real reason. I suspect that the camera is still running in the background, and the frame sync locks the app to 30fps, which is my issue: how to properly dispose of a page that uses the PhotoCamera API?

My XAML:

<Grid Background="Black">
    <ProgressBar x:Name="PBar" IsIndeterminate="True" VerticalAlignment="Center" />

    <Rectangle x:Name="ScanRect">
        <Rectangle.Fill>
            <VideoBrush x:Name="ScanVideoBrush" />
        </Rectangle.Fill>
    </Rectangle>
</Grid>

My C# to stop the scan process:

    private void StopScan() {
        if (analysisTimer != null) {
            analysisTimer.Stop();
            analysisTimer = null;
        }

        if (focusTimer != null) {
            focusTimer.Stop();
            focusTimer = null;
        }

        if (camera != null) {
            camera.Dispose();
            camera.Initialized -= OnCameraInitialized;
            camera = null;
        }

        // Following two lines are a try to dispose stuff 
        // as much as possible, but the app still lags
        // sometimes after a scan...

        ScanVideoBrush.SetSource(new MediaElement());
        ScanRect.Fill = new SolidColorBrush(Colors.Black);
    }

Note: I'm testing the app on a Lumia 920.

Calling cam.Dispose(); should dispose the image source stream and free resources used by the Camera object, so that's fine.

Are you sure you are releasing memory, ie unsubscribing from the PhotoCamera class Events?

When is your StopScan method called? A good practice could be to call it in OnNavigatingFrom of the PhoneApplicationPage .

Here is the code sample from MSDN that disposes the PhotoCamera:

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
    if (cam != null)
    {
        // Dispose camera to minimize power consumption and to expedite shutdown. 
        cam.Dispose();

        // Release memory, ensure garbage collection. 
        cam.Initialized -= cam_Initialized;
        cam.CaptureCompleted -= cam_CaptureCompleted;
        cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
        cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;
        cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
        CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;
        CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
        CameraButtons.ShutterKeyReleased -= OnButtonRelease;
    }
} 

Source

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