简体   繁体   English

Java graphics2d使用证卡打印机将高分辨率图像打印到CR80卡中

[英]Java graphics2d printing high resolution image into CR80 card using Card Printer

I have problem when printing CR80 (Smartcard size) using Java desktop Application. 使用Java桌面应用程序打印CR80(智能卡尺寸)时出现问题。 The card printer named Fargo HDP5000. 证卡打印机名为Fargo HDP5000。 Printer specification is 300dpi. 打印机规格为300dpi。 It can print normally when I resize/rescale the image using java getScaledInstance or imgScalr library into card dimension (53.98mm x 85.6fmm). 当我使用java getScaledInstanceimgScalr库将图像调整大小/缩放为卡片尺寸(53.98mm x 85.6fmm)时,它可以正常打印。 But the result are become poor or pixelated. 但是结果变得很差或像素化。

But when I try to print high resolution image using windows print dialog, the result are very good and sharp. 但是,当我尝试使用Windows打印对话框打印高分辨率图像时,效果非常好而且清晰。

Any solution how to print high resolution image using java application in such small media? 在如此小的介质中,如何使用Java应用程序打印高分辨率图像的解决方案? Thank you 谢谢

here are my code: 这是我的代码:

         try {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintService(printService);

            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

            aset.add(OrientationRequested.PORTRAIT);

            aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));

            aset.add(new MediaPrintableArea(0.0f, 0.0f, 53.98f, 85.6f, MediaSize.MM));


            if (duplex) {
                aset.add(Sides.DUPLEX);
                mDuplex = true;
            } else {
                aset.add(Sides.ONE_SIDED);
            }

            printJob.setPrintable(this);
            printJob.print(aset);
        } catch (Exception PrintException) {
            Helper.errorHandling(TAG, "printDemo", PrintException.toString());
        }

implementation: 执行:

@Override
public int print(Graphics g, PageFormat pageFormat, int pageNumber) {


    if (pageNumber > 1) {
        System.out.println("Print job has been sent to spooler.");
        return Printable.NO_SUCH_PAGE;
    }


    Graphics2D graphics2D = (Graphics2D) g;
    graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());        

    final int printerDPI = 300;

    double pageWidth = pageFormat.getImageableWidth() * printerDPI / POINTS_PER_INCH; //PPI = 72
    double pageHeight = pageFormat.getImageableHeight() * printerDPI / POINTS_PER_INCH; 


    if (pageNumber == 0) {           
        final int x= 0;
        final int y= 0;

        final int FIXED_WIDTH = 504;
        final int FIXED_HEIGHT = 808;


        BufferedImage bimg;
        try {
            bimg = ImageIO.read(new File(filenamePath));
        } catch (Exception e) {
            Helper.errorHandling(e.toString());
            bimg = null;
        }

        if (bimg == null) {
            return;
        }


        int w = bimg.getWidth();
        int h = bimg.getHeight();
        int destW = (int) (w * 0.3) + x;
        int destH = (int) (h * 0.3) + y;

        if (w > pageWidth) {
            destW = pageWidth;
        }

        if (h > pageHeight) {
            destH = pageHeight;
        }


        Image img;
        if (w != FIXED_WIDTH && h != FIXED_HEIGHT) {
            img = bimg.getScaledInstance(FIXED_WIDTH, FIXED_HEIGHT, Image.SCALE_SMOOTH);
            // or
            img = Scalr.resize(bimg, Scalr.Method.ULTRA_QUALITY, FIXED_WIDTH, FIXED_HEIGHT);
            graphics2D.drawImage(img, x, y, destW, destH, 0, 0, w, h, null);
        } else {
            graphics2D.drawImage(bimg, x, y, destW, destH, 0, 0, w, h, null);
        }


        return (Printable.PAGE_EXISTS);
    } else {
        return (NO_SUCH_PAGE);
    }
}

It is because the: 这是因为:

graphics2D.drawImage(img, x, y, destW, destH, 0, 0, w, h, null);

will also do scale the img to fit the destW -x and destH-y if they are different from the image size - this scaling produces the pixelized and blurry effect. 如果它们与图像大小不同,还将对img进行缩放以适合destW -xdestH-y这种缩放会产生像素化和模糊效果。 You can set better interpolation options on the graphics2D using: 您可以使用以下方法在graphics2D上设置更好的插值选项:

graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC)

But I don't recommend this way, as the graphics2D interpolations aren't the best one that you can possibly use. 但我不推荐这种方式,因为graphics2D插值并不是您可以使用的最佳插值方法。 In order to achieve best possible results (crispiest image) the size of the image rendered on Graphics2D should be pixel to pixel match with the real image size. 为了获得最佳效果(最清晰的图像),在Graphics2D渲染的图像的大小应与实际图像大小相匹配。 You can draw the img to be a pixel to pixel match with graphics2D using this simple method: 您可以使用以下简单方法将img绘制为与graphics2D匹配的像素:

graphics2D.drawImage(img, x, y, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM