Announcement

Collapse
1 of 2 < >

IBMi Brunch and Learn Series by Programmers.io

Dear IBMi Community Member,

We are delighted to announce the launch of our new educational series, the "Brunch and Learn" sessions, here at Programmers.io. We are excited to provide you with an opportunity to participate in these upcoming sessions. To accommodate a wider audience, these sessions will be conducted at 11:30 am EST/8:30 am PST, allowing for greater flexibility.

To gain more insights about the sessions and secure your spot, we invite you to visit the following link:

https://programmers.io/ibm-i-brunch-learn/

We kindly request you to share this information with your friends and colleagues who are actively working in the IBMi world. Their participation will contribute to a more enriching and collaborative learning experience.



Thank you for your support.
Programmers.io Team
2 of 2 < >

IBMi Brunch and Learn Series by Programmers.io

Dear IBMi Community Member,

We are delighted to announce the launch of our new educational series, the "Brunch and Learn" sessions, here at Programmers.io. We are excited to provide you with an opportunity to participate in these upcoming sessions. To accommodate a wider audience, these sessions will be conducted at 11:30 am EST/8:30 am PST, allowing for greater flexibility.

To gain more insights about the sessions and secure your spot, we invite you to visit the following link:

https://programmers.io/ibm-i-brunch-learn/

We kindly request you to share this information with your friends and colleagues who are actively working in the IBMi world. Their participation will contribute to a more enriching and collaborative learning experience.



Thank you for your support.
Programmers.io Team
See more
See less

Convert .PNG to ASCII/EBCDIC Hexadecimal

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert .PNG to ASCII/EBCDIC Hexadecimal

    I've been working on a Web Services project for my company.
    After several posts on here I have had a tremendous amount of help. (Thanks to ALL)
    I've got another issue...

    I'm taking a Base64 encoded XML string (.PNG), decoding it and storing it to the IFS.
    (See Image#1 for decoded image)

    My plan is to convert this to Hexadecimal and build an internal Print File that I can send to my Zebra Printer using Scott K's PRTSTMF.

    I'm currently using the CVTHC/CVTCH to convert to Hex before I build my print file.
    The problem is that it's not picking up the white space... just the data.
    (Image #2 is what it looks like in the IFS)

    When I convert this to Hex, I'm not getting the Blank Space.. Or X'40'...or (Hex) 00
    Should I use a Different Procedure than CVTHC? (Image#3 is the converted Hex.Embedded in ZPL code. )

    I've been messing with the ZPL code a lot because I'm unfamiliar with it, but when I get it to print it's just a line of dots...

    I'll p[ost some code here in a bit.

  • #2
    You ever got this working?

    Comment


    • c0nfitty
      c0nfitty commented
      Editing a comment
      Did my post below help?

  • #3
    Yes!
    I used a bit of java to help me generate the ZPL code. The main method accepts a string array. I have the java prototyped in an ILE pgm. I send it the IFS path and it returns the ZPL code. Then I weite the zpl to a printer file that's overridden to an outq. Hope this helps.

    Here's the java
    Code:
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import javax.imageio.ImageIO;
    import java.lang.Exception;
    import java.lang.ClassLoader;
    import java.io.File;
    public class ZPLConverter {
        private int blackLimit = 380;
        private int total;
        private int widthBytes;
        private boolean compressHex = false;
        private static Map<Integer, String> mapCode = new HashMap<Integer, String>();
        {
            mapCode.put(1, "G");
            mapCode.put(2, "H");
            mapCode.put(3, "I");
            mapCode.put(4, "J");
            mapCode.put(5, "K");
            mapCode.put(6, "L");
            mapCode.put(7, "M");
            mapCode.put(8, "N");
            mapCode.put(9, "O");
            mapCode.put(10, "P");
            mapCode.put(11, "Q");
            mapCode.put(12, "R");
            mapCode.put(13, "S");
            mapCode.put(14, "T");
            mapCode.put(15, "U");
            mapCode.put(16, "V");
            mapCode.put(17, "W");
            mapCode.put(18, "X");
            mapCode.put(19, "Y");
            mapCode.put(20, "g");
            mapCode.put(40, "h");
            mapCode.put(60, "i");
            mapCode.put(80, "j");
            mapCode.put(100, "k");
            mapCode.put(120, "l");
            mapCode.put(140, "m");
            mapCode.put(160, "n");
            mapCode.put(180, "o");
            mapCode.put(200, "p");
            mapCode.put(220, "q");
            mapCode.put(240, "r");
            mapCode.put(260, "s");
            mapCode.put(280, "t");
            mapCode.put(300, "u");
            mapCode.put(320, "v");
            mapCode.put(340, "w");
            mapCode.put(360, "x");
            mapCode.put(380, "y");        
            mapCode.put(400, "z");            
        }
        public String convertfromImg(BufferedImage image) throws IOException {
            String cuerpo = createBody(image);
            if(compressHex)
               cuerpo = encodeHexAscii(cuerpo);
            return headDoc() + cuerpo + footDoc();        
        }
        private String createBody(BufferedImage orginalImage) throws IOException {
            StringBuffer sb = new StringBuffer();
            Graphics2D graphics = orginalImage.createGraphics();
            graphics.drawImage(orginalImage, 0, 0, null);
            int height = orginalImage.getHeight();
            int width = orginalImage.getWidth();
            int rgb, red, green, blue, index=0;        
            char auxBinaryChar[] =  {'0', '0', '0', '0', '0', '0', '0', '0'};
            widthBytes = width/8;
            if(width%8>0){
                widthBytes= (((int)(width/8))+1);
            } else {
                widthBytes= width/8;
            }
            this.total = widthBytes*height;
            for (int h = 0; h<height; h++)
            {
                for (int w = 0; w<width; w++)
                {
                    rgb = orginalImage.getRGB(w, h);
                    red = (rgb >> 16 ) & 0x000000FF;
                    green = (rgb >> 8 ) & 0x000000FF;
                    blue = (rgb) & 0x000000FF;
                    char auxChar = '1';
                    int totalColor = red + green + blue;
                    if(totalColor>blackLimit){
                        auxChar = '0';
                    }
                    auxBinaryChar[index] = auxChar;
                    index++;
                    if(index==8 || w==(width-1)){
                        sb.append(fourByteBinary(new String(auxBinaryChar)));
                        auxBinaryChar =  new char[]{'0', '0', '0', '0', '0', '0', '0', '0'};
                        index=0;
                    }
                }
                sb.append("\n");
            }
            return sb.toString();
        }
        private String fourByteBinary(String binaryStr){
            int decimal = Integer.parseInt(binaryStr,2);
            if (decimal>15){
                return Integer.toString(decimal,16).toUpperCase();
            } else {
                return "0" + Integer.toString(decimal,16).toUpperCase();
            }
        }
        private String encodeHexAscii(String code){
            int maxlinea =  widthBytes * 2;        
            StringBuffer sbCode = new StringBuffer();
            StringBuffer sbLinea = new StringBuffer();
            String previousLine = null;
            int counter = 1;
            char aux = code.charAt(0);
            boolean firstChar = false;
            for(int i = 1; i< code.length(); i++ ){
                if(firstChar){
                    aux = code.charAt(i);
                    firstChar = false;
                    continue;
                }
                if(code.charAt(i)=='\n'){
                    if(counter>=maxlinea && aux=='0'){
                        sbLinea.append(",");
                    } else     if(counter>=maxlinea && aux=='F'){
                        sbLinea.append("!");
                    } else if (counter>20){
                        int multi20 = (counter/20)*20;
                        int resto20 = (counter%20);
                        sbLinea.append(mapCode.get(multi20));
                        if(resto20!=0){
                            sbLinea.append(mapCode.get(resto20) + aux);    
                        } else {
                            sbLinea.append(aux);    
                        }
                    } else {
                        sbLinea.append(mapCode.get(counter) + aux);
                        if(mapCode.get(counter)==null){
                        }
                    }
                    counter = 1;
                    firstChar = true;
                    if(sbLinea.toString().equals(previousLine)){
                        sbCode.append(":");
                    } else {
                        sbCode.append(sbLinea.toString());
                    }                
                    previousLine = sbLinea.toString();
                    sbLinea.setLength(0);
                    continue;
                }
                if(aux == code.charAt(i)){
                    counter++;                
                } else {
                    if(counter>20){
                        int multi20 = (counter/20)*20;
                        int resto20 = (counter%20);
                        sbLinea.append(mapCode.get(multi20));
                        if(resto20!=0){
                            sbLinea.append(mapCode.get(resto20) + aux);    
                        } else {
                            sbLinea.append(aux);    
                        }
                    } else {
                        sbLinea.append(mapCode.get(counter) + aux);
                    }
                    counter = 1;
                    aux = code.charAt(i);
                }            
            }
            return sbCode.toString();
        }
        private String headDoc(){
            String str = "^XA " +
                            "^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", ";
            return str;
        }
        private String footDoc(){
            String str = "^FS"+
                            "^XZ";        
            return str;
        }
        public void setCompressHex(boolean compressHex) {
            this.compressHex = compressHex;
        }
        public void setBlacknessLimitPercentage(int percentage){
            blackLimit = ( percentage * 768 / 100);
        }
        public static void main(String[] args) throws Exception {
            BufferedImage orginalImage = ImageIO.read(new File(args[0].trim()));
            ZPLConverter zp = new ZPLConverter();
            zp.setCompressHex(true);
            zp.setBlacknessLimitPercentage(50);     
                args[1] = zp.convertfromImg(orginalImage);
        }
    }
    Here's the RPG

    Code:
    H DFTACTGRP(*NO) Thread(*Serialize)
         ************************************************
         FPRT198    O    F  132        PRINTER usropn
         ************************************************
          /COPY QSYSINC/QRPGLESRC,JNI
          *---Java-Variables--------------------------
           dcl-s j_mainParam0 object(*JAVA : 'java.lang.String' ) Dim(2);
           dcl-s airstring Object(*JAVA:'java.lang.String');
          *------EBCDIC-char-to-Java-String--------------------
           dcl-pr makestring Object(*JAVA:'java.lang.String') extproc (*JAVA:
                                          'java.lang.String':*CONSTRUCTOR );
               bytes varchar(30) const;
           end-pr;
          *-----Java-String-to-EBCDIC-char---------------------
           dcl-pr getBytes varchar(95000) extproc(*JAVA:'java.lang.String':
                                                                 'getBytes');
           End-Pr;
          *----ZPLConverter-png-to-ZPL-Java-Method---------------------
             dcl-pr main  extproc(*JAVA : 'ZPLConverter' : 'main' ) Static;
               arg0 Object(*JAVA : 'java.lang.String' ) Dim(2) Options(*VARSIZE);
             end-pr;
          *----Qc3ExecuteCommand-prototype-------------------------
              dcl-pr QcmdExc extpgm;
                *n char(512) options(*varsize) const;
                *n packed(15:5) const;
              end-pr;
          *---Main-prototype---------------------------------------
              dcl-pr Mainz extpgm('ZPLCONVERT');
                *n char(150);
                *n char(10);
               // *n ind;
              end-pr;
    
              dcl-pi Mainz;
                img char(150);
                outq char(10);
               // prt ind;
              end-pi;
    
            dcl-s prt ind;
          *---Standalone-Variables------------------------------
                dcl-s cmd varchar(512);
                dcl-s len packed(15:5);
                dcl-s t char(1) inz('''');
                dcl-s buf char(132);
                dcl-s x int(10) inz(1);
                dcl-s label char(95000);
          *-----------------------------------------------------
           /Free
            cmd = 'ADDENVVAR ENVVAR(CLASSPATH) VALUE(' + t + '.:' +
                  '/OPS_util/Java/jt400.jar:/OPS_util/Java' +
                                              t + ') REPLACE(*YES)';
                  len = %len(%trim(cmd));
                 QcmdExc(cmd:len);
    
                 airstring = makestring(%trim(img));
                  j_mainParam0(1) = airstring;
                     Clear airstring;
    
                 airstring = makestring(' ');
                  j_mainParam0(2) = airstring;
                     Clear airstring;
    
                  main( j_mainParam0);
    
                   label = getBytes(j_mainParam0(2));
    
                     len = %len(%trim(label));
    
                              cmd = 'OVRPRTF FILE(PRT198) '
                                +  'DEVTYPE(*SCS) '
                                +  'OUTQ(' + %trim(Outq) + ')';
    
                       callp QCMDEXC(cmd: %len(cmd));
    
                         buf = %subst(label:x:132);
                          Open PRT198;
                            Dow buf <> *Blanks;
                              except;
                                x += 132;
                                buf = %subst(label:x:132);
                            Enddo;
                          Close PRT198;
                          prt = *on;
             *inlr = *on;
               return;
           /End-Free
          *--------------------------------------------
         Oprt198    e
         o                       buf
          *--------------------------------------------

    Comment


    • OLDMAN25
      OLDMAN25 commented
      Editing a comment
      jesus somebody call 911 my nose gonna bleed

  • #4
    Hello,

    I have this identical requirement of having a .png file that needs to print on a Zebra label printer. I have attempted to use the Java and RPGLE solution posted above but when I try converting a .png file, Java throws an error "javax.imageio.IIOException: Can't create an ImageInputStream!" when calling method "main" with signature "([Ljava.lang.StringV" in class "ZPLConverter". Can someone please provide some detail. Is it the IFS filename of a .png file that is the input parm or does the IFS file need to be in a different format? Is there a specific CCSID required? Thanks in advance for any help.

    Comment

    Working...
    X