0

Lately i've been experimenting with printing images. In photoshop i'm able to resize(downsize) images by giving measurements in millimeters and a dpi value (my printer claims it can handle up to 2400 dpi so i set it to that). this gives amazing sharp results in exactly the right size when i print it with Photoshop.

However this is all done manually and quite labour intensive for large quantities of images so i want to be able to do this programmatically

The result i've got this far include huge cut-off versions of the images or small (but not even close to the desired size) and still bad quality (as if it did resize but kept low dpi)

I'm not sure i'm moving in the right direction. Is there something wrong with my code? should i try something completely different?

I've tried the methods(and tried altering them) in this post: java setting resolution and print size for an Image

private static class MyPrintJob{
    static PrinterJob myJob;
    static double dpi;
    static double dpCm;
    static Paper myPaper;
    static PageFormat myPageFormat;
    static double cardHeightInCm;
    static double cardWidthInCm;

    static ArrayList<BufferedImage> imageList;

   MyPrintJob(){
        myJob = PrinterJob.getPrinterJob();
        dpi = 2400;
        dpCm = dpi/2.54;

        myPaper = new Paper();
        myPaper.setSize(21.3 * dpCm, 29.7 * dpCm);
        myPaper.setImageableArea(0, 0, 21.3 * dpCm, 29.7 * dpCm);

        myPageFormat = myJob.defaultPage();//new PageFormat();
        myPageFormat.setPaper(myPaper);

        imageList = new ArrayList<>();

        cardHeightInCm = 8;
        cardWidthInCm = 6;

    }

    static void resizeForDpi(BufferedImage input, Graphics2D g, double dpi){
        BufferedImage theImage = input;
        AffineTransform theAT = g.getTransform();

        double theScaleFactor = (72d / dpi);
        g.scale(theScaleFactor, theScaleFactor);
        g.drawRenderedImage(theImage, null);
        g.setTransform(theAT);
    }


    void execute(){
        myJob.setPrintable(new PrintableJob(), myPageFormat);

        if (myJob.printDialog()) {
            try {
                HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
                PrinterResolution pr = new PrinterResolution((int) (dpCm), (int) (dpCm), ResolutionSyntax.DPCM);//was dpi
                set.add(pr);
                myJob.setJobName("Jobname");
                myJob.print(set);
            } catch (PrinterException e) {
            }
        }

    }

    static class PrintableJob implements Printable{

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            Graphics2D g2 = (Graphics2D) graphics;
            int totalPages = 1;
            if (pageIndex < totalPages) {
                g2.setColor(Color.LIGHT_GRAY);

                System.out.println("dpcm: " + dpCm + "dpi: " + dpi);
                System.out.println("should draw rect");

                // Draw Page Header
                //g2.fillRect(0, 0, (int)(dpCm), (int)(dpCm));
                BufferedImage printCard = resizeToCard(activeCard.myImage);

                resizeForDpi(printCard,g2,2400);
                g2.drawImage(printCard,20,20,(int)(cardWidthInCm*dpCm),(int)(cardHeightInCm*dpCm),null);
                saveImageForDebug(printCard,"printCard");


                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }
    }


}

0 Answers0