驱动安装

佳博打印机

在佳博官网下载驱动包,要和自己的机型匹配。我使用的是GP-3120TU打印机,驱动包如下:

java 打印stack java 打印机驱动调用_Image

点击安装,选择GP-3120TU

java 打印stack java 打印机驱动调用_Image_02


设置打印机端口

使用连接线选择usb端口,完成后可以打印空白页进行测试。刚开始的时候因为找不到连接线,我使用笔记本蓝牙进行连接,选用串口com4连接,也可以进行打印。

得力打印机

下载驱动,配置端口,安装驱动。

后台

pom.xml

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.52</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- 谷歌二维码-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

二维码

public class MatrixToImageWriterWithLogo {
    //日志
    private static Logger log = Logger.getLogger(MatrixToImageWriterWithLogo.class);
    private static final int BLACK=0XFF000000;
    private static final int WHITE=0XFFFFFFFF;
    private MatrixToImageWriterWithLogo(){ }

    public static BufferedImage toBufferedImage(BitMatrix matrix){
                  BufferedImage bi=new BufferedImage(matrix.getWidth(), matrix.getHeight(), BufferedImage.TYPE_INT_RGB);
                  for(int i=0;i<matrix.getWidth();i++){
                          for(int j=0;j<matrix.getHeight();j++){
                                 //有值的是黑色,没有值是白色
                                 bi.setRGB(i, j,matrix.get(i, j)?BLACK:WHITE);
                            }
                     }
                 return bi;
            }

     public static void MatrixToImage(BitMatrix matrix,String format,File f) {
         BufferedImage b = null;
         try {
             b = ImageIO.read(new File("C:\\Users\\Asus\\Desktop\\z.png"));
             Image image = b.getScaledInstance(10, 10, Image.SCALE_FAST);
             BufferedImage bi = toBufferedImage(matrix);
             //获取二维码画刷
             Graphics g = bi.getGraphics();
             //定位
             g.drawImage(image, 250, 150, null);
             //二维码画到相应文件位置,结束。
             if (ImageIO.write(bi, format, f)) {

             }
             log.info("OK");
         } catch (IOException e) {
            log.error(e.getMessage());
         }
     }

}

生成的二维码图片:

java 打印stack java 打印机驱动调用_Image_03


打印实体类

public class UserTicket implements Printable {
    private String tit1;
    private String con1;
    private String tit2;
    private String con2;
    private BufferedImage image;

    private Font font;

    public UserTicket(String tit1, String con1, String tit2, String con2, BufferedImage image) {
        super();
        this.tit1 = tit1;
        this.con1 = con1;
        this.tit2 = tit2;
        this.con2 = con2;
        this.image = image;

    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex){
        int right = 115;        //右边一张距离左边的距离
        int first_second = 0;   //第一行与第二行的距离
        // 转换成Graphics2D 拿到画笔
        Graphics2D g2 = (Graphics2D) graphics;
        // 设置打印颜色为黑色
        g2.setColor(Color.black);

        // 打印起点坐标
        double x = pageFormat.getImageableX();
        double y = pageFormat.getImageableY();
        // 设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
        font = new Font("宋体", Font.BOLD, 10);
        g2.setFont(font);// 设置字体
        float heigth = font.getSize2D();// 字体高度
        // 标题 第一行

        g2.drawString("杭州公司资产", (float) x+11, (float) y + heigth+5);
        g2.drawImage(image,null,0,20);

        float line = 2 * heigth + first_second;


        g2.scale(1, 1.5);            //对接下来的内容(字体)进行拉高
        font = new Font("宋体", Font.BOLD, 5);
        g2.setFont(font);// 设置字体
        heigth = font.getSize2D();// 字体高度
        // 第二行
        g2.drawString(tit1, (float) x+50, (float) y + line+5);

        line += 1.3 * heigth;

        g2.drawString(con1, (float) x + 50, (float) y + line+5);

        line += 1.3 * heigth;

        font = new Font("宋体", Font.BOLD, 5);
        g2.setFont(font);// 设置字体
        heigth = font.getSize2D();// 字体高度

        // 第二行:地址
        g2.drawString(tit2, (float) x+50, (float) y + line+5);
        line += 1.3 * heigth;
       g2.drawString(con2, (float) x + 50, (float) y + line+5);

        /*line += heigth;
       g2.scale(1, 0.67);              //将比例还原
        // 第三行:广告语
        font = new Font("宋体", Font.BOLD, 5);
        g2.setFont(font);// 设置字体
        g2.drawString("杭州思软信息技术有限公司", (float) x, (float)y+line+30);*/

        switch (pageIndex) {
            case 0:
                return PAGE_EXISTS;
            default:
                return NO_SUCH_PAGE;

        }
    }
}

测试类

public class UserTest {
    private static Logger log = Logger.getLogger(MatrixToImageWriterWithLogo.class);     //日志
    private static final int printNum=1;    //设置打印次数
    public static void PrintUser(String tit1, String con1, String tit2, String con2, BufferedImage image) {
        try {
            // 通俗理解就是书、文档
            Book book = new Book();
            // 设置成竖打
            PageFormat pf = new PageFormat();
            pf.setOrientation(PageFormat.PORTRAIT);
            // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
            Paper paper = new Paper();
            paper.setSize(230, 85);// 纸张大小
            paper.setImageableArea(10, 3, 210, 75);
            pf.setPaper(paper);
            book.append(new UserTicket(tit1,con1,tit2,con2,image), pf);

            // 获取打印服务对象
            PrinterJob job = PrinterJob.getPrinterJob();
            // 设置打印类
            job.setPageable(book);
            job.print();
        } catch (PrinterException e) {
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        BufferedImage image =null;
        try {
            MultiFormatWriter mulwriter = new MultiFormatWriter();
            //写入二维码的内容,也可以是链接,xml,json字符串
            String content = "ihzsr";
            //对EncodeHintType的各种设置写入一个map中
            HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
            //设置我们放入的字符串编码为utf-8
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //长宽,二维码为QR码
            BitMatrix bitmatrix= mulwriter.encode(content, BarcodeFormat.QR_CODE, 60, 60, hints);
            MatrixToImageWriterWithLogo.MatrixToImage(bitmatrix, "png", new File("D:\\z.png"));
            image = ImageIO.read(new FileInputStream(new File("D:\\z.png")));
        } catch (Exception e) {
           log.error(e.getMessage());
        }
        //打印
        for(int i=0;i<printNum;i++){
            PrintUser("资产编号:","HZSR156394562","资产名称:","tjigh电脑主机",image);
        }

    }
}