TheNH813

IMG2ZPL.java

Aug 11th, 2021 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. # Copyright jcgonzalez: http://www.jcgonzalez.com/java-image-to-zpl-example
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import javax.imageio.ImageIO;
  9. public class ZPLConveter {
  10.     private int blackLimit = 380;
  11.     private int total;
  12.     private int widthBytes;
  13.     private boolean compressHex = false;
  14.     private static Map<Integer, String> mapCode = new HashMap<Integer, String>();
  15.     {
  16.         mapCode.put(1, "G");
  17.         mapCode.put(2, "H");
  18.         mapCode.put(3, "I");
  19.         mapCode.put(4, "J");
  20.         mapCode.put(5, "K");
  21.         mapCode.put(6, "L");
  22.         mapCode.put(7, "M");
  23.         mapCode.put(8, "N");
  24.         mapCode.put(9, "O");
  25.         mapCode.put(10, "P");
  26.         mapCode.put(11, "Q");
  27.         mapCode.put(12, "R");
  28.         mapCode.put(13, "S");
  29.         mapCode.put(14, "T");
  30.         mapCode.put(15, "U");
  31.         mapCode.put(16, "V");
  32.         mapCode.put(17, "W");
  33.         mapCode.put(18, "X");
  34.         mapCode.put(19, "Y");
  35.         mapCode.put(20, "g");
  36.         mapCode.put(40, "h");
  37.         mapCode.put(60, "i");
  38.         mapCode.put(80, "j");
  39.         mapCode.put(100, "k");
  40.         mapCode.put(120, "l");
  41.         mapCode.put(140, "m");
  42.         mapCode.put(160, "n");
  43.         mapCode.put(180, "o");
  44.         mapCode.put(200, "p");
  45.         mapCode.put(220, "q");
  46.         mapCode.put(240, "r");
  47.         mapCode.put(260, "s");
  48.         mapCode.put(280, "t");
  49.         mapCode.put(300, "u");
  50.         mapCode.put(320, "v");
  51.         mapCode.put(340, "w");
  52.         mapCode.put(360, "x");
  53.         mapCode.put(380, "y");        
  54.         mapCode.put(400, "z");            
  55.     }
  56.     public String convertfromImg(BufferedImage image) throws IOException {
  57.         String cuerpo = createBody(image);
  58.         if(compressHex)
  59.            cuerpo = encodeHexAscii(cuerpo);
  60.         return headDoc() + cuerpo + footDoc();        
  61.     }
  62.     private String createBody(BufferedImage orginalImage) throws IOException {
  63.         StringBuffer sb = new StringBuffer();
  64.         Graphics2D graphics = orginalImage.createGraphics();
  65.         graphics.drawImage(orginalImage, 0, 0, null);
  66.         int height = orginalImage.getHeight();
  67.         int width = orginalImage.getWidth();
  68.         int rgb, red, green, blue, index=0;        
  69.         char auxBinaryChar[] =  {'0', '0', '0', '0', '0', '0', '0', '0'};
  70.         widthBytes = width/8;
  71.         if(width%8>0){
  72.             widthBytes= (((int)(width/8))+1);
  73.         } else {
  74.             widthBytes= width/8;
  75.         }
  76.         this.total = widthBytes*height;
  77.         for (int h = 0; h<height; h++)
  78.         {
  79.             for (int w = 0; w<width; w++)
  80.             {
  81.                 rgb = orginalImage.getRGB(w, h);
  82.                 red = (rgb >> 16 ) & 0x000000FF;
  83.                 green = (rgb >> 8 ) & 0x000000FF;
  84.                 blue = (rgb) & 0x000000FF;
  85.                 char auxChar = '1';
  86.                 int totalColor = red + green + blue;
  87.                 if(totalColor>blackLimit){
  88.                     auxChar = '0';
  89.                 }
  90.                 auxBinaryChar[index] = auxChar;
  91.                 index++;
  92.                 if(index==8 || w==(width-1)){
  93.                     sb.append(fourByteBinary(new String(auxBinaryChar)));
  94.                     auxBinaryChar =  new char[]{'0', '0', '0', '0', '0', '0', '0', '0'};
  95.                     index=0;
  96.                 }
  97.             }
  98.             sb.append("\n");
  99.         }
  100.         return sb.toString();
  101.     }
  102.     private String fourByteBinary(String binaryStr){
  103.         int decimal = Integer.parseInt(binaryStr,2);
  104.         if (decimal>15){
  105.             return Integer.toString(decimal,16).toUpperCase();
  106.         } else {
  107.             return "0" + Integer.toString(decimal,16).toUpperCase();
  108.         }
  109.     }
  110.     private String encodeHexAscii(String code){
  111.         int maxlinea =  widthBytes * 2;        
  112.         StringBuffer sbCode = new StringBuffer();
  113.         StringBuffer sbLinea = new StringBuffer();
  114.         String previousLine = null;
  115.         int counter = 1;
  116.         char aux = code.charAt(0);
  117.         boolean firstChar = false;
  118.         for(int i = 1; i< code.length(); i++ ){
  119.             if(firstChar){
  120.                 aux = code.charAt(i);
  121.                 firstChar = false;
  122.                 continue;
  123.             }
  124.             if(code.charAt(i)=='\n'){
  125.                 if(counter>=maxlinea && aux=='0'){
  126.                     sbLinea.append(",");
  127.                 } else     if(counter>=maxlinea && aux=='F'){
  128.                     sbLinea.append("!");
  129.                 } else if (counter>20){
  130.                     int multi20 = (counter/20)*20;
  131.                     int resto20 = (counter%20);
  132.                     sbLinea.append(mapCode.get(multi20));
  133.                     if(resto20!=0){
  134.                         sbLinea.append(mapCode.get(resto20) + aux);    
  135.                     } else {
  136.                         sbLinea.append(aux);    
  137.                     }
  138.                 } else {
  139.                     sbLinea.append(mapCode.get(counter) + aux);
  140.                     if(mapCode.get(counter)==null){
  141.                     }
  142.                 }
  143.                 counter = 1;
  144.                 firstChar = true;
  145.                 if(sbLinea.toString().equals(previousLine)){
  146.                     sbCode.append(":");
  147.                 } else {
  148.                     sbCode.append(sbLinea.toString());
  149.                 }                
  150.                 previousLine = sbLinea.toString();
  151.                 sbLinea.setLength(0);
  152.                 continue;
  153.             }
  154.             if(aux == code.charAt(i)){
  155.                 counter++;                
  156.             } else {
  157.                 if(counter>20){
  158.                     int multi20 = (counter/20)*20;
  159.                     int resto20 = (counter%20);
  160.                     sbLinea.append(mapCode.get(multi20));
  161.                     if(resto20!=0){
  162.                         sbLinea.append(mapCode.get(resto20) + aux);    
  163.                     } else {
  164.                         sbLinea.append(aux);    
  165.                     }
  166.                 } else {
  167.                     sbLinea.append(mapCode.get(counter) + aux);
  168.                 }
  169.                 counter = 1;
  170.                 aux = code.charAt(i);
  171.             }            
  172.         }
  173.         return sbCode.toString();
  174.     }
  175.     private String headDoc(){
  176.         String str = "^XA " +
  177.                         "^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", ";
  178.         return str;
  179.     }
  180.     private String footDoc(){
  181.         String str = "^FS"+
  182.                         "^XZ";        
  183.         return str;
  184.     }
  185.     public void setCompressHex(boolean compressHex) {
  186.         this.compressHex = compressHex;
  187.     }
  188.     public void setBlacknessLimitPercentage(int percentage){
  189.         blackLimit = (percentage * 768 / 100);
  190.     }
  191.     public static void main(String[] args) throws Exception {
  192.         BufferedImage orginalImage = ImageIO.read(new File("/tmp/logo.jpg"));
  193.         ZPLConveter zp = new ZPLConveter();
  194.         zp.setCompressHex(true);
  195.         zp.setBlacknessLimitPercentage(50);        
  196.         System.out.println(zp.convertfromImg(orginalImage));        
  197.     }
  198. }
Add Comment
Please, Sign In to add comment