From 60b528b664a5cf8086476553ac9d28128601451e Mon Sep 17 00:00:00 2001 From: SUCCpred <45946205+SUCCpred@users.noreply.github.com> Date: Thu, 26 May 2022 14:39:00 +0300 Subject: [PATCH] =?UTF-8?q?Revert=20"Revert=20"=D0=A7=D1=82=D0=BE-=D1=82?= =?UTF-8?q?=D0=BE=20=D0=BF=D0=BE=D1=85=D0=BE=D0=B6=D0=B5=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D1=81=D0=BA=D0=B0=D0=BD=D0=BD=D0=B5=D1=80=D0=B0=20QR-?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=BE=D0=B2""?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 50f3e653aed9480863a4a2ecb83349c100776aa3. --- Assets/Scenes/QRScaner.unity | 146 ++++++++++++++++++++++++++++++++--- Assets/Scripts/QRScaner.cs | 129 +++++++++++++++++++++++-------- QCAR/somedata16 | 2 +- 3 files changed, 233 insertions(+), 44 deletions(-) diff --git a/Assets/Scenes/QRScaner.unity b/Assets/Scenes/QRScaner.unity index 459c78c..91a507b 100644 --- a/Assets/Scenes/QRScaner.unity +++ b/Assets/Scenes/QRScaner.unity @@ -291,7 +291,7 @@ Camera: near clip plane: 0.3 far clip plane: 1000 field of view: 60 - orthographic: 0 + orthographic: 1 orthographic size: 5 m_Depth: -1 m_CullingMask: @@ -356,6 +356,82 @@ MonoBehaviour: m_RequiresDepthTexture: 0 m_RequiresColorTexture: 0 m_Version: 2 +--- !u!1 &521245986 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 521245987} + - component: {fileID: 521245989} + - component: {fileID: 521245988} + m_Layer: 5 + m_Name: scannerZone + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &521245987 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 521245986} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 969415614} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 1000, y: 1000} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &521245988 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 521245986} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 0, b: 0, a: 0.41960785} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 0} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &521245989 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 521245986} + m_CullTransparentMesh: 1 --- !u!1 &845688367 GameObject: m_ObjectHideFlags: 0 @@ -367,6 +443,7 @@ GameObject: - component: {fileID: 845688368} - component: {fileID: 845688370} - component: {fileID: 845688369} + - component: {fileID: 845688371} m_Layer: 5 m_Name: cameraPreview m_TagString: Untagged @@ -390,7 +467,7 @@ RectTransform: m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 1, y: 1} + m_AnchorMax: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 0.5} @@ -429,6 +506,20 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 845688367} m_CullTransparentMesh: 1 +--- !u!114 &845688371 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 845688367} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 86710e43de46f6f4bac7c8e50813a599, type: 3} + m_Name: + m_EditorClassIdentifier: + m_AspectMode: 4 + m_AspectRatio: 0.45454547 --- !u!1 &969415610 GameObject: m_ObjectHideFlags: 0 @@ -441,7 +532,6 @@ GameObject: - component: {fileID: 969415613} - component: {fileID: 969415612} - component: {fileID: 969415611} - - component: {fileID: 969415615} m_Layer: 5 m_Name: Canvas m_TagString: Untagged @@ -478,10 +568,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3} m_Name: m_EditorClassIdentifier: - m_UiScaleMode: 0 + m_UiScaleMode: 1 m_ReferencePixelsPerUnit: 100 m_ScaleFactor: 1 - m_ReferenceResolution: {x: 800, y: 600} + m_ReferenceResolution: {x: 1920, y: 1080} m_ScreenMatchMode: 0 m_MatchWidthOrHeight: 0 m_PhysicalUnit: 3 @@ -498,8 +588,8 @@ Canvas: m_GameObject: {fileID: 969415610} m_Enabled: 1 serializedVersion: 3 - m_RenderMode: 0 - m_Camera: {fileID: 0} + m_RenderMode: 1 + m_Camera: {fileID: 495848847} m_PlaneDistance: 100 m_PixelPerfect: 0 m_ReceivesEvents: 1 @@ -524,6 +614,7 @@ RectTransform: m_Children: - {fileID: 1750083181} - {fileID: 845688368} + - {fileID: 521245987} m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -532,19 +623,52 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} ---- !u!114 &969415615 +--- !u!1 &1189421136 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1189421138} + - component: {fileID: 1189421137} + m_Layer: 0 + m_Name: QRCodeReader + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1189421137 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 969415610} + m_GameObject: {fileID: 1189421136} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 8b72e5734fef979418aa3d532a254b13, type: 3} m_Name: m_EditorClassIdentifier: - rawimage: {fileID: 845688369} + _rawImage: {fileID: 845688369} + _aspectRatioFitter: {fileID: 845688371} +--- !u!4 &1189421138 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1189421136} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1580677550 GameObject: m_ObjectHideFlags: 0 @@ -581,7 +705,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 1, y: 0.86300004} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: -187, y: 8.4001465} + m_AnchoredPosition: {x: -187, y: 8.400391} m_SizeDelta: {x: 177.9712, y: 2} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1580677552 diff --git a/Assets/Scripts/QRScaner.cs b/Assets/Scripts/QRScaner.cs index 6d2ba46..2055559 100644 --- a/Assets/Scripts/QRScaner.cs +++ b/Assets/Scripts/QRScaner.cs @@ -1,71 +1,136 @@ using ZXing; using UnityEngine; using System; +using UnityEngine.UI; using Assets.CodeBase.Infrastructure; using Assets.CodeBase.Infrastructure.StateMachine.States; class QRScaner: MonoBehaviour { [SerializeField] - public UnityEngine.UI.RawImage rawimage; + public RawImage _rawImage; + [SerializeField] + public AspectRatioFitter _aspectRatioFitter; + //[SerializeField] + //public RectTransform _scannerZone; - private WebCamTexture camTexture; - private Rect screenRect; - private bool detected = false; + private WebCamTexture _camTexture; + private bool _isCamAvaible = false; void Start() { + SetUpCamera(); + StartCoroutine(YieldScanForQR()); + } + + void Update() + { + // Update camera render + if (_isCamAvaible) + { + _aspectRatioFitter.aspectRatio = (float)_camTexture.width / (float)_camTexture.height; + //_rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, -_camTexture.videoRotationAngle); + } + } + private System.Collections.IEnumerator YieldTryToPlayCamera() + { + while (true) + { + yield return new WaitForSecondsRealtime(.1f); + _camTexture.Play(); + if (_camTexture.isPlaying) + { + break; + } + } + } + + private void SetUpCamera() + { WebCamDevice[] devices = WebCamTexture.devices; + + if (devices.Length == 0) + { + Debug.Log("No camera detected"); + return; + } + + Debug.Log("Cameras detected:"); for (int i = 0; i < devices.Length; i++) { - Debug.Log(String.Format("{0} : {1}", i, devices[i].name)); + Debug.Log(String.Format("{0}: {1}", i, devices[i].name)); + if (!devices[i].isFrontFacing) + { + //_camTexture = new WebCamTexture(devices[i].name, (int)_scannerZone.rect.width, (int)_scannerZone.rect.height); + _camTexture = new WebCamTexture(devices[i].name); + _isCamAvaible = true; + break; + } } - //screenRect = new Rect(0, 0, Screen.width, Screen.height); - camTexture = new WebCamTexture(); + // TODO: Remove or comment this in build + if (!_isCamAvaible) + { + //_camTexture = new WebCamTexture(devices[2].name, (int)_scannerZone.rect.width, (int)_scannerZone.rect.height); + _camTexture = new WebCamTexture(devices[2].name); + _isCamAvaible = true; + } - if (camTexture != null) + if (_isCamAvaible) + { + _rawImage.texture = _camTexture; + //_rawImage.material.mainTexture = _camTexture; + StartCoroutine(YieldTryToPlayCamera()); + } else { - camTexture.requestedHeight = Screen.height; - camTexture.requestedWidth = Screen.width; - GetComponent().material.mainTexture = camTexture; - rawimage.texture = camTexture; - camTexture.Play(); + Debug.Log("No camera detected"); } } - - void OnGUI() + + // Perfom a scan once a second + private System.Collections.IEnumerator YieldScanForQR() { - // drawing the camera on screen - GUI.DrawTexture(screenRect, camTexture, ScaleMode.ScaleToFit); - // do the reading � you might want to attempt to read less often than you draw on the screen for performance sake - if (!detected) + IBarcodeReader barcodeReader = new BarcodeReader(); + //var snap = new Texture2D((int)_scannerZone.rect.width, (int)_scannerZone.rect.height, TextureFormat.RGBA32, false); + + while (true) { - if (camTexture.isPlaying) + yield return new WaitForSecondsRealtime(1.5f); + + if (_isCamAvaible) { try { - IBarcodeReader barcodeReader = new BarcodeReader(); - // decode the current frame - var result = barcodeReader.Decode(camTexture.GetPixels32(), - camTexture.width, camTexture.height); + //snap.SetPixels(_camTexture.GetPixels((int)_scannerZone.anchoredPosition.x, (int)_scannerZone.anchoredPosition.y, (int)_scannerZone.rect.width, (int)_scannerZone.rect.height)); + //var result = barcodeReader.Decode(snap.GetPixels32(), snap.width, snap.height); + var result = barcodeReader.Decode(_camTexture.GetPixels32(), _camTexture.width, _camTexture.height); if (result != null) { - Debug.Log("DECODED TEXT FROM QR: " + result.Text); - string[] param = result.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); - detected = true; - ModelLoader._id = param[0]; - ModelLoader._prefabName = param[1]; - GameBootstrapper.Instance.Game.StateMachine.Enter("AR_Ground_Scene"); + Debug.Log("QR code detected: " + result.Text); + LoadARScene(result.Text); + break; + } } + catch(Exception ex) + { + Debug.Log(String.Format("Failed trying to read QR code: {0}", ex)); } - catch (Exception ex) { Debug.LogWarning(ex.Message); } } else { - camTexture.Play(); + Debug.Log("Perfom a scan when camera not ready yet"); } } + + _camTexture.Stop(); + } + + private void LoadARScene(string result) + { + string[] param = result.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); + ModelLoader._id = param[0]; + ModelLoader._prefabName = param[1]; + GameBootstrapper.Instance.Game.StateMachine.Enter("AR_Ground_Scene"); } } \ No newline at end of file diff --git a/QCAR/somedata16 b/QCAR/somedata16 index 0407d24..a3cbda2 100644 --- a/QCAR/somedata16 +++ b/QCAR/somedata16 @@ -1 +1 @@ -AAABmUn2FY1Cnjnvb9uGhUGCR3yqaqaKjDKOO7ZHV3CwyRRA2T/EYMer8pPTKXBhdjyX+6rsNgFP+spDORmHLQWDiReJEps1r6O79cndmh2jGt//+TUPJIalkvH1NvasK7L6rSPw/q4x46VtDe27auQl5gEUxpTRZBfNnf3LphEwFJLeCUtOxBzBY7bVG9QqhKnZoW0kU9rMpBm56BYw0tBzbd7ES6PaaSqyCrDPyxwcea7IxyUu0hDFyXY0i5RcE8eQuMJz4POjg40b9K7fMcQ5G/BVon8ifmTq7dpwtjAo1XogfUne5ysAUTyzrz4Ub89kkGF7b9mB5suHfrr9YQwB0kE= \ No newline at end of file +AAABmXKclANgK4OXm5YlEu85mkMv6VBB8WB3Iyfq0IxEwqAZtIXcN3AUjy/Ip2HRDwYw0F31q2By6nYaYSyxwXRtY/bRNq6b3J1zpg7uDhH2+9i34S6ZX/SG+ziFl23EubSI8VJXJxM/xCGKTkUsPjYbyaNLQ8Zfji8rAM/I+J7/A9N8XbmP+V4oGj4ly4vEf8KJ/11pZKVv5vxpurjvWUrTy2lsNrrU/IQMH7nWOx1w67FeoI7d6sQCJOIrteDdGJn7asp/ka5e4wv3LRsrj/qzNjod0LoWSp4/01xolXY5Y7VP+ODIph5MIl90qElfX18EL804AypkloFqT5oZGJQY/MM= \ No newline at end of file