iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
20200401 開啟外部檔案的語法
by 黃國哲 2020-04-01 08:25:57, Reply(0), Views(662)

開啟外部檔案的語法:
///請先匯入:using System.Diagnostics;

寫法一:
///Process.Start (@"C:\Users\Est\Desktop\GodHand3D\GodHand3D.exe");
檔名需要全部為英文,不能有中文的字元,不然會讀取不到。

實際例子如下:點擊滑鼠會開啟桌面的一個檔案名稱為「GJ.png」的檔案




寫法二:
///ProcessStartInfo open = new ProcessStartInfo ();
///open.FileName = "GodHand3D.exe"; // 檔案名稱
///open.WorkingDirectory = @"C:\Users\Est\Desktop\GodHand3D"; // 資料夾路徑
///Process.Start (open);

開啟外部的圖片到程式中顯示
程式碼:

using UnityEngine;
using System.Collections;
using System.Drawing;
 
public class ReadImage : MonoBehaviour {
 
    void Start () {
        Bitmap image = new Bitmap("C:/C.jpg");
 
        Texture2D t = new Texture2D(image.Width, image.Height);
 
        for(int x=0; x<image.Width; x++){
            for(int y=0; y<image.Height; y++){
                t.SetPixel(x,y,
                      new Color32(
                       image.GetPixel(x,y).R,
                       image.GetPixel(x,y).G,
                       image.GetPixel(x,y).B,
                       image.GetPixel(x,y).A
                       )
                );
            }
        }
        t.Apply();
        renderer.material.mainTexture = t;
    }
}






部落格




Unity讀取PDF


-------------------------------------------------------------------------------
//讀取PDF中的圖片

private void ReadPDFImage()
{
    string path = Application.streamingAssetsPath + "/aa.pdf";
    ExtractImageEvent(path);
}

private void ExtractImageEvent(string padPath)
{
    try
    {
        int index = 0;
        PdfReader pdfReader = new PdfReader(padPath);
        Debug.Log(pdfReader.NumberOfPages);
        for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
        {
            PdfReader pdf = new PdfReader(padPath);
            PdfDictionary pg = pdf.GetPageN(pageNumber);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
            try
            {
                foreach (PdfName name in xobj.Keys)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        string width = tg.Get(PdfName.WIDTH).ToString();
                        string height = tg.Get(PdfName.HEIGHT).ToString();
                        //ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject((GraphicsState)new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg);
                        ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new GraphicsState(), (PRIndirectReference)obj, tg);
                        RenderImageByte(imgRI,index);

                    }
                }
            }
            catch
            {      continue; }
        }
    }
    catch (Exception)
    {

        throw;
    }
}

private void RenderImageByte(ImageRenderInfo renderInfo,int index)
{
    PdfImageObject imageObj = renderInfo.GetImage();
    pimage = imageObj.GetDrawingImage();
    MemoryStream ms = new MemoryStream();
    pimage.Save(ms, ImageFormat.Png);
    byte[] byteData = new Byte[ms.Length];
    ms.Position = 0;
    ms.Read(byteData, 0, byteData.Length);
    ms.Close();
    Texture2D tex2d = new Texture2D(500, 1000);
    if (tex2d.LoadImage(byteData))
    {
        UIimage.texture = tex2d;
    }

    ///保存到本地
    //Bitmap dd = new Bitmap(pimage);
    //dd.Save(Application.dataPath + "/Resources/wode.Jpeg");

}

----------------------------------------------------------------------------------------------

//讀取PDF文字內容
private void ReadPDF_Click()
{
    string path = Application.streamingAssetsPath + "/SimplePDF.pdf";
    msg.text = OnCreated(path);
}

private string OnCreated(string filepath)
{
    try
    {
        string pdffilename = filepath;
        PdfReader pdfReader = new PdfReader(pdffilename);
        int numberOfPages = pdfReader.NumberOfPages;
        string text = string.Empty;

        for (int i = 1; i <= numberOfPages; ++i)
        {
            iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
            text += iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i, strategy);
        }
        pdfReader.Close();

        return text;
    }
    catch (Exception ex)
    {
        StreamWriter wlog = File.AppendText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\mylog.log");
        wlog.WriteLine("出錯文件:" + "原因:" + ex.ToString());
        wlog.Flush();
        wlog.Close();
        return null;
    }
}


Reply