123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using System.Text.RegularExpressions;
- using System.Data.SqlClient;
- using TMPro;
- using System;
- using System.IO;
- using ZXing;
- using ZXing.QrCode;
- using System.Net;
- using SimpleJSON;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- public class 拍照測試 : MonoBehaviour
- {
- [SerializeField] GameObject 主要;
- [SerializeField] GameObject 次要;
- [SerializeField] RawImage rowImage;
- [SerializeField] RawImage rowImage1;
- private WebCamTexture myCam;//接收攝影機返回的圖片數據
- private BarcodeReader barcodeReader;
- public CanvasScaler canvasScaler;
- // Start is called before the first frame update
- void Start()
- {
- barcodeReader = new BarcodeReader();
- 主要.SetActive(true);
- 次要.SetActive(false);
- StartCoroutine(open_Camera(60));
- }
-
-
- private IEnumerator open_Camera(int fps)
- {
- yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); //授權開啟鏡頭
- if (Application.HasUserAuthorization(UserAuthorization.WebCam))
- {
- //設置攝影機要攝影的區域
- if (Screen.orientation == ScreenOrientation.LandscapeLeft)
- {
- // myCam = new WebCamTexture(WebCamTexture.devices[0].name, (int)screenWidth, (int)screenHeight, fps);
- myCam = new WebCamTexture(WebCamTexture.devices[0].name, 250, 250, fps);/* (攝影機名稱, 攝影機要拍到的寬度, 攝影機要拍到的高度, 攝影機的FPS) */
- }
- else
- {
- //myCam = new WebCamTexture(WebCamTexture.devices[0].name, (int)screenHeight, (int)screenWidth, fps);
- myCam = new WebCamTexture(WebCamTexture.devices[0].name, 250, 250, fps);
- }
-
- rowImage.texture = myCam;
- myCam.Play();//開啟攝影機
-
- }
- }
-
- public void 拍照()
- {
- Texture2D photoTexture = CapturePhoto();
- if (photoTexture != null)
- {
- rowImage1.texture = photoTexture;
- }
- myCam.Stop();
- myCam = null;
- Main.Global.鏡頭啟動=false;
- rowImage.texture = Texture2D.blackTexture;
- 主要.SetActive(false);
- 次要.SetActive(true);
- }
-
- private Texture2D CapturePhoto()
- {
- if (myCam != null)
- {
- int width = myCam.width;
- int height = myCam.height;
-
- // 创建一个新的 Texture2D,并读取当前帧的像素
- Texture2D photoTexture = new Texture2D(width, height);
- photoTexture.SetPixels(myCam.GetPixels());
- photoTexture.Apply();
-
- return photoTexture;
- }
-
- return null;
- }
- public void SavePhoto()
- {
- Texture2D photoTexture = (Texture2D)rowImage1.texture;
- byte[] photoBytes = photoTexture.EncodeToJPG(); // You can also use EncodeToPNG
- string filePath = Path.Combine(Application.persistentDataPath, "capturedPhoto.jpg");
- File.WriteAllBytes(filePath, photoBytes);
- }
- public void 上頁 ()
- {
- 主要.SetActive(true);
- 次要.SetActive(false);
- StartCoroutine(open_Camera(60));
- }
- public void back()
- {
- change_Secen(1);
- }
- public void change_Secen(int _screenNum)
- {
-
- SceneManager.LoadScene(_screenNum);
- }
- }
|