【问题标题】:How to convert erlang module (wxImage) const/array to a list in Elixir?如何将 erlang 模块 (wxImage) const/array 转换为 Elixir 中的列表?
【发布时间】:2019-08-14 00:44:30
【问题描述】:

我正在尝试使用 Erlang wxImage 库在 Elixir 中操作“test.jpg”图像,但出现错误。我不知道如何将数组/常量输出转换为列表,以便我可以在 Elixir 中使用它。

另外我不知道为什么语法似乎没问题的时候会出现子句函数错误?

defmodule Imedit2 do
  def readimg(image) do
    {:ok, _file} = File.open("happy737.txt", [:write])
    IO.puts("hi there")
    _output =
      image
      |> File.read!()
      |> :wxImage.getData()
      |> to_charlist()

    # IO.puts(is_list(output))
    # IO.puts(is_tuple(output))
    # IO.binwrite(file, output)
    # File.close(file)
  end
end
iex(58)> Imedit2.readimg("test.jpg")
hi there
** (FunctionClauseError) no function clause matching in :wxImage.getData/1

The following arguments were given to :wxImage.getData/1:

    # 1
    <<255, 216, 255, 226, 2, 28, 73, 67, 67, 95, 80, 82, 79, 70, 73, 76, 69, 0, 1,
      1, 0, 0, 2, 12, 108, 99, 109, 115, 2, 16, 0, 0, 109, 110, 116, 114, 82, 71,
      66, 32, 88, 89, 90, 32, 7, 220, 0, 1, 0, 25, ...>>

gen/wxImage.erl:405: :wxImage.getData/1
lib/imedit2.ex:5: Imedit2.readimg/1

【问题讨论】:

  • 您将二进制文件传递给函数:wxImage.getData/1。如果您查看该函数的documentation,它期望引用图像,而不是图像二进制数据。我以前从未使用过这个库,但看起来您需要调用 :wxImage.loadFile/{2,3,4} 才能从文件加载图像。您可能还需要调用new/0 函数来首先获取空白图像参考。
  • 我认为 File.read 会加载图像,这就是为什么 wximage 处理加载的图像会输出数字的原因。

标签: elixir wxerlang


【解决方案1】:

我玩了:wxImage,发现你的代码有几个问题:

  1. 您需要调用 :wx.new() 来初始化 wx,然后任何 :wxImage 函数才能工作。
  2. getData/1 的参数应该是图像句柄,而不是二进制文件数据。来自docs:

wxImage()

对象引用,表示是内部的,可以更改,恕不另行通知。它不能用于存储在磁盘上的比较或分发给其他节点使用。

对于getData/1:

getData(This) -> binary()

类型
这 = wxImage()

所以你可以这样做:

def readimg(image) do
  :wx.new()

  data =
    image
    |> String.to_charlist()
    |> :wxImage.new()
    |> :wxImage.getData()
    |> :binary.bin_to_list()

  :wx.destroy()
  data
end

但请注意,bin_to_list/1 调用速度很慢,而且我认为您无论如何都不需要它。您可能想停在:wxImage.new(),将句柄保存在一个变量中,并使用它来调用您需要的任何其他:wxImage functions。

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多