我的应用程序中实现了条形码扫描程序。 该应用程序始终以60 FPS运行。
在捕捉(相机激活和扫描)期间,帧速率降至30 fps。
捕获后,当我处理我初始化的所有内容时, 整个应用程序继续使用30 FPS。
如何在摄像机任务后恢复到60 FPS?
private DispatcherTimer _timer;
//Fields I need for scanning (ZXing)
private PhotoCameraLuminanceSource _luminance;
private MultiFormatReader _reader;
//The cam itself
private PhotoCamera _camera;
这是我在凸轮初始化时调用的内容。
private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
{
try
{
_camera.FlashMode = FlashMode.Off;
int width = Convert.ToInt32(_camera.PreviewResolution.Width);
int height = Convert.ToInt32(_camera.PreviewResolution.Height);
_luminance = new PhotoCameraLuminanceSource(width, height);
_reader = new MultiFormatReader();
_reader.Hints = hints;
Dispatcher.BeginInvoke(() =>
{
try
{
brushTransform.Rotation = _camera.Orientation;
if (_timer != null) _timer.Start();
}
catch (Exception ex) { Debug.WriteLine(ex.Message); }
});
}
catch {}
}
这是计时器的滴答方法
private void Scan()
{
_camera.Focus();
try
{
_camera.GetPreviewBufferY(_luminance.PreviewBufferY);
var binarizer = new HybridBinarizer(_luminance);
var binBitmap = new BinaryBitmap(binarizer);
var result = _reader.decode(binBitmap);
if (result != null)
{
FileManagement.saveFile("barcode", result.Text, null);
goBack();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
这就是我最终回到搜索页面的方式
private void goBack()
{
VibrateController.Default.Start(TimeSpan.FromMilliseconds(40));
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
customDispose();
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
customDispose();
}
private void customDispose()
{
_timer.Stop();
_timer.Tick -= (o, arg) => Scan();
_camera.Initialized -= OnPhotoCameraInitialized;
_camera.Dispose();
_camera = null;
_reader = null;
_luminance = null;
_timer = null;
GC.Collect();
}