<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ZXing.Mobile
{
    public partial class ZXingScannerControl : UserControl, IDisposable
    {
        public ZXingScannerControl() : base()
        {
            InitializeComponent();
        }

        public void StartScanning(Action<ZXing.Result> scanCallback, MobileBarcodeScanningOptions options = null)
        {
            ScanCallback = scanCallback;
            ScanningOptions = options ?? MobileBarcodeScanningOptions.Default;

            this.topText.Text = TopText;
            this.bottomText.Text = BottomText;

            if (UseCustomOverlay && CustomOverlay != null)
            {
                gridCustomOverlay.Children.Clear();
                gridCustomOverlay.Children.Add(CustomOverlay);

                gridCustomOverlay.Visibility = Visibility.Visible;
                gridDefaultOverlay.Visibility = Visibility.Collapsed;
            }
            else
            {
                gridCustomOverlay.Visibility = Visibility.Collapsed;
                gridDefaultOverlay.Visibility = Visibility.Visible;
            }

            // Initialize a new instance of SimpleCameraReader with Auto-Focus mode on
            if (_reader == null)
            {
                _reader = new SimpleCameraReader(options);
                _reader.ScanInterval = ScanningOptions.DelayBetweenAnalyzingFrames;

                // We need to set the VideoBrush we're going to display the preview feed on
                // IMPORTANT that it gets set before Camera initializes
                _previewVideo.SetSource(_reader.Camera);

                // The reader throws an event when a result is available 
                _reader.DecodingCompleted += (o, r) => DisplayResult(r);

                // The reader throws an event when the camera is initialized and ready to use
                _reader.CameraInitialized += ReaderOnCameraInitialized;
            }
        }

        public Action<Result> ScanCallback { get; set; }
        public MobileBarcodeScanningOptions ScanningOptions { get; set; }
        public MobileBarcodeScannerBase Scanner { get; set; }
        public UIElement CustomOverlay { get; set; }
        public string TopText { get; set; }
        public string BottomText { get; set; }
        public bool UseCustomOverlay { get; set; }
        public bool ContinuousScanning { get; set; }

        public Result LastScanResult { get; set; }

        SimpleCameraReader _reader;
        
        public bool IsTorchOn
        {
            get { return _reader.Camera.FlashMode == FlashMode.On; }
        }

        public void Torch(bool on)
        {
            _reader.Camera.FlashMode = on ? FlashMode.On : FlashMode.Auto;
        }

        public void ToggleTorch()
        {
            _reader.Camera.FlashMode = _reader.Camera.FlashMode == FlashMode.On ? FlashMode.Auto : FlashMode.On;
        }

        public void AutoFocus()
        {
            _reader.Camera.Focus();
        }

        public void StopScanning()
        {
            if (UseCustomOverlay && CustomOverlay != null)
                gridCustomOverlay.Children.Remove(CustomOverlay);

            BlackoutVideoBrush();
            
            _reader.Stop();
			_reader = null;
        }

        private void BlackoutVideoBrush()
        {
        	_previewVideo.SetSource(new MediaElement());
        }
        
        public void Cancel()
        {
            LastScanResult = null;
<commit_msg>WindowsPhone: Scanner control shouldn't access camera directly For things like flash and autofocus, the camera reader has built in checks and protection which weren't being used.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ZXing.Mobile
{
    public partial class ZXingScannerControl : UserControl, IDisposable
    {
        public ZXingScannerControl() : base()
        {
            InitializeComponent();
        }

        public void StartScanning(Action<ZXing.Result> scanCallback, MobileBarcodeScanningOptions options = null)
        {
            ScanCallback = scanCallback;
            ScanningOptions = options ?? MobileBarcodeScanningOptions.Default;

            this.topText.Text = TopText;
            this.bottomText.Text = BottomText;

            if (UseCustomOverlay && CustomOverlay != null)
            {
                gridCustomOverlay.Children.Clear();
                gridCustomOverlay.Children.Add(CustomOverlay);

                gridCustomOverlay.Visibility = Visibility.Visible;
                gridDefaultOverlay.Visibility = Visibility.Collapsed;
            }
            else
            {
                gridCustomOverlay.Visibility = Visibility.Collapsed;
                gridDefaultOverlay.Visibility = Visibility.Visible;
            }

            // Initialize a new instance of SimpleCameraReader with Auto-Focus mode on
            if (_reader == null)
            {
                _reader = new SimpleCameraReader(options);
                _reader.ScanInterval = ScanningOptions.DelayBetweenAnalyzingFrames;

                // We need to set the VideoBrush we're going to display the preview feed on
                // IMPORTANT that it gets set before Camera initializes
                _previewVideo.SetSource(_reader.Camera);

                // The reader throws an event when a result is available 
                _reader.DecodingCompleted += (o, r) => DisplayResult(r);

                // The reader throws an event when the camera is initialized and ready to use
                _reader.CameraInitialized += ReaderOnCameraInitialized;
            }
        }

        public Action<Result> ScanCallback { get; set; }
        public MobileBarcodeScanningOptions ScanningOptions { get; set; }
        public MobileBarcodeScannerBase Scanner { get; set; }
        public UIElement CustomOverlay { get; set; }
        public string TopText { get; set; }
        public string BottomText { get; set; }
        public bool UseCustomOverlay { get; set; }
        public bool ContinuousScanning { get; set; }

        public Result LastScanResult { get; set; }

        SimpleCameraReader _reader;
        
        public bool IsTorchOn
        {
            get { return _reader.FlashMode == FlashMode.On; }
        }

        public void Torch(bool on)
        {
            _reader.FlashMode = FlashMode.On;
        }

        public void ToggleTorch()
        {
            _reader.FlashMode = _reader.FlashMode == FlashMode.Off ? FlashMode.On : FlashMode.Off;
        }

        public void AutoFocus()
        {
            _reader.Focus();
        }

        public void StopScanning()
        {
            if (UseCustomOverlay && CustomOverlay != null)
                gridCustomOverlay.Children.Remove(CustomOverlay);

            BlackoutVideoBrush();
            
            _reader.Stop();
			_reader = null;
        }

        private void BlackoutVideoBrush()
        {
        	_previewVideo.SetSource(new MediaElement());
        }
        
        public void Cancel()
        {
            LastScanResult = null;

{'fraction_non_alphanumeric': 0.072681371246346, 'fraction_numerical': 0.0, 'mean_word_length': 1.9390862944162437, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6344581', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1822, 'n_words': 483}