亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity調(diào)用打印機(jī)打印圖片

 更新時(shí)間:2019年10月16日 14:28:09   作者:月夜風(fēng)雨磊  
這篇文章主要為大家詳細(xì)介紹了Unity通過(guò)調(diào)用打印機(jī)打印圖片的代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity打印機(jī)打印圖片的具體代碼,供大家參考,具體內(nèi)容如下

1、調(diào)用打印機(jī)首先就是要配置好打印機(jī)

就是電腦跟打印機(jī)已經(jīng)連接好,有默認(rèn)的打印機(jī)可以啟動(dòng)使用

2、調(diào)用方式

(1)使用外部第三方軟件exe

代碼如下:(就兩句)

string path = Application.dataPath + @"\Textures\002.png";
  System.Diagnostics.Process.Start("mspaint.exe", path);//調(diào)用第三方應(yīng)用去打印(其中path是要打印圖片的路徑,而mspaint.exe是調(diào)用Windows中的畫(huà)板,然后從畫(huà)板里啟用打印功能) 

(2)使用win自帶軟件

這個(gè)需要下載一個(gè)應(yīng)用(應(yīng)用會(huì)放在我的博客下載文件中名字是PrintImage.exe)
然后直接上代碼:

public void Test()
  {
    string path = Application.dataPath + @"\Textures\002.png,0,0,750,400";//從紙張的0. 0點(diǎn),將圖像調(diào)整為750×350點(diǎn)(計(jì)算:150mm/28.346 px/cm=529點(diǎn),100mm/28.346 pm/cm=352點(diǎn)) 圖片路徑
    string exepath = Application.streamingAssetsPath + @"\PrintImage.exe";//這個(gè)是需要下載的應(yīng)用直接放到電腦上就行(調(diào)用打印機(jī)打印圖片應(yīng)用的路徑)
    ProcessStartInfo info = new ProcessStartInfo(exepath);//指定啟動(dòng)進(jìn)程時(shí)使用的一組值
    info.Arguments = path;//獲取或設(shè)置啟動(dòng)應(yīng)用程序時(shí)要使用的一組命令行自變量
    using (Process p=new Process())
    {
      p.StartInfo = info;
      p.Start();
    }
  }

(3)自己進(jìn)行打印

/// <summary>
  /// 打印
  /// </summary>
  public void PrintFile()
  {
    PrintDocument pri = new PrintDocument();
    pri.PrintPage += Printpagetest;
    pri.Print();
  }

  private void Printpagetest(object sender, PrintPageEventArgs e)
  {
    try
    {
      System.Drawing.Image image = System.Drawing.Image.FromFile(printPath);
      System.Drawing.Graphics g = e.Graphics;
      g.TranslateTransform(_4AHeight, 0);
      g.RotateTransform(90);
      g.DrawImage(image, 0, 0, _4AWidth, _4AHeight);
    }
    catch (Exception ee)
    {
      Debug.LogError(ee.Message);
    }
  }

(這里的第三種我還未進(jìn)行測(cè)試,如出現(xiàn)錯(cuò)誤無(wú)法實(shí)現(xiàn)請(qǐng)指正)

這里我選擇的是第二種,1不好實(shí)現(xiàn)靜默,3太麻煩,2使用是后臺(tái)調(diào)用命令行

3、顏色問(wèn)題

同時(shí)這里本人還找到了有博主自己寫(xiě)的調(diào)用打印機(jī)方法
項(xiàng)目中需要用到調(diào)用打印機(jī)打印圖片,原本覺(jué)得會(huì)很復(fù)雜,結(jié)果一搜索發(fā)現(xiàn)Assetstore有相應(yīng)的插件。在網(wǎng)上找到別人分享的插件,完美的實(shí)現(xiàn)了功能,所以現(xiàn)在也來(lái)分享一下(因?yàn)橄肟吹骄唧w實(shí)現(xiàn),所以用工具反編譯了DLL,原本插件是直接導(dǎo)入就可以的)。

using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using UnityEngine;

namespace LCPrinter
{
  public static class Print
  {
    public static void PrintTexture(byte[] texture2DBytes, int numCopies, string printerName)
    {
      if (texture2DBytes == null)
      {
        UnityEngine.Debug.LogWarning("LCPrinter: Texture is empty.");
        return;
      }
      PrinterSettings printerSettings = new PrinterSettings();
      if (printerName == null || printerName.Equals(""))
      {
        printerName = printerSettings.PrinterName;
        UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");
      }
      string str = string.Concat(new string[]
      {
        DateTime.Now.Year.ToString(),
        "-",
        DateTime.Now.Month.ToString(),
        "-",
        DateTime.Now.Day.ToString(),
        "-",
        DateTime.Now.Hour.ToString(),
        "-",
        DateTime.Now.Minute.ToString(),
        "-",
        DateTime.Now.Second.ToString(),
        "-",
        DateTime.Now.Millisecond.ToString()
      });
      string text = (Application.persistentDataPath + "\\LCPrinterFiletmp_" + str + ".png").Replace("/", "\\");
      UnityEngine.Debug.Log("LCPrinter: Temporary Path - " + text);
      File.WriteAllBytes(text, texture2DBytes);
      Print.PrintCMD(text, numCopies, printerName);
    }

    public static void PrintTextureByPath(string path, int numCopies, string printerName)
    {
      PrinterSettings printerSettings = new PrinterSettings();
      if (printerName == null || printerName.Equals(""))
      {
        printerName = printerSettings.PrinterName;
        UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");
      }
      Print.PrintCMD(path, numCopies, printerName);
    }

    private static void PrintCMD(string path, int numCopies, string printerName)
    {
      Process process = new Process();
      try
      {
        for (int i = 0; i < numCopies; i++)
        {
          process.StartInfo.FileName = "rundll32";
          process.StartInfo.Arguments = string.Concat(new string[]
          {
            "C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo \"",
            path,
            "\" \"",
            printerName,
            "\""
          });
          process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
          process.StartInfo.UseShellExecute = true;
          process.Start();
        }
      }
      catch (Exception arg)
      {
        UnityEngine.Debug.LogWarning("LCPrinter: " + arg);
      }
      finally
      {
        process.Close();
        UnityEngine.Debug.Log("LCPrinter: Texture printing.");
      }
    }
  }
}

這是實(shí)現(xiàn)功能的源碼。調(diào)用方法如下:

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
using System.IO;
using LCPrinter;
using UnityEngine.UI;

public class LCExampleScript : MonoBehaviour {

  public Texture2D texture2D;
  public string printerName = "";
  public int copies = 1;

  public InputField inputField;

  public void printSmileButton()
  {
    Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);//打印一張編輯器中的圖片
  }

  public void printByPathButton()
  {
    Print.PrintTextureByPath("D:\\pic.png", copies, printerName);//打印一張存在指定路徑的圖片
  }
}

由于原本插件是添加好引用的,反編譯之后缺少了引用,所以要去統(tǒng)一的安裝路徑E:\ unity5.3.2 \統(tǒng)一\編輯\數(shù)據(jù)\單聲道\ lib中\(zhòng)單\ 2.0(這是我本地安裝的路徑)中找到System.Drawing.dll程序程序放入項(xiàng)目中的插件下。如在VS中報(bào)錯(cuò)沒(méi)有添加引用,則要對(duì)項(xiàng)目添加引用

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論