This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert image to raw binary data

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • 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:


 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic