Invalid encoding with rqrcode

不问归期 提交于 2020-01-05 08:47:15

问题


I'm having an invalid encoding error that doesn't let me save the image to a carrierwave uploader.

require 'rqrcode_png'
img = RQRCode::QRCode.new( 'test', :size => 4, :level => :h ).to_img.to_s

img.valid_encoding? 

=> false

回答1:


I'm not sure if this is what you're looking for, in my case I needed to associate the generated QR code with a Rails model using carrierwave, what I ended up doing was saving the image to a temp file, associating that file with the model and afterwards deleting the temp file, here's my code:

def generate_qr_code!
  tmp_path = Rails.root.join('tmp', "some-filename.png")
  tmp_file = RQRCode::QRCode.new(self.hash_value).to_img.resize(200,200).save(tmp_path)

  # Stream is handed closed, we need to reopen it
  File.open(tmp_file.path) do |file|
    self.qr_code = file
  end

  File.delete(tmp_file.path)

  self.save!
end


来源:https://stackoverflow.com/questions/25223060/invalid-encoding-with-rqrcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!