This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Convert image to raw binary data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Convert image to raw binary data" Watch "Convert image to raw binary data" New topic
Author

Convert image to raw binary data

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
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:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 07, 2004
Posts: 270
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

http://faq.javaranch.com/java/AvailableDoesntDoWhatYouThinkItDoes

Use a ByteArrayOutputStream to write to instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
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
 
Don't get me started about those stupid light bulbs.
 
subject: Convert image to raw binary data
 
Similar Threads
how to insert carriage return
Using streams
Obtaining the contents of a binary file
How to pass byte array to RPC procedure
Cannot Figure Out This Syntax Error