Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

拍照測試.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using TMPro;
  10. using System;
  11. using System.IO;
  12. using ZXing;
  13. using ZXing.QrCode;
  14. using System.Net;
  15. using SimpleJSON;
  16. using System.ComponentModel;
  17. using System.Runtime.InteropServices;
  18. public class 拍照測試 : MonoBehaviour
  19. {
  20. [SerializeField] GameObject 主要;
  21. [SerializeField] GameObject 次要;
  22. [SerializeField] RawImage rowImage;
  23. [SerializeField] RawImage rowImage1;
  24. private WebCamTexture myCam;//接收攝影機返回的圖片數據
  25. private BarcodeReader barcodeReader;
  26. public CanvasScaler canvasScaler;
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. barcodeReader = new BarcodeReader();
  31. 主要.SetActive(true);
  32. 次要.SetActive(false);
  33. StartCoroutine(open_Camera(60));
  34. }
  35. private IEnumerator open_Camera(int fps)
  36. {
  37. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); //授權開啟鏡頭
  38. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  39. {
  40. //設置攝影機要攝影的區域
  41. if (Screen.orientation == ScreenOrientation.LandscapeLeft)
  42. {
  43. // myCam = new WebCamTexture(WebCamTexture.devices[0].name, (int)screenWidth, (int)screenHeight, fps);
  44. myCam = new WebCamTexture(WebCamTexture.devices[0].name, 250, 250, fps);/* (攝影機名稱, 攝影機要拍到的寬度, 攝影機要拍到的高度, 攝影機的FPS) */
  45. }
  46. else
  47. {
  48. //myCam = new WebCamTexture(WebCamTexture.devices[0].name, (int)screenHeight, (int)screenWidth, fps);
  49. myCam = new WebCamTexture(WebCamTexture.devices[0].name, 250, 250, fps);
  50. }
  51. rowImage.texture = myCam;
  52. myCam.Play();//開啟攝影機
  53. }
  54. }
  55. public void 拍照()
  56. {
  57. Texture2D photoTexture = CapturePhoto();
  58. if (photoTexture != null)
  59. {
  60. rowImage1.texture = photoTexture;
  61. }
  62. myCam.Stop();
  63. myCam = null;
  64. Main.Global.鏡頭啟動=false;
  65. rowImage.texture = Texture2D.blackTexture;
  66. 主要.SetActive(false);
  67. 次要.SetActive(true);
  68. }
  69. private Texture2D CapturePhoto()
  70. {
  71. if (myCam != null)
  72. {
  73. int width = myCam.width;
  74. int height = myCam.height;
  75. // 创建一个新的 Texture2D,并读取当前帧的像素
  76. Texture2D photoTexture = new Texture2D(width, height);
  77. photoTexture.SetPixels(myCam.GetPixels());
  78. photoTexture.Apply();
  79. return photoTexture;
  80. }
  81. return null;
  82. }
  83. public void SavePhoto()
  84. {
  85. Texture2D photoTexture = (Texture2D)rowImage1.texture;
  86. byte[] photoBytes = photoTexture.EncodeToJPG(); // You can also use EncodeToPNG
  87. string filePath = Path.Combine(Application.persistentDataPath, "capturedPhoto.jpg");
  88. File.WriteAllBytes(filePath, photoBytes);
  89. }
  90. public void 上頁 ()
  91. {
  92. 主要.SetActive(true);
  93. 次要.SetActive(false);
  94. StartCoroutine(open_Camera(60));
  95. }
  96. public void back()
  97. {
  98. change_Secen(1);
  99. }
  100. public void change_Secen(int _screenNum)
  101. {
  102. SceneManager.LoadScene(_screenNum);
  103. }
  104. }