2013-03-01 6 views
2

누구든지 자바에서 HEX 문자열로 이미지 (예 : * .bmp 파일)를 변환하는 방법을 알고 있으므로 HEX 문자열을^DG 명령 (zpl의 명령 중 하나) .thanks!에서 사용할 수 있습니다!^DG 명령을 사용하여 Java의 얼룩말 프린터로 이미지를 보내는 방법은 무엇입니까?

+0

Zebra 프린터 정확히 주류 기술하지 여기에 인쇄하지 않고 결과를 테스트합니다. 질문에 16 진수 형식 요구 사항을 포함하는 것이 좋습니다. –

+0

다음은 [zpl maual document] (https://km.zebra.com/kb/index?page=content&id=SO6755)에서 16 진수 형식에 대한 설명입니다 (^ DG 명령을 볼 수 있음). – htwj1998

+1

나는 온라인 변환기를 바로 작성했다. http://www.jcgonzalez.com/zpl-ascii-hex-representation-image-generator – PbxMan

답변

0

쉽지 않습니다. 이미지를 픽셀 당 1 비트로 변환 한 다음 행 당 바이트를 포함하여 크기를 계산 한 다음 적절히 구성하여 ~DG 명령의 매개 변수를 저장해야합니다.

또는 ...

얼룩말 GRFs 이미지를 변환하는 API (얼룩말 내부 이미지 유형)를 제공합니다. 내가 자바 예제를 작성했습니다

+0

대단히 감사합니다! 방법 1에 대한 데모를 줄 수 있습니까? – htwj1998

+0

B/W 란 무엇입니까? – htwj1998

3

개발자 데모의 무리이 도움이 프린터에

Zebra Link-OS SDK download

희망을 GRF 같은 이미지를 저장/인쇄하는 방법에 대한 데모를 포함한 패키지에있다 그렇게 할 수 있습니다. ascii 16 진수 코드 또는 압축 ZPL 코드를 생성하고 검정색 백분율을 선택하여 정확도를 향상시킬 수도 있습니다.

그에 따라 주 방법의 매개 변수를 변경하십시오. 더 광범위한 설명 here

*를 들어 enter

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; 
public class ZPLConveter { 
    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 body = createBody(image); 
     if(compressHex) 
      body = encodeHexAscii(body); 
     return headDoc() + body + 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 mod20 = (counter%20); 
        sbLinea.append(mapCode.get(multi20)); 
        if(mod20!=0){ 
         sbLinea.append(mapCode.get(mod20) + 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("/tmp/logo.jpg")); 
     ZPLConveter zp = new ZPLConveter(); 
     zp.setCompressHex(true); 
     zp.setBlacknessLimitPercentage(50);   
     System.out.println(zp.convertfromImg(orginalImage));   
    } 
} 
+0

답변 해 주셔서 감사합니다. 귀하의 코드를 시도했지만 인쇄하는 동안 이미지 크기가 줄어 듭니다. 인쇄하려면 1/3을 사용하고있는 것 같아요. 나는 너비 384 픽셀과 높이 288 픽셀의 이미지를 4/3 비율로 인쇄하려고합니다. – Lokesh