Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Convert image to raw binary data

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I want to convert an image (company logo) to raw binary data, as I am going to load it into a desktop printer.

The printer specification (EPL2) says that is has to be raw binary data without graphic file formatting. Data must be in bytes.

How do we convert an image into bytes in java? Do we need a specific image format other than gif or jpg?

The code below doesn´t seem to work on my printer:


 
Sheriff
Posts: 26796
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I can say is, I think you have misunderstood what your printer specification says. Everything in a file is bytes. Whether those bytes represent a GIF image or a MS Word document or something else is a matter of interpretation. I don't understand what "raw" bytes meant to the people who wrote that document, or what "graphic file formatting" meant, so I can't help you with that.

As for your code, I can't tell whether it reads the file into an array of bytes correctly or not. There's certainly scope for off-by-one errors in it. If I had to read a file into an array of bytes, I would just open a DataInputStream onto the file and use its readFully() method to do that, so I don't feel like looking hard at your code. But it's more likely that you could just use a standard piece of file-copying code to send the image to the printer, instead of risking running out of memory if your file was exceptionally large.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

I have also tried to read the image into a FileInputStream with the code below, but with no succes:



What is the different between using FileInputStream and DataInputStream (readFully(byte[] b))?

Regarding the specification this is a copy from the documentation:

DATA = Raw binary data without graphic file formatting. Data must be in bytes. Multiply the width in bytes (p3) by the number of print lines (p4) for the total amount of graphic data. The printer automatically calculates the exact size of the data block based upon this formula.
 
Marshal
Posts: 22460
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/AvailableDoesntDoWhatYouThinkItDoes

Use a ByteArrayOutputStream to write to instead.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:http://faq.javaranch.com/java/AvailableDoesntDoWhatYouThinkItDoes

Use a ByteArrayOutputStream to write to instead.



Great, ByteArrayOutputStream seems to work. Now a dark square is being printed to the label. So far so good

When sending data to the printer I have to send some parameters as well, i.e. the position (p1, p2) and the width (p3) and lenght (p4) of the image in bytes.

I am unsure about how to calculate p3 and p4? What is the width of graphic in bytes? And what is length of graphic in dots? I don´t get it.

I got the size of the image by using:
int imageSize = graphic.length;

EPL2 Documentation of GW:
p3 =Width of graphic in bytes. Eight (8) dots = One (1) byte of data
p4 =Length of graphic in dots (or print lines).

DATA = Data must be in bytes. Multiply
the width in bytes (p3) by the number
of print lines (p4) for the total
amount of graphic data. The printer automatically
calculates the exact size of
the data block based upon this formula.

Full EPL2 documentation:
EPL2 GW
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic