2016-05-16 1 views
2

En utilisant Esc/Pos, je veux télécharger une image bitmap à la mémoire graphique NV sur une imprimante. J'utilise GS (L/GS 8 L<Function 67> du manuel Esc/Pos.JAVA ESC Pos télécharger le graphique NV à l'imprimante

Je peux utiliser <Function 65> et <Function 66> pour supprimer tout ou partie des graphiques.

Je sais qu'il me manque quelque chose lors de l'ajout du Bitmap à la fonction.

Heres est ma chaîne de commande incluant le bitmap. Le bitmapString a l'en-tête d'en-tête de fichier et les informations de l'Bitmap supprimé (62 premiers octets) (DataOffset) .:

String bitmapString = new String(bitmapBytes, Charsets.US_ASCII); 
bitmapString = bitmapString.substring(DataOffset, bitmapStringSize); 
String commandString = ""; 

int commandLength = (bitmapStringSize.length) + 11; 
    pL = commandLength % 256; 
    if (commandLength < 256) { 
     pH = 0; 
    } else { 
     pH = (commandLength - pL)/256; 
    } 

    xL = bitmapWidth % 256; 
    if (bitmapWidth < 256) { 
     xH = 0; 
    } else { 
     xH = (bitmapWidth - (bitmapWidth % 256))/256; 
    } 

    yL = bitmapHeight % 256; 
    if (bitmapHeight < 256) { 
     yH = 0; 
    } else { 
     yH = (bitmapHeight - (bitmapHeight % 256))/256; 
    } 

    commandString 
      += Utils.H("1B")// 27 
      + Utils.H("40") // 64 
      + Utils.H("1B") // 27 
      + Utils.H("3D") // 61 
      + Utils.H("01") // 1 

      + Utils.H("1D") // GS = 29 
      + Utils.H("28") // (= 40 
      + Utils.H("4C") // L = 76 

      + Utils.D(pL) // pL 
      + Utils.D(pH) // pH 

      + Utils.H("30") // m = 48 
      + Utils.H("43") // fn = 67 
      + Utils.H("30") // a = 48 

      + Utils.H(KC1) // kc1 
      + Utils.H(KC2) // kc2 

      + Utils.H("01") // b = 1 

      + Utils.D(xL) // xL 
      + Utils.D(xH) // xH 
      + Utils.D(yL) // yL 
      + Utils.D(yH) // yH 

      + Utils.H("31");// c = 49 

    commandString += bitmapString; 

J'utilise ePOS-Print.jar pour ouvrir et écrire à l'imprimante:

EpsonIo epsonio = new EpsonIo(); 
byte[] commandBytes = commandString.getBytes(Charsets.US_ASCII); 

     epsonio.open(DevType.BLUETOOTH, MAC, null, ESCPosService.this); 

     while (n > 0) { 

      epsonio.write(commandBytes, i, n > bufferSize ? bufferSize : n, SEND_TIMEOUT); 

      Thread.sleep(450); 

      i += bufferSize; 
      n -= bufferSize; 
     } 

Mais quand j'imprimer le graphique, il est déformé:

enter image description here

Répondre

2

Je l'ai résolu la question.

Les octets bitmap envoyés ont dû être décodés à l'aide de la méthode ci-dessous. La méthode convertit les pixels bitmap en octets monochromes. 1 ou 0.

Espérons que cette aide est quelqu'un si le futur!

public static byte[] decodeBitmap(byte[] bitmapBytes) { 

    Bitmap bmp = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length); 

    int zeroCount = bmp.getWidth() % 8; 
    String zeroStr = ""; 
    if (zeroCount > 0) { 
     for (int i = 0; i < (8 - zeroCount); i++) { 
      zeroStr = zeroStr + "0"; 
     } 
    } 

    List<String> list = new ArrayList<>(); 
    for (int i = 0; i < bmp.getHeight(); i++) { 
     StringBuilder sb = new StringBuilder(); 
     for (int j = 0; j < bmp.getWidth(); j++) { 
      int color = bmp.getPixel(j, i); 

      int r = (color >> 16) & 0xff; 
      int g = (color >> 8) & 0xff; 
      int b = color & 0xff; 

      // if color close to white,bit='0', else bit='1' 
      if (r > 160 && g > 160 && b > 160) 
       sb.append("0"); 
      else 
       sb.append("1"); 
     } 
     if (zeroCount > 0) { 
      sb.append(zeroStr); 
     } 

     list.add(sb.toString()); 
    } 

    List<String> bmpHexList = binaryListToHexStringList(list); 
    List<String> commandList = new ArrayList<>(); 
    commandList.addAll(bmpHexList); 

    return hexListToBytes(commandList); 
}