3

我想做的是在我的网站上以表格形式捕获用户的签名。目前我已经成功地将签名板 jQuery 插件添加到我的表单中,我看到画布签名的 JSON 从网页发送到我的服务器:

Started PUT "/participant_user_reg_forms/2" for 127.0.0.1 at 2013-10-23 19:06:59             -0500
Processing by ParticipantUserRegFormsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CLNbopTBxiLI7WIRNanL9KOtGpAy1
iLjxFGy00nP750=", "participant_user_reg_form"=>{"firm_name"=>"", "user_name"=>"",
"address"=>"", "city"=>"", "state"=>"", "country"=>"", "zip_code"=>"", "email"
=>"", "phone"=>"", "fax"=>"", "question_1"=>"4", "answer_1"=>"", "question_2"=>"
5", "answer_2"=>"", "date_of_birth"=>"", "ssn"=>""}, "output"=>"[{\"lx\":69,\"ly
\":13,\"mx\":69,\"my\":12},{\"lx\":68,\"ly\":12,\"mx\":69,\"my\":13},{\"lx\":67,
\"ly\":12,\"mx\":68,\"my\":12},{\"lx\":66,\"ly\":12,\"mx\":67,\"my\":12},{\"lx\"
:65,\"ly\":12,\"mx\":66,\"my\":12}, ... ETC ... 

在我的表单控制器中,我捕获“输出”参数并尝试将其解析为图像,如下所示:

if !params[:output].nil?
  @participant_user_reg_form.signature = addSignature(params[:output])
end
...
def addSignature(data)
    require 'open3'
    instructions = JSON.parse(data).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '
    tempfile = Tempfile.new(["signature", '.png'])
    Open3.popen3("convert -size 298x55 xc:transparent -stroke blue -draw @- #{tempfile.path}") do |input, output, error|
        input.puts instructions
    end
    return tempfile
end

我从这里得到了将 JSON 转换为图像的这段代码,这是在签名板网站上的 ruby​​ on rails 服务器端转换下推荐的。

问题是当我去查看保存的签名时(在默认的回形针目录中 - :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename)我只找到空的 png 图像(png具有 0 字节数据的图像)。

我认为转换不起作用,但我对转换代码的了解/了解不够,无法知道它是否正常运行。或者,我愿意以其他方式(例如使用 chunky_png)保存在画布上生成的签名,但我无法找到任何好的信息/代码来描述或显示如何从画布到回形针图像的转换。

4

0 回答 0