从base64数据创建图像文件 [英] Creating image file from base64 data

查看:223
本文介绍了从base64数据创建图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个base64编码的图像数据.我要粘贴前几个字符

I have a base64 encoded image data . I am pasting the first few characters

string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD     /2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopG   R8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgo......'

我正在用红宝石跟踪它

decoded_string=Base64.decode64 string
output_file = Tempfile.new(['image','.jpeg'])
output_file.binmode
output_file.write image 

此后,当我打开'image.jpeg'时,出现错误

After this when I am opening 'image.jpeg', It is giving error

Error interpreting JPEG image file (Not a JPEG file: starts with 0x75 0xab)

我也尝试过

File.open('a.jpeg', 'wb') do|f|
   f.write decoded_string
end 

在这种情况下,我也遇到相同的错误.

In this case also, I got the same error.

我在做什么错了?

推荐答案

File.open('shipping_label.gif', 'wb') do|f|
  f.write(Base64.decode64(base_64_encoded_data))
end

此答案来自:如何使用ruby将base64字符串另存为图像

这篇关于从base64数据创建图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆