【问题标题】:Java graphics2d printing high resolution image into CR80 card using Card PrinterJava graphics2d 使用卡片打印机将高分辨率图像打印到 CR80 卡中
【发布时间】:2023-03-11 04:01:01
【问题描述】:

我在使用 Java 桌面应用程序打印 CR80(智能卡尺寸)时遇到问题。证卡打印机名为 Fargo HDP5000。打印机规格为 300dpi。当我使用 java getScaledInstanceimgScalr 库将图像调整为卡片尺寸(53.98mm x 85.6fmm)时,它可以正常打印。但结果会变差或像素化。

但是当我尝试使用 windows 打印对话框打印高分辨率图像时,结果非常好且清晰。

任何解决方案如何在如此小的媒体中使用 java 应用程序打印高分辨率图像? 谢谢

这是我的代码:

         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());
        }

实现:

@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);
    }
}

【问题讨论】:

标签: java windows image printing smartcard


【解决方案1】:

这是因为:

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

还会缩放img 以适应destW -xdestH-y(如果它们与图像大小不同) - 这种缩放会产生像素化和模糊效果。您可以使用以下方法在 graphics2D 上设置更好的插值选项:

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

但我不推荐这种方式,因为 graphics2D 插值并不是您可以使用的最好的插值方式。为了获得最佳结果(最清晰的图像),Graphics2D 上渲染的图像大小应该与真实图像大小像素到像素匹配。您可以使用以下简单方法将img 绘制为与 graphics2D 的像素到像素匹配:

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

【讨论】:

  • 如果我已经在没有缩放的情况下绘制了像素到像素匹配的图像,你知道如何适应卡片尺寸吗? (53.98 毫米 x 85.6 毫米)?谢谢
  • 只需使用一些好的算法先将其缩放到合适的大小,然后以“像素到像素”模式绘制缩放后的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多