使用 ruby,从需要身份验证的 IP 摄像机中获取单个 JPG 图像并将图像写入文件的最简单方法是什么?例如,使用 URL 的 IP 摄像机:
http://192.168.69.81/cgi/jpg/image.cgi
我不需要对图像进行任何操作。
提前致谢。
Open the remote location and write the file as a jpeg image called image.jpg:
require 'open-uri'
url = 'http://192.168.69.81/cgi/jpg/image.cgi'
open(url, :http_basic_authentication => ['username', 'password']) do |f|
open('image.jpg','wb') do |file|
file.puts f.read
end
end
你可以试试这个
require 'net/http'
Net::HTTP.start(url, port) do |http|
req = Net::HTTP::Get.new('/image.cgi')
req.basic_auth 'username', 'password'
response = http.request(req)
open("image.cgi", "wb") do |file|
file.write(response.body)
end
end