Merge pull request #17 from Eric-Guo/windows_dev_support
Add dev support in windows
This commit is contained in:
commit
6fc4ef96fa
|
@ -4,7 +4,11 @@ module RuCaptcha
|
||||||
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
|
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
|
||||||
headers['Pragma'] = 'no-cache'
|
headers['Pragma'] = 'no-cache'
|
||||||
|
|
||||||
send_data generate_rucaptcha, disposition: 'inline', type: 'image/png'
|
if Gem.win_platform?
|
||||||
|
send_file generate_rucaptcha, disposition: 'inline', type: 'image/png'
|
||||||
|
else
|
||||||
|
send_data generate_rucaptcha, disposition: 'inline', type: 'image/png'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -60,12 +60,21 @@ module RuCaptcha
|
||||||
-implode #{RuCaptcha.config.implode} -trim label:- png:-
|
-implode #{RuCaptcha.config.implode} -trim label:- png:-
|
||||||
CODE
|
CODE
|
||||||
|
|
||||||
command.strip!
|
if Gem.win_platform?
|
||||||
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
|
png_file_path = Rails.root.join('tmp', 'cache', "#{code}.png")
|
||||||
Process.waitpid(pid)
|
command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}"
|
||||||
err = stderr.read
|
require 'open3'
|
||||||
raise "RuCaptcha: #{err.strip}" if err != nil && err.length > 0
|
_stdout_str, stderr_str = Open3.capture3(command)
|
||||||
stdout.read
|
raise "RuCaptcha: #{stderr_str.strip}" if stderr_str != nil && stderr_str.length > 0
|
||||||
|
png_file_path
|
||||||
|
else
|
||||||
|
command.strip!
|
||||||
|
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
|
||||||
|
Process.waitpid(pid)
|
||||||
|
err = stderr.read
|
||||||
|
raise "RuCaptcha: #{err.strip}" if err != nil && err.length > 0
|
||||||
|
stdout.read
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
require 'tmpdir'
|
||||||
|
|
||||||
describe 'OCR' do
|
describe 'OCR' do
|
||||||
before do
|
before do
|
||||||
|
@tmp_dir = Dir.mktmpdir
|
||||||
@samples = []
|
@samples = []
|
||||||
10.times do
|
10.times do
|
||||||
@samples << SecureRandom.hex(2)
|
@samples << SecureRandom.hex(2)
|
||||||
end
|
end
|
||||||
@filenames = []
|
@filenames = []
|
||||||
@samples.each do |chars|
|
@samples.each do |chars|
|
||||||
fname = File.join(File.dirname(__FILE__), "..", "tmp", "#{chars}.png")
|
fname = File.join(@tmp_dir, "#{chars}.png")
|
||||||
img = RuCaptcha::Captcha.create(chars)
|
img = RuCaptcha::Captcha.create(chars)
|
||||||
File.open(fname, 'w+') do |f|
|
File.open(fname, 'w+') do |f|
|
||||||
f.puts img
|
f.puts img
|
||||||
|
@ -19,14 +21,13 @@ describe 'OCR' do
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
path = File.expand_path File.join(File.dirname(__FILE__), '..', 'tmp/*.png')
|
FileUtils.rm_f(@tmp_dir)
|
||||||
FileUtils.rm_f(path)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not read by OCR lib' do
|
it 'should not read by OCR lib' do
|
||||||
results = []
|
results = []
|
||||||
@samples.each do |chars|
|
@samples.each do |chars|
|
||||||
str = RTesseract.new(File.join(File.dirname(__FILE__), "..", "tmp", "#{chars}.png"), processor: 'mini_magick').to_s
|
str = RTesseract.new(File.join(@tmp_dir, "#{chars}.png"), processor: 'mini_magick').to_s
|
||||||
results << "- Chars: #{chars}, OCR read #{str.strip}"
|
results << "- Chars: #{chars}, OCR read #{str.strip}"
|
||||||
expect(chars).not_to eq(str)
|
expect(chars).not_to eq(str)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue