привет после того как этот код считал ссылку он ее открывает и при возврати обратно в…
- User
привет после того как этот код считал ссылку он ее открывает и при возврати обратно в приложение у меня уже не работает считывание перепиши код так чтобы при возвращении он считывал снова qr public class QRCodeScanner : MonoBehaviour { [SerializeField] private string lastResult;
private WebCamTexture camTexture; private Rect screenRect; private Color32[] cameraColorData; private int width, height; private CancellationTokenSource cts = new CancellationTokenSource(); private bool startEncoding; private bool startDecoding; private BarcodeWriter writer; private Result result; private BarcodeReader barcodeReader = new BarcodeReader { AutoRotate = false, Options = new ZXing.Common.DecodingOptions { TryHarder = false } }; public void ActiveScanner() { SetupWebcamTexture(); PlayWebcamTexture(); cameraColorData = new Color32[width * height]; screenRect = new Rect(0, 0, Screen.width, Screen.height); // Pass the token to the cancelable operation - decoding and encoding. ThreadPool.QueueUserWorkItem(new WaitCallback(GetCodeFromImageData), cts.Token); ThreadPool.QueueUserWorkItem(new WaitCallback(EncodeNewFromLastResult), cts.Token); } private void Update() { if (!startDecoding) { camTexture.GetPixels32(cameraColorData); startDecoding = !startDecoding; } } private void OnGUI() { if (camTexture != null) { // Сохраняем текущую матрицу GUI, чтобы восстановить её позже Matrix4x4 matrixBackup = GUI.matrix; // Получаем угол поворота и проверяем на зеркальное отображение float rotationAngle = -camTexture.videoRotationAngle; // Добавляем 180 градусов для дополнительного поворота rotationAngle += 180; bool isVerticallyMirrored = camTexture.videoVerticallyMirrored; Rect rect = screenRect; if (isVerticallyMirrored) { // Если изображение отображается зеркально, корректируем положение rect rect.y = Screen.height - rect.y - rect.height; } // Высчитываем центр поворота Vector2 pivotPoint = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f); // Применяем поворот вокруг центра поворота GUIUtility.RotateAroundPivot(rotationAngle, pivotPoint); // Рисуем текстуру с учётом поворота и зеркального отображения GUI.DrawTexture(rect, camTexture, ScaleMode.ScaleToFit); // Восстанавливаем матрицу GUI GUI.matrix = matrixBackup; try { if (result != null) { // QR-код успешно считан Debug.Log("Decoded QR: " + result.Text); // Останавливаем сканирование и камеру camTexture.Stop(); var resultURL = result.Text.Substring(0, result.Text.Length - 1); // Переходим по ссылке, если это нужно result = null; //ActiveScanner(); Application.OpenURL(resultURL); } } catch (System.Exception ex) { Debug.LogWarning(ex.Message); } } } private void OnDestroy() { camTexture.Stop(); cts.Cancel(); // Cancellation should have happened, so call Dispose. cts.Dispose(); } private void SetupWebcamTexture() { camTexture = new WebCamTexture(); camTexture.requestedHeight = Screen.height; camTexture.requestedWidth = Screen.width; } private void PlayWebcamTexture() { if (camTexture != null) { camTexture.Play(); width = camTexture.width; height = camTexture.height; } } private void EncodeNewFromLastResult(object obj) { CancellationToken token = (CancellationToken)obj; while (!token.IsCancellationRequested) { if (startEncoding && lastResult != null) { startEncoding = !startEncoding; } } } private void GetCodeFromImageData(object obj) { CancellationToken token = (CancellationToken)obj; while (!token.IsCancellationRequested) { // decode the current frame if (startDecoding && cameraColorData != null) { result = barcodeReader.Decode(cameraColorData, width, height); if (result != null) { lastResult = result.Text + " " + result.BarcodeFormat; startEncoding = true; } startDecoding = !startDecoding; } } } - Assistant
- User
можешь полностью написать весь код
- Assistant