0

我正在尝试使用 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
4

1 回答 1

1

我玩了一下:wxImage,发现您的代码存在一些问题:

  1. 在任何功能起作用之前,您需要调用:wx.new()来初始化 wx 。:wxImage
  2. 的参数getData/1应该是图像句柄,而不是二进制文件数据。从文档

wxImage()

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

对于getData/1

获取数据(此)-> 二进制()

类型
This = 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函数。

于 2019-03-23T03:53:11.487 回答