유니티 C# QR code 인식 좌표 3d 오브젝트 생성 방법 간단 구현

반응형

유니티 C# QR code 인식 좌표 3d 오브젝트 생성 방법 간단 구현

  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using ZXing.Common;

public class QRCodeScanner : MonoBehaviour
{
    public Camera camera;
    public GameObject prefab; // 생성할 3D 오브젝트 프리팹
    public int cameraWidth = 640;
    public int cameraHeight = 480;
    
    private WebCamTexture webcamTexture;

    void Start()
    {
        // 웹캠 텍스처 설정
        webcamTexture = new WebCamTexture(cameraWidth, cameraHeight);
        camera.targetTexture = new RenderTexture(cameraWidth, cameraHeight, 24);
        webcamTexture.Play();
    }

    void Update()
    {
        if (webcamTexture.isPlaying)
        {
            // QR 코드 이미지 데이터를 가져옴
            var barcodeReader = new BarcodeReader();
            var color32 = webcamTexture.GetPixels32();
            var bitmap = new ZXing.BitmapLuminanceSource(color32, webcamTexture.width, webcamTexture.height);
            var result = barcodeReader.Decode(bitmap);
            
            if (result != null)
            {
                Debug.Log("QR Code Detected: " + result.Text);
                Vector3 qrPosition = GetQRCodePosition(result.Text);
                PlacePrefabAtPosition(qrPosition);
            }
        }
    }

    Vector3 GetQRCodePosition(string qrCodeData)
    {
        // QR 코드 데이터에 따라 위치를 계산하는 로직
        // 예를 들어 QR 코드 데이터에 좌표 정보가 포함되어 있다고 가정
        return new Vector3(0, 0, 0); // 실제 좌표로 변경해야 함
    }

    void PlacePrefabAtPosition(Vector3 position)
    {
        Instantiate(prefab, position, Quaternion.identity);
    }
}
반응형