2014-01-24 3 views
3

이미지를 프린터에 다운로드하지 않고도 네트워크를 통해 Zebra 프린터에서 CPCL을 사용하여 PCX 이미지를 인쇄하는 방법을 이해하는 데 많은 시간을 할애했습니다.CPCL을 사용하여 Zebra 프린터에 PCX 이미지 인쇄

설명서의 샘플은 제 생각에는 꽤 모호합니다.

이미지를 단순히 인쇄하는 방법을 보여주기 위해 샘플 클래스를 첨부합니다. 클래스 경로에 "zebra.pcx"이미지가 필요합니다.

희망이 있습니다.

답변

2
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.Socket; 

public class PrintZebraPCXImage { 

    public static void main(String[] args) throws Exception { 
     PrintZebraPCXImage instance = new PrintZebraPCXImage(); 
     instance.print("192.168.1.133", 6101); 
    } 

    public void print(String address, int port) throws Exception { 
     Socket socket = null; 
     DataOutputStream stream = null; 

     socket = new Socket(address, port); 

     try { 
      stream = new DataOutputStream(socket.getOutputStream()); 
      ByteArrayOutputStream bos = readFileToString(this.getClass().getClassLoader().getResourceAsStream("zebra.pcx")); 

      stream.writeBytes("! 0 200 200 300 1\r\n"); 
      stream.writeBytes("PCX 20 0\r\n"); 

      stream.write(bos.toByteArray()); 
      stream.writeBytes("PRINT\r\n"); 

     } finally { 
      if (stream != null) { 
       stream.close(); 
      } 
      if (socket != null) { 
       socket.close(); 
      } 
     } 
    } 

    public ByteArrayOutputStream readFileToString(InputStream is) { 
     InputStreamReader isr = null; 
     ByteArrayOutputStream bos = null; 
     try { 
      isr = new InputStreamReader(is); 
      bos = new ByteArrayOutputStream(); 

      byte[] buffer = new byte[2048]; 
      int n = 0; 
      while (-1 != (n = is.read(buffer))) { 
       bos.write(buffer, 0, n); 
      } 

      return bos; 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } finally { 
      if (isr != null) { 
       try { 
        isr.close(); 
       } catch (IOException e) { 
       } 
      } 
      if (is != null) { 
       try { 
        is.close(); 
       } catch (IOException e) { 
       } 
      } 
     } 
    } 
} 
관련 문제