這也真的很搞死我,先來說說硬體吧
Arduino這一方主要有:
- Uno (這不用解釋了)
- Grove Camera Serail Kit - 這個感覺是OV0706,在網路上找到很多攝影機,現在有電腦視覺辨識的攝影機了,在這之下的感覺都LOW掉,但我還是用這個,網路上有很多程式碼,我是用這個。
- SD Card module - 上面那Arduino程式碼有包含把相片存入SDcard中。
- Bluetooth (HC05) - 便宜好用,在用之前要記得初始化(主從設定、藍芽名稱、密碼,八八八,這裡有指令可以往後忘了來看)
- Serial.begin(9600) 傳出去的值才是對的,我也不知道為啥
Arduino 主要程式: 主要改編的是讀取SDcard中的圖片,很重要的是資料型態,再傳給Unity之前可以用Online軟體去看Arduino讀取的byte印出來是不是正確的,1.轉碼網址,2.解碼轉圖。
File file = SD.open(Image_name,FILE_READ);
if (file) {
while (file.position() < file.size()) //用SD lib 去讀取資料長度與位置
{
if(file.available())
{
uint8_t test; //重點:不知道是否arduino傳送一次只能8byte 這可能要大家指教一下 但我用uint8_t印出來資料是正確的
test = file.read(); //把讀取的區塊資料塞到變數中
String stringOne = String(test, HEX); //這邊是把區塊資料做 HEX編碼 型態像這樣 0xff , BIN:011101010 , OTC,DEC等資料型態 就是十進位二進位十六進位八啦叭啦
//fill in "0" to Hex 這邊是做一個防止錯誤的判斷 arduino轉碼時候好像會把0 lost掉 就觀察下來會漏在第一個字碼
if(stringOne.length() % 2 == 1)
{
stringOne = "0"+stringOne;
}else{
stringOne = stringOne;
}
Serial.print(stringOne); //透過BLT TX 傳給 Unity接收
}
}
}
file.close();
Unity端
public class BasicDemo : MonoBehaviour {
private BluetoothDevice device;
public Text statusText;
public string data = "";
// Use this for initialization
void Awake () {
BluetoothAdapter.enableBluetooth();
device = new BluetoothDevice();
device.MacAddress = "98:D3:33:81:18:3E";//8096
}
public void CustomerButtonMac(string BtnInput)
{
device.MacAddress = BtnInput;
}
public void CustomerInputFunction()
{
device.MacAddress = CustomerInput.text;
Debug.Log(CustomerInput.text);
}
public void connect() {
statusText.text = "Status : ...";
device.connect();
}
public void disconnect() {
device.close();
}
public void sendHello() {
if (device != null) {
device.send (System.Text.Encoding.ASCII.GetBytes ("1"));
}
}
public void SendData()
{
FindObjectOfType().BlueToothToImage(data);
}
public void ShowImage()
{
FindObjectOfType().LoadPng();
data = "";
}
//############### Reading Data #####################
void Update() {
if (device.IsReading) {
byte [] msg = device.read ();
if (msg != null ) {
string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);
data += content;
statusText.text = data;
}
}
}
}
Unity成像
public void LoadPng()
{
StartCoroutine(load_image(readPicCount));
}
IEnumerator load_image(int count)
{
string[] filePaths = Directory.GetFiles(Application.persistentDataPath, "*.png"); // 讀取空間中 png檔案
WWW www = new WWW("file://" + filePaths[count]); // 這邊是讀取在android手機路徑中
yield return www; // 等待下載完成
Texture2D new_texture = new Texture2D(640, 480); // 設定材質大小
www.LoadImageIntoTexture(new_texture); // 利用www下載材質賦予給new_texture
GetComponent().material.mainTexture = new_texture; // 顯示材質 我直接把這程式掛在3D物件上
bytes = new_texture.EncodeToJPG(); //測試用 要解碼圖片是不是跟傳來的一樣
}
public void BlueToothToImage(string data)
{
count++;
File.WriteAllBytes(Application.persistentDataPath + "/SavedScreen" + count + ".png", ToByte(data)); //存在android空間中
readPicCount = count-1;
}
//HEX string to byte array 重點在此
public static byte[] ToByte(string _hexString)
{
int _digit = 2;
// make sure the _hexString's length is matching the byte format, group by 2
int _no = (Mathf.FloorToInt(_hexString.Length / _digit) * _digit) + ((_hexString.Length % _digit) * _digit);
_hexString = _hexString.PadLeft(_no, '0');
byte[] _bytes = new byte[_hexString.Length / _digit];
for (int i = 0; i < _bytes.Length; i++)
{
// only taken target hex number range
string _hexByte = _hexString.Substring(i * _digit, _digit);
// Convert "_hexByte" to bytes.
_bytes[i] = byte.Parse(_hexByte, System.Globalization.NumberStyles.HexNumber);
}
return _bytes;
}
先這樣吧 之後還要做圖片OCR
留言