Results 1 to 11 of 11
  1. #1
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Question JAVA and DPL-problems with sending image to printer

    Hi all

    I started to program Datamax printers in Java directly using DPL language. It all worked great-bar codes and tekst on labels but today i encountered a problem:

    I can't send an image into the printer and print it.

    The "downloading" phase I think is good: led shows that image is downloading and then led stops blinking...but when I try to print label with image the effect is strange. The problem maybe is in converting byte aray where the image is stored to String and puting it into DPL line and then convert again to byte array and send it direcly to printer.

    I will put some code later but could You tell me-Is somebody have some experiance in this?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by lipkerson View Post
    I will put some code later but could You tell me-Is somebody have some experiance in this?
    Nope, but my nipples will explode in delight when you show us your (relevant) code.

    kind regards,

    Jos

  3. #3
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Below i put my code of loading and printing bmp file
    but something is wrong-the printed bitmap is bad....what I'am doing wrong?

    <code>
    String printingImage="1Y11"
    + "000"
    + "0000"
    + "0000"
    + "v"; //name of image


    //DPL commands to invoke with parameters
    String STX = new Character((char) 2).toString();
    String SOH = new Character((char) 1).toString();
    String CR = new Character((char) 0x0d).toString();


    //Loading bitmap to byte[]
    File file2 = new File("c:/1/final.bmp");
    FileInputStream fin2 = new FileInputStream(file2);
    byte testbin2[] = new byte[(int) file2.length()];
    fin2.read(testbin2);

    String v = new String(testbin2); //bitmap to String

    //DPL command
    String loadAndPrintImage= SOH + "D" +CR
    + STX + "I" + "A" + "b" + "v" + CR + v + CR
    + STX + "L" +"D22" + CR + printingImage+CR + "E" + CR;

    byte[] theByteArray = loadAndPrintImage.getBytes();

    </code>
    and then of course I use Java print service and sent that byte[] direct to printer:
    <code>
    .
    .
    .
    Doc doc = new SimpleDoc(theByteArray, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
    job.print(doc, pras);

    </code>

    With barcodes and labels with text it works fine but Images....can You help me?
    Last edited by lipkerson; 06-30-2010 at 09:13 PM.

  4. #4
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Nope, but my nipples will explode in delight when you show us your (relevant) code.

    kind regards,

    Jos
    Did Your nipples exploded?
    Now You know why the issues of converting String to bmp was importend to me...but it was from area of programming that I don't use often...i integrate hardware with apps and I had no time to think about Graphics 2D.
    oooh and i very like JNA...much more like it then JNI and You?:)
    Last edited by lipkerson; 06-30-2010 at 09:57 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    So questions:
    Did 'fin2.read(testbin2)' read all of the file OK? You don't test the return code.

    String v = new String(testbin2)
    What do you expect to happen here? That seems very weird to me to convert the bytes in a bitmap file to a String???? Not all bytes convert correctly. Depends on the charset used.

  6. #6
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Exactly! I fear that this convert to String is the reason too...The DPL command is a String of characters and before it is send to printer it is converted to byte[]. The reason why I convert image bytes to String is that i thought it isthe best way to write correct DPL command (one string, then converted to byte[] and send to printer).
    It is the issue of charsets...the standart Java code page is unicode so I belive there is no errors in translating bytes to string...am I right? Is there a "binary" code page? I know it doesn't exist so how can I be sure that this String Is exacly my byte array?
    The way of solving I guess is to marge to byte arrays...one translated string to byte (coomand) and one with imagedata, right?

    The loading of a bitmap is good...i addes:

    File check = new File("C:/2/check.bmp");
    OutputStream out = new FileOutputStream(check);
    out.write(testbin2);
    out.close();


    to check and the check.bmp is exacly the same bmp file that I load.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    I belive there is no errors in translating bytes to string
    Here is a program to test byte to String conversion:
    Java Code:
    // Test  if String can hold 256 different values from a byte array
    
    public class TestByteToString {
    
       public static void main(String args[]) {
         byte[] all256 = new byte[256];
         // fill array with 0-255
         for(int i=0; i < 256; i++) {
           all256[i] = (byte)i;
         }
         String aStr = new String(all256);
         for(int i=0; i < 256; i++) {
           if(i != aStr.charAt(i)) {
             System.out.println("not equal i=" + i + ", char=" + Integer.toHexString(aStr.charAt(i)));
           }
         }
      }
    }
    /* Part of output:
    not equal i=128, char=20ac
    not equal i=129, char=fffd
    not equal i=130, char=201a
    not equal i=131, char=192
    not equal i=132, char=201e 
     ...
    */
    The DPL command must have some documentation on how to print images.

  8. #8
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    not equal:/ so now I know in which part of DPL command I do mistake.

    There is a documantation about sending Images but it is laconic:

    "STX I Input Image Data This command must precede image downloading from a host computer to the printer. The data that immediately follows the command string will be image data. If any of the 8-bit input formats are to be used, it is necessary to disable the Immediate Command interpreter by executing an command before issuing the <STX>I command.

    Syntax: <STX>Iabfnn…n<CR>data

    Where:
    a - Memory Module Bank Select (see Appendix K).
    b - Data Type (optional), A or omit.
    b Value: Image Data Value Range:
    ----- A ASCII Characters 0-9, A-F, (7 bit)
    ----- omitted 00-FF, (8 bit)

    f - Designator: Format Type:
    ----- F 7-bit Datamax image load file
    ----- B .BMP 8-bit format (image flipped), black and white (B&W)
    ----- b .BMP 8-bit format (image as received), B&W
    ----- I .IMG 8-bit format (image flipped), B&W
    ----- i .IMG 8-bit format (image as received), B&W
    ----- P .PCX 8-bit format (image flipped), B&W
    ----- p .PCX 8-bit format (image as received), B&W
    ----- nn…n - Up to 16 characters used as an image name.

    <CR> - 0x0d terminates the name.
    data - Image data

    Sample:
    <SOH>D
    <STX>IDpTest <CR>
    data...data <CR>

    The sample instructs the printer to (1) receive an 8-bit PCX image sent by the host in an 8-bit data format, (2) name the image ‘Test’, and (3) store it in memory module D (with a .dim file extension)."

    In my code I try send bmp file into module A.
    Last edited by lipkerson; 07-01-2010 at 08:59 AM.

  9. #9
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Ok. I have a solution:-)

    Quote Originally Posted by Norm View Post
    Part of output:
    not equal i=128, char=20ac
    not equal i=129, char=fffd
    not equal i=130, char=201a
    not equal i=131, char=192
    not equal i=132, char=201e
    ...

    You help me a lot-I was thinking that I have a error in my DPl syntax but the problem was trivial (huge for me but You seen it immediately). I'am now at work but as soon as I could I will post a solution.
    Last edited by lipkerson; 07-01-2010 at 10:30 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Using this charset seems to preserve all 256 bytes:
    Java Code:
         Charset charset = Charset.forName("ISO-8859-1");      //<<< this gives 100% equality
    
         String aStr = new String(all256, charset);   // convert bytes to String

  11. #11
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Like I promised I put a solution here:

    First convert Your BufferedImage into Byte array.

    Then:

    Control Characters:
    Java Code:
        private static final String STX = new Character((char) 2).toString();
        private static final String SOH = new Character((char) 1).toString();
        private static final String CR = new Character((char) 0x0d).toString();
    Print Image command :
    Java Code:
        private static String commandPrintImage= "1Y"//Fixed values
                + "11"//Multiple of width and height
                + "000"//Fixed value
                + "0010"//from down
                + "0330"//from left
                + logo;//image name
    Before downloading image into printer we clean module and set printer status to avoid commands that may have been read by mistake from the bitmap binary:
    Java Code:
    String reset= STX + "qA" + CR + SOH + "D" + CR ;

    Command that downloads imag called "logo" into module A (bmp):
    Java Code:
    String commandDownloadingImage= STX + "I" + "A" + "b" + logo + CR;

    Command thats prints label-in this case it is only image:
    Java Code:
    String commandPrintLabel= STX + "L" + CR 
        + "D11" + CR 
        + "m" + CR //metric units
        + commandPrintImage+ CR 
        + "E" + CR;//end
    Now we have to convert all Strings to byte arrays (getBytes())

    and then those byte arrays marge into one big byte array that represents all above commands and image (remember - the sequence is importent!).


    This one big byte array just send to printer. I had a printer connected to USB so i used :
    Java Code:
    Doc doc = new SimpleDoc(finalCommand, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
    but if You had connected via com etc instead of using DocFlavor You could just send this data direct to prot that You use.

    PS: "Logo" is just a name of file that the print will use to write recived image in module.

    And that's ALL :-)
    Last edited by lipkerson; 07-11-2010 at 10:13 AM.

Similar Threads

  1. sending data to printer directly
    By Abhinav.k in forum Advanced Java
    Replies: 4
    Last Post: 12-09-2009, 07:26 AM
  2. Replies: 2
    Last Post: 09-10-2009, 02:29 PM
  3. Sending text to printer
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 05-25-2009, 06:14 AM
  4. Sending PCL to Citizen Printer
    By CommanderLuv in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 02:20 PM
  5. Replies: 2
    Last Post: 02-20-2009, 07:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •