web-dev-qa-db-fra.com

Comment utiliser une imprimante thermique (USB/Ethernet) sur Android, sans utiliser le kit de développement logiciel du fournisseur?

J'ai déjà implémenté EPSON SDK (pour Bluetooth) et je fonctionne bien, mais je ne travaille pas sur d'autres imprimantes. Il existe un moyen général de le réaliser. qu'est-ce que la commande ESC, comment ça marche?

5
Anu Martin

Trouvez this one.Il contribuera à résoudre votre problème. La référence de commande ESC/POS fournit des informations détaillées sur les commandes ESC/POS, telles que la syntaxe et le protocole standard. Il s’adresse aux programmeurs souhaitant contrôler l’imprimante à l’aide des commandes ESC/POS.

La référence de commande ESC/POS est fournie en remplacement de ESC/POS APG pour les imprimantes à rouleau de papier. Le fichier ESC/POS APG pour imprimantes à rouleau de papier ne sera donc plus mis à jour. La référence de commande ESC/POS contient les informations de commande pour les modèles standard tels que le modèle ANK ou japonais, et peut contenir des modèles chinois ou sud. Modèles asiatiques. Les autres modèles, tels que ceux personnalisés, peuvent prendre en charge différentes commandes ou avoir une plage ou des valeurs par défaut différentes pour les paramètres de commande. S'il vous plaît se référer à chaque spécification de produit pour eux.

Utilisez le code ci-dessous
Remarque: vous pouvez utiliser un objet OutPutStream pour écrire une imprimante, que ce soit en Bluetooth, en Ethernet ou en wifi. 

public class PrinterConstants {
public static int PORT=9100,TOTAL_CHAR=45,DIV1=10,DIV2=5,DIV3=10,DIV4=10,DIV5=10;
public static String IP="192.168.1.35";
private OutputStream printer;
public static final String UUID="00001101-0000-1000-8000-00805f9b34fb";
public PrinterConstants(OutputStream printer){
    this.printer=printer;
}

public void printString(String str) throws IOException {
    Log.i("PRINTER_PRE",str);
    printer.write(str.getBytes());
    printer.write(0xA);
    printer.flush();
}
public void storeString(String str) throws IOException {
    printer.write(str.getBytes());

    printer.flush();
}
public void printStorage() throws IOException {
    printer.write(0xA);

    printer.flush();
}
public void feed(int feed) throws IOException {
    //escInit();
    printer.write(0x1B);
    printer.write("d".getBytes());
    printer.write(feed);printer.flush();

}
public void printAndFeed(String str, int feed) throws IOException {
    //escInit();
    printer.write(str.getBytes());
    printer.write(0x1B);
    printer.write("d".getBytes());
    printer.write(feed);printer.flush();

}
public void setBold(Boolean bool) throws IOException {
    printer.write(0x1B);
    printer.write("E".getBytes());
    printer.write((int)(bool?1:0));printer.flush();
}
/**
 * Sets white on black printing
 * */
public void setInverse(Boolean bool) throws IOException {
    bool=false;
    printer.write(0x1D);
    printer.write("B".getBytes());
    printer.write( (int)(bool?1:0) );printer.flush();

}
public void resetToDefault() throws IOException {
    setInverse(false);
    setBold(false);
    setUnderline(0);
    setJustification(0);printer.flush();
}
/**
 * Sets underline and weight
 *
 * @param val
 *      0 = no underline.
 *      1 = single weight underline.
 *      2 = double weight underline.
 * */
public void setUnderline(int val) throws IOException {
    printer.write(0x1B);
    printer.write("-".getBytes());
    printer.write(val);printer.flush();
}
/**
 * Sets left, center, right justification
 *
 * @param val
 *      0 = left justify.
 *      1 = center justify.
 *      2 = right justify.
 * */

public void setJustification(int val) throws IOException {
    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(val);
    printer.flush();
}
public void setLeftRight(String left,String right) throws IOException {
    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(0);
    printString(left);

    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(2);
    printString(right);

    printer.flush();
}
public void printBarcode(String code, int type, int h, int w, int font, int pos) throws IOException {

    //need to test for errors in length of code
    //also control for input type=0-6

    //GS H = HRI position
    printer.write(0x1D);
    printer.write("H".getBytes());
    printer.write(pos); //0=no print, 1=above, 2=below, 3=above & below

    //GS f = set barcode characters
    printer.write(0x1D);
    printer.write("f".getBytes());
    printer.write(font);

    //GS h = sets barcode height
    printer.write(0x1D);
    printer.write("h".getBytes());
    printer.write(h);

    //GS w = sets barcode width
    printer.write(0x1D);
    printer.write("w".getBytes());
    printer.write(w);//module = 1-6

    //GS k
    printer.write(0x1D); //GS
    printer.write("k".getBytes()); //k
    printer.write(type);//m = barcode type 0-6
    printer.write(code.length()); //length of encoded string
    printer.write(code.getBytes());//d1-dk
    printer.write(0);//print barcode

    printer.flush();
}

public void beep() throws IOException {
    printer.write(0x1B);
    printer.write("(A".getBytes());
    printer.write(4);
    printer.write(0);
    printer.write(48);
    printer.write(55);
    printer.write(3);
    printer.write(15);printer.flush();
}

public void setLineSpacing(int spacing) throws IOException {

    //function ESC 3
    printer.write(0x1B);
    printer.write("3".getBytes());
    printer.write(spacing);

}
public void cut() throws IOException {
    printer.write(0x1D);
    printer.write("V".getBytes());
    printer.write(48);
    printer.write(0);printer.flush();
}
}

en utilisant ci-dessus, vous pouvez écrire des commandes ESC/POS dans le flux de sortie directement 

6
NITHIN VARGHESE

Vous avez trouvé et créé une implémentation pour différentes variantes d’imprimantes. La plupart sont compatibles avec d’autres, ce ne sera donc pas si difficile (vous devrez en plus copier les kits de développement du fournisseur).

Puis créez une interface que les deux implémentations utiliseront telles que initialize, scan, printText, printImage, printBarCode

Lire l'appareil comme si ...

static String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(manufacturer)) {
            return capitalize(model);
        } else {
            return capitalize(manufacturer) + " " + model;
        }
    }

Et utilisez les résultats pour déterminer quelle implémentation utiliser avant de choisir celle qui fonctionne sur la plupart des périphériques. Une interface vous fera bientôt oublier les ennuis que vous avez traversés.

une commande ESC , n’est qu’une instruction adressée à l’imprimante; elles sont presque identiques à la plupart des périphériques ... elles servent à commencent une nouvelle ligne, alignent le texte, les caractères gras, etc. Pensez comme les balises html (fort, h1, centre), car vous les mélangez avec le texte que vous allez imprimer, ce qui facilite grandement la création d’impressions superbes. 

1
konzo