ESC/POS 图片在 java

ESC/POS image in java

我需要在热敏打印机 (SAM4S ELLIX40) 中打印此图像。

我一直在搜索和测试几个代码,但实际上没有任何效果,他们说 "use this command and you'll see a line",我发送了它,但没有任何反应。

我尝试了手册中的命令:

我试的最多的是ESC *,但是一直不懂nL和nH怎么设置

String text_to_print = "Hello world!";
DocPrintJob dpj = selected_printjob.createPrintJob();
InputStream stream = new ByteArrayInputStream((text_to_pring).getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(stream, flavor, null);
dpj.print(doc, null);

clone escpos-coffee project ,所有代码都有,打印图片可以这样:

        BufferedImage  imageBufferedImage = ImageIO.read(imageURL);

       // this wrapper uses esc/pos sequence: "GS 'v' '0'"
        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
        escpos = new EscPos(new PrinterOutputStream(printService));
        Bitonal algorithm = new BitonalThreshold(); 
        EscPosImage escposImage = new EscPosImage(imageBufferedImage, algorithm);     
        escpos.write(imageWrapper, escposImage);

Pedro 的提问(不知如何引用):"with this library I'll have to change the way I send the data to the printer in my proyect, right?"

回答你最后的评论,我认为混合来自不同库的调用是不完全合适的,但偶尔会发生这种情况......让我们开始吧,在发送图像之前,我们需要对齐示例你好世界//我认为要向热敏打印机(ESC / POS)发送问候语,您可以这样编写代码。使用此种子,您可以根据需要输入新的 ESC / POS 命令。如果您的遗留代码可以嵌入到这个示例中,那么您的问题的答案是肯定的,escpos_coffee 库与 OutputStream 一起工作,并且可以像下面的示例一样静默嵌入。 此代码使用 escpos_coffee

package pedrojoaquim;

import escpos.EscPos;
import escpos.image.BitonalThreshold;
import escpos.image.EscPosImage;
import escpos.image.RasterBitImageWrapper;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PedroJoaquim {

    public void printImage() throws PrintException, IOException{
        String text_to_print = "Hello world!";
        PrintService foundService = PrintServiceLookup.lookupDefaultPrintService();
        DocPrintJob dpj = foundService.createPrintJob();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        /// your legacy commands ini
        // initialize printer
        outputStream.write(27); // ESC
        outputStream.write('@');

        // print text
        outputStream.write(text_to_print.getBytes());

        // feed 5 lines
        outputStream.write(27); // ESC
        outputStream.write('d');
        outputStream.write(5);

        // cut paper
        outputStream.write(29); // GS
        outputStream.write('V');
        outputStream.write(48);

        /// your legacy commands end

        /// fitting lib calls on same outputStream
        EscPos escpos = new EscPos(outputStream);

        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
        BufferedImage  githubBufferedImage = ImageIO.read(new File("/Users/marco/Downloads/smile.jpg"));
        EscPosImage escposImage = new EscPosImage(githubBufferedImage, new BitonalThreshold()); 
        // print smile image...
        escpos.write(imageWrapper, escposImage);        
        // lib end
        /// legacy continues from this point

        // print text
        outputStream.write(" Joaquim's Smile image".getBytes());

        // feed 5 lines
        outputStream.write(27); // ESC
        outputStream.write('d');
        outputStream.write(5);

        // cut
        outputStream.write(29); // GS
        outputStream.write('V');
        outputStream.write(48);


        // do not forguet to close outputstream
        outputStream.close();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());


        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc doc = new SimpleDoc(inputStream, flavor, null);
        dpj.print(doc, null);


    }

    public static void main(String[] args) throws IOException {
        PedroJoaquim obj = new PedroJoaquim();
        try {
            obj.printImage();
        } catch (PrintException ex) {
            Logger.getLogger(PedroJoaquim.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}