escper 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ *.*~
5
+ *~
data/README ADDED
@@ -0,0 +1,44 @@
1
+ = Escper (read: "escaper") -- Convert an image to ESCPOS commands for thermal receipt printers
2
+
3
+ == Usage:
4
+
5
+ gem install escper
6
+
7
+ Test it in a Ruby script:
8
+
9
+ require 'escper'
10
+ puts Escper::Image.new('test.png', :file).to_s
11
+
12
+ Or if you read the image from an image upload form in Rails, do this:
13
+
14
+ Escper::Image.new(data.read, :blob).to_s
15
+
16
+ ... where "data" is a variable containing the image data of a multipart HTML form.
17
+
18
+ Or if you have an Magick::Image already loaded, call it like this:
19
+
20
+ Escper::Image.new(magickobject, :object)
21
+
22
+
23
+ For optimal visual results, image.png should previously be converted to an indexed, black and white 1-bit palette image. In Gimp, click on "Image -> Mode -> Indexed..." and select "Use black and white (1-bit) palette". For dithering, choose "Floyd-Steinberg (reduced color bleeding)". The image size depends on the resolution of the printer.
24
+
25
+ To send an image directly to a thermal receipt printer:
26
+
27
+ File.open('/dev/usb/lp0','w') { |f| f.write Escper::Image.new('image.png', :file).to_s }
28
+
29
+ == Licence
30
+
31
+ Copyright (C) 2011-2012 Michael Franzl <office@michaelfranzl.com>
32
+
33
+ This program is free software: you can redistribute it and/or modify
34
+ it under the terms of the GNU Affero General Public License as
35
+ published by the Free Software Foundation, either version 3 of the
36
+ License, or (at your option) any later version.
37
+
38
+ This program is distributed in the hope that it will be useful,
39
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
40
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41
+ GNU Affero General Public License for more details.
42
+
43
+ You should have received a copy of the GNU Affero General Public License
44
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -18,4 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "rmagick"
21
23
  end
@@ -1,41 +1,52 @@
1
+ # Escper -- Convert an image to ESCPOS commands for thermal printers
2
+ # Copyright (C) 2011-2012 Michael Franzl <michael@billgastro.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Affero General Public License as
6
+ # published by the Free Software Foundation, either version 3 of the
7
+ # License, or (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Affero General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
1
17
  require 'RMagick'
18
+
2
19
  module Escper
3
20
  class Image
4
- def initialize(data, type)
21
+ def initialize(source, type)
5
22
  if type == :file
6
- @image = convert(Magick::Image.read(data).first)
23
+ @image = Magick::Image.read(source).first
7
24
  elsif type == :blob
8
- @image = convert(Magick::Image.from_blob(data).first)
25
+ @image = Magick::Image.from_blob(source).first
26
+ elsif type == :object
27
+ @image = source
9
28
  end
29
+ @x = (@image.columns / 8.0).round
30
+ @y = (@image.rows / 8.0).round
31
+ @x = 1 if @x.zero?
32
+ @y = 1 if @y.zero?
10
33
  end
11
34
 
12
- def convert(img=nil)
13
- if img.nil? and @image
14
- @image = @image.quantize 2, Magick::GRAYColorspace
15
- @image = crop(@image)
16
- return @image
17
- else
18
- img = img.quantize 2, Magick::GRAYColorspace
19
- img = crop(img)
20
- return img
21
- end
35
+ def convert
36
+ @image = @image.quantize 2, Magick::GRAYColorspace
22
37
  end
23
38
 
24
- def crop(image)
25
- @x = (image.columns / 8.0).round
26
- @y = (image.rows / 8.0).round
27
- @x = 1 if @x == 0
28
- @y = 1 if @y == 0
29
- image = image.extent @x * 8, @y * 8
30
- return image
39
+ def crop
40
+ @image = @image.extent @x * 8, @y * 8
31
41
  end
32
42
 
33
43
  def to_a
34
- colorarray = @image.export_pixels
35
- return colorarray
44
+ @image.export_pixels
36
45
  end
37
46
 
38
47
  def to_s
48
+ self.convert
49
+ self.crop
39
50
  colorarray = self.to_a
40
51
  bits = []
41
52
  mask = 0x80
@@ -44,7 +55,7 @@ module Escper
44
55
  (@x * @y * 8 * 3 * 8).times do |j|
45
56
  next unless (j % 3).zero?
46
57
  temp |= mask if colorarray[j] == 0 # put 1 in place if black
47
- mask = mask >> 1 # shift mask
58
+ mask = mask >> 1
48
59
  i += 3
49
60
  if i == 24
50
61
  bits << temp
Binary file
@@ -1,3 +1,3 @@
1
1
  module Escper
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
metadata CHANGED
@@ -1,70 +1,69 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: escper
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 3
9
- version: 1.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Michael Franzl
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-10-24 00:00:00 +02:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: ""
22
- email:
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rmagick
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ''
31
+ email:
23
32
  - office@michaelfranzl.com
24
33
  executables: []
25
-
26
34
  extensions: []
27
-
28
35
  extra_rdoc_files: []
29
-
30
- files:
36
+ files:
31
37
  - .gitignore
32
38
  - Gemfile
39
+ - README
33
40
  - Rakefile
34
41
  - escper.gemspec
35
42
  - lib/escper.rb
43
+ - lib/escper/test.png
36
44
  - lib/escper/version.rb
37
- has_rdoc: true
38
45
  homepage: http://michaelfranzl.com
39
46
  licenses: []
40
-
41
47
  post_install_message:
42
48
  rdoc_options: []
43
-
44
- require_paths:
49
+ require_paths:
45
50
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
51
+ required_ruby_version: !ruby/object:Gem::Requirement
47
52
  none: false
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
- version: "0"
54
- required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
58
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- segments:
60
- - 0
61
- version: "0"
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
62
63
  requirements: []
63
-
64
64
  rubyforge_project: escper
65
- rubygems_version: 1.3.7
65
+ rubygems_version: 1.8.24
66
66
  signing_key:
67
67
  specification_version: 3
68
68
  summary: Converts bitmaps to the ESC/POS receipt printer command
69
69
  test_files: []
70
-