Java中的ESC/POS图像 [英] ESC/POS image in java

查看:356
本文介绍了Java中的ESC/POS图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to print this image in a thermal printer(SAM4S ELLIX40).

我一直在搜索和测试多个代码,但实际上没有任何效果,他们说:使用此命令,您会看到一行",我发送该代码,什么都没有发生.

I've been searching and testing several codes but nothing actually works, they said "use this command and you'll see a line", I send that and nothing happens.

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

I tried this commands from manuals:

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

我最想尝试的是ESC *,但我从不了解如何设置nL和nH.

The one that I most tried was ESC *, but I never understood how to set nL and 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);

推荐答案

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

Question from Pedro (i do not know how to make reference to it): "with this library I'll have to change the way I send the data to the printer in my proyect, right?"

回答您的最后一条评论,我认为混合来自不同库的调用并不完全适合,但是偶尔会发生这种情况……放手,在发送图像之前,我们需要调整Hello World的示例//我想向热敏打印机(ESC/POS)发送问候语,您可以用这种方式编写代码.有了这个种子,您可以根据需要输入新的ESC/POS命令.如果您的旧代码可以嵌入到此示例中,那么您的问题的答案是肯定的,escpos_coffee库可与OutputStream一起使用,并且可以像下面的示例中那样以静默方式嵌入. 此代码使用 escpos_coffee

Answering your last comment, I do not think that is totally apropriate to mix calls from diferent libs, but, occasionally, this can happen... lets go, before sending the image, we need to align the example of hello world // I think that to send a hello word to the thermal printer (ESC / POS) you could write the code this way. With this seed, you can enter new ESC / POS commands as needed. If your legacy code can be embedded in this example, then the answer to your question is yes, the escpos_coffee library works with OutputStream and can be silently embedded as in the following example. this code uses 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);
        }
    }

}

这篇关于Java中的ESC/POS图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆