Java中的ESC / POS图像

时间:2018-09-27 22:08:48

标签: java printing thermal-printer escpos

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

enter image description here

我一直在搜索和测试几个代码,但实际上没有任何作用,他们说:“使用此命令,您会看到一行”,我将其发送,没有任何反应。

我从手册中尝试了以下命令:

  • ESC * m nL nH d1…dk
  • GS * x y d1…dk

我最想尝试的是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);

2 个答案:

答案 0 :(得分:1)

Pedro的问题(我不知道如何引用它):“有了这个库,我将不得不更改将数据发送到我的Proyect中的打印机的方式,对吗?”

回答您的最后一条评论,我认为这完全不适合混合来自不同库的调用,但是,偶尔会发生这种情况...放手,在发送图像之前,我们需要调整Hello World的示例//我想向热敏打印机(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);
        }
    }

}

答案 1 :(得分:0)

克隆escpos-coffee项目,包含所有代码,可以按照以下方式制作打印图像:

        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);