package shop.alien.util.print;

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.Sides;
import java.awt.print.PrinterJob;
import java.io.*;

/**
 * 打印图片
 *
 * @author ssk
 * @version 1.0
 * @date 2020/8/26 16:39
 */
public class PrintImgUtil {

    public static void main(String[] argv) {
        String printFilePath = "C:\\Users\\17238\\Pictures\\斗图\\新建文件夹\\u=3097978604,2848113486&fm=15&gp=0 (1).jpg";
        //打印机名包含字串
        String printerName = "HP LaserJet Pro MFP M125-M126 PCLmS";
        jpgPrint(printFilePath, printerName);
    }

    /**
     * 打印图片,传入文件和打印机名称
     *
     * @param printFilePath 要打印的文件路径
     * @param printerName   打印机名
     */
    public static boolean jpgPrint(String printFilePath, String printerName) {
        boolean flag = false;
        File file = new File(printFilePath);
        InputStream fis = null;
        try {
            // 设置打印格式，如果未确定类型，可选择autosense
            DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
            // 设置打印参数
            PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
            //打印份数
            printRequestAttributeSet.add(new Copies(1));
            //纸张大小
            printRequestAttributeSet.add(MediaSizeName.ISO_A4);
            //装订
            // aset.add(Finishings.STAPLE);
            //单双面
            printRequestAttributeSet.add(Sides.DUPLEX);
            // 定位打印服务
            PrintService printService = null;
            if (printerName != null) {
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if (printServices == null || printServices.length == 0) {
                    System.out.print("打印失败，未找到可用打印机，请检查。");
                    return false;
                }
                //匹配指定打印机
                for (PrintService service : printServices) {
                    System.out.println(service.getName());
                    if (service.getName().contains(printerName)) {
                        printService = service;
                        break;
                    }
                }
                if (printService == null) {
                    System.out.print("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
                    return false;
                }
            }
            // 构造待打印的文件流
            fis = new FileInputStream(file);
            Doc doc = new SimpleDoc(fis, flavor, null);
            // 创建打印作业
            DocPrintJob job = null;
            if (printService != null) {
                job = printService.createPrintJob();
            }
            if (job != null) {
                job.print(doc, printRequestAttributeSet);
            }
        } catch (FileNotFoundException | PrintException e1) {
            e1.printStackTrace();
            flag = false;
        } finally {
            // 关闭打印的文件流
            if (fis != null) {
                try {
                    fis.close();
                    flag = true;
                } catch (IOException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
        }
        return flag;
    }
}