• Java调用打印机打印指定路径图片


    依赖 javax.print

    package com.xgt.util;
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    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;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    import javax.print.attribute.standard.MediaPrintableArea;
    import javax.print.attribute.standard.OrientationRequested;
    import javax.print.attribute.standard.PrintQuality;
    
    public class PrintUtil {
        private static final Logger logger = LoggerFactory.getLogger(PrintUtil.class);
        public static void drawImage(String fileName, int count) {
            FileInputStream fin = null;
            try {
                DocFlavor dof = null;
                if (fileName.endsWith(".gif")) {
                    dof = DocFlavor.INPUT_STREAM.GIF;
                } else if (fileName.endsWith(".jpg")) {
                    dof = DocFlavor.INPUT_STREAM.JPEG;
                } else if (fileName.endsWith(".png")) {
                    dof = DocFlavor.INPUT_STREAM.PNG;
                }
    
                PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
    
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(OrientationRequested.PORTRAIT);
    
                pras.add(new Copies(count));
                pras.add(PrintQuality.HIGH);
                DocAttributeSet das = new HashDocAttributeSet();
    
                // 设置打印纸张的大小(以毫米为单位)
                das.add(new MediaPrintableArea(0, 0, 210, 296, MediaPrintableArea.MM));
                fin = new FileInputStream(fileName);
    
                Doc doc = new SimpleDoc(fin, dof, das);
    
                DocPrintJob job = ps.createPrintJob();
    
                job.print(doc, pras);
                fin.close();
                logger.info("打印成功!文件:"+fileName+"数量为:"+count);
            } catch (IOException ie) {
                ie.printStackTrace();
            } catch (PrintException pe) {
                pe.printStackTrace();
            }finally {
                IOUtils.closeQuietly(fin);
            }
        }
    
        public static void main(String[] args) {
            new PrintUtil().drawImage("D:\QRCodePng\合成图.png", 1);
        }
    
    }
  • 相关阅读:
    深入理解java:2.3.1. 并发编程concurrent包 之Atomic原子操作(循环CAS)
    深入理解java:2.3. 并发编程 java.util.concurrent包
    深入理解java:2.2. 同步锁Synchronized及其实现原理
    深入理解java:2.1. volatile的使用及其原理
    深入理解java:2. 多线程机制
    深入理解java:1.3.2 JVM监控与调优
    深入理解java:1.3.1 JVM内存区域的划分(运行时数据区)
    深入理解java:1.3. 垃圾收集
    深入理解java:1.2. 字节码执行引擎
    线程的等待与唤醒,实现if...else里面的值交互依次输出
  • 原文地址:https://www.cnblogs.com/Java-Starter/p/9283575.html
Copyright © 2020-2023  润新知