/
CameraRawImageComponent.cs
100 lines (85 loc) · 2.54 KB
/
CameraRawImageComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
using Service.ScreenAutorotateSetting;
public class CameraRawImageComponent : MonoBehaviour {
private ScreenAutorotateSetting screenAutorotateSetting = new ScreenAutorotateSetting();
private BarcodeReader reader;
private Color32[] color;
private Result result;
private WebCamTexture webcamTexture;
private int height = Screen.height;
private int width = Screen.width;
private float timeleft;
private RawImage cameraRawImage;
/**
* インスタンス生成された時のみ実行されるメソッド
*/
void Awake () {
#if UNITY_IPHONE
BarcodeScanIOS.barcordScanInit ();
#endif
#if UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentUnityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass plugin = new AndroidJavaClass("work.syonet.bariiiii.ActivityLauncher");
plugin.CallStatic("launchActivity", "work.syonet.bariiiii.BarcodeScanAndroidActivity", currentUnityActivity );
#endif
}
/**
* Awakeの後で
* 最初のフレームのアップデート前に実行されるメソッド
*/
void Start () {
#if UNITY_EDITOR
reader = new BarcodeReader ();
cameraRawImage = GameObject.Find ("CameraRawImage").GetComponent<RawImage> ();
webcamTexture = new WebCamTexture(height, width);
webcamTexture.Play();
#endif
}
/**
* フレーム毎に一度実行されるメソッド
*/
void Update () {
#if UNITY_EDITOR
cameraRawImage.texture = webcamTexture;
readTextFronCode ();
#endif
}
void readTextFronCode() {
#if UNITY_EDITOR
color = webcamTexture.GetPixels32();
result = reader.Decode(color, width, height);
if (result.Text != null) {
// GameObject.Find ("oooooText").GetComponent<Text> ().gameObject.SetActive (false);
}
#endif
}
/**
* GUIイベントに応じて、フレームごとに複数回呼び出されるメソッド
*/
void OnGUI() {
}
/**
* Behaviour が有効/アクティブになったときに呼び出される
*/
void OnEnable() {
// 画面の回転を許可しない
screenAutorotateSetting.setAutorotateSwichFalse();
// immersiveモードを解除
Screen.fullScreen = false;
}
/**
* Behaviour が無効/非アクティブになったときに呼び出される
*/
void OnDisable() {
// 画面の回転を許可する
screenAutorotateSetting.setAutorotateSwichTrue();
}
}