【问题标题】:Running console commands to convert with ImageMagick from within Unity 3d在 Unity 3d 中运行控制台命令以使用 ImageMagick 进行转换
【发布时间】:2023-03-09 11:47:01
【问题描述】:

伙计们!

所以,正如标题所示,我正在尝试将 PDF 转换为 .png。我正在使用软件包 ImageMagick。我想使用这个包从 Unity 3d 项目中即时将 pdf 转换为 png - 以便应用程序可以在需要时在游戏中将 PDF 显示为 .png 纹理,但仍将它们保留为较小的 PDF文件大小。我不太确定我做错了什么,在这里 - 但是当我在 Unity 中运行它时,我得到的只是一个打开的 cmd 提示符,其中没有我的命令。这里有什么明显的我遗漏的东西吗?代码如下:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Policy;

public class CommandLineTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string convertedDirName = "ConvertedPDFs";
        string currDir = System.Environment.CurrentDirectory;
        System.IO.Directory.CreateDirectory(currDir + @"\" + convertedDirName);

        string strCmdText;
    strCmdText= @"/c " + currDir + @"\ImageMagick\convert.exe " + currDir + @"\PDFs\Appointment.pdf " + currDir + @"\" + convertedDirName + @"\" + "Appointment.png";
        System.Diagnostics.Process.Start("CMD.exe",strCmdText);
        //ImageMagick
        print(strCmdText);
    }

}

当最后的打印语句运行时,它会打印以下字符串: c/ convert /c F:\Documents and Settings\Administrator\Desktop\ImageMagickTest\ImageMagick\convert.exe F:\Documents and Settings\Administrator\ Desktop\ImageMagickTest\PDFs\Appointment.pdf F:\Documents and Settings\Administrator\Desktop\ImageMagickTest\ConvertedPDFs\Appointment.png

对你来说,有什么明显的错误吗?我应该提一下,ImageMagick 的转换应用程序实际上并没有“安装”在我的系统上——我只是使用“便携式”版本并将它扔到我的项目文件夹中。所以我希望“转换”命令行仍然有效。这是否意味着我无法使用 dos 提示符访问它?如果我不能,那么如果我知道它将在我的项目文件夹中,我如何将图像传递给 imagemagick 中的“转换”程序?

编辑:有些人建议我访问 convert.exe 而不是 cmd.exe,并尝试以这种方式将图像路径提供给它。所以这是我尝试的第二种方式:

strCmdText= currDir + @"\PDFs\Appointment.pdf" + " " + currDir + @"\" + convertedDirName + @"\" + "Appointment.png";
        System.Diagnostics.Process.Start(currDir + @"\ImageMagick\convert.exe",strCmdText);

【问题讨论】:

  • 路径周围可能缺少引号 ',因为它们似乎有一个或多个空格。
  • 嘿 Oli,我很欣赏这个想法,但我认为报价情况还可以......如果有什么问题,打印声明不会显示吗?干杯。

标签: windows shell imagemagick cmd


【解决方案1】:

尝试使用其他一些命令(如dir)来确定您在文件系统中的位置以及出了什么问题。

另外请记住,如果您使用的是可移植版本并且它不在您的路径中,则您必须从与它所在的目录相同的目录中执行它。

【讨论】:

  • 你是绝对正确的——应该是/c,告诉DOS运行命令然后关闭cmd窗口。我为 convert.exe 应用程序添加了一个路径——我不确定它是否正确——它仍然无法正常工作。不过,dos 提示符现在肯定正在运行并正在做某事。不过,快速提问——你知道从它所在的目录调用 imagemagick 的任何例子吗?我需要使用“convert.exe”然后给它文件名吗?如果您不熟悉 ImageMagick 语法,请不要担心,但您无法回答我不问的问题,对吧? :P
  • 你根本不需要cmd,除了像dir这样的内置命令。顺便说一下,它也不是 DOS。
  • 感谢您为我指明了更好的方向!嗯...我不需要 cmd?所以我只需要在 Process.Start( 调用的第一个参数中通过它的路径调用 convert.exe,然后在第二个参数中告诉它“转换 image1.pdf image2.png”?我现在试试...
  • @Catlerd 听起来你的最后一条评论是正确的。 Imagemagick convert 有一个非常复杂的语法,在文档中有解释。基本上convert hello.pdf hello.png 应该做你想做的事。
  • 对。我很感激,而且我认为我更接近了——实际上,现在我已经开始了一个类似的问题——再次与语法有关。 [想看看吗?] (stackoverflow.com/questions/13291537/…)
猜你喜欢
  • 2014-01-26
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 2013-04-12
相关资源
最近更新 更多