小编典典
来自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);
}
}
}
2020-11-23