Unity內通過百度api實現圖片轉文字

閒暇時間實現的一個小工具,在Unity引擎通過接入百度api實現打開windows文件資源管理器選擇圖片轉成文本,複製到剪貼板的功能。
需要導入System.Windows.Forms的dll和Baidu.Aip.Ocr的dll,並且去百度雲申請開通免費的OCR功能

主要代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Baidu.Aip.Ocr;
using System.IO;
using System;
using System.Runtime.InteropServices;
using LitJson;
using UnityEngine.UI;
public class LocalDialog {
    //鏈接指定系統函數       打開文件對話框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    public static bool GetOpenFileName1([In, Out] OpenFileName ofn) {
        return GetOpenFileName(ofn);
    }

    //鏈接指定系統函數        另存爲對話框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
    public static bool GetSFN([In, Out] OpenFileName ofn) {
        return GetSaveFileName(ofn);
    }
}
/// <summary>
/// 百度雲圖像識別
/// </summary>
public class OCRFunction : MonoBehaviour
{
    public InputField inputField;
    private const string APP_ID= "********";
    private const string APP_KEY = "***********************";
    private const string SECRET_KEY = "*****************************";
    private Ocr ocr;
    private Texture2D  texture2d;
    // Start is called before the first frame update
    void Start()
    {
        ocr = new Ocr(APP_KEY,SECRET_KEY);
        ocr.Timeout = 60000;
    }

    public void UseOcrFunc(byte[] image) {
        try {
            inputField.text = "";
            var result = ocr.AccurateBasic(image);
            OCRMsg oCRMsg = JsonMapper.ToObject<OCRMsg>(result.ToString());
            for (int i = 0; i < oCRMsg.words_result.Length; i++) {
                inputField.text += oCRMsg.words_result[i].words;
            }
        }
        catch (Exception e){
            inputField.text = "解析失敗:"+ e;
        }
    }
    /// <summary>
    /// 打開文件選區框
    /// </summary>
    public void OpenFileReader() {
        OpenFileName openFileName = new OpenFileName();
        openFileName.structSize = Marshal.SizeOf(openFileName);
        //openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";
        openFileName.filter = "圖片文件(*.jpg*.png)\0*.jpg;*.png";
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir =UnityEngine.Application.streamingAssetsPath.Replace('/', '\\');//默認路徑
        openFileName.title = "選擇圖片";
        openFileName.defExt = "JPG";//顯示文件類型
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;//0x00000008爲必選項

        //if (LocalDialog.GetSaveFileName(openFileName)) {
        //    Debug.Log(openFileName.file);
        //    Debug.Log(openFileName.fileTitle);
        //}
        if (LocalDialog.GetOpenFileName(openFileName)) {
            ImageToByteArray(openFileName.file);
        }
    }
    private byte[] fileBytes;
    /// <summary>
    /// IO加載圖片並轉換成二進制數組
    /// </summary>
    /// <param name="ImagePath"></param>
    /// <returns></returns>
    public async void ImageToByteArray(string ImagePath) {
        inputField.text = "轉換中。。。";
        FileStream fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
        int byteLength = (int)fileStream.Length;
        fileBytes = new byte[byteLength];
        await fileStream.ReadAsync(fileBytes, 0, byteLength);
        fileStream.Close();
        UseOcrFunc(fileBytes);
    }

    /// <summary>
    /// 拷貝到剪貼板
    /// </summary>
    public void CopyToScrapBook() {
        GUIUtility.systemCopyBuffer = inputField.text;
    }
}

LitJson解析類:

public class OCRMsg {
    public System.Int64 log_id;
    public int words_result_num;
    public WordsResult[] words_result;
}
public class WordsResult {
    public string words;
}

WinForm數據類:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName {
    public int structSize = 0;
    public IntPtr dlgOwner = IntPtr.Zero;
    public IntPtr instance = IntPtr.Zero;
    public String filter = null;
    public String customFilter = null;
    public int maxCustFilter = 0;
    public int filterIndex = 0;
    public String file = null;
    public int maxFile = 0;
    public String fileTitle = null;
    public int maxFileTitle = 0;
    public String initialDir = null;
    public String title = null;
    public int flags = 0;
    public short fileOffset = 0;
    public short fileExtension = 0;
    public String defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public String templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章