簡體   English   中英

java.awt.Image.BufferedImage 條形碼發送以在空白處打印結果

[英]java.awt.Image.BufferedImage barcode sent to print results in whitespace

我正在使用 Barcode4J 庫來生成條形碼。 我返回了一個 BufferedImage,我可以在 JLabel 上顯示為 ImageIcon 並保存到文件中,但我無法終生將它打印到打印機上,drawString 和 drawRect 按預期工作,但圖像是空白的。 我在下面發布了一些代碼; 我不知道如何使它成為 SSCCE,但如果有人能指出我的任何方向,我將不勝感激。

    snippet:
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;


public class BarcodeBean extends Code128Bean implements Printable{

    BufferedImage _bImage;

    
    public void createBarcode2Print(BarcodeBean bean, String barcodeValue) throws FileNotFoundException, IOException {

        final int dpi = 150;
        
        BufferedImage image;
        BitmapCanvasProvider canvas = null;

        if (barcodeValue == null) {
            barcodeValue = "0123456789-000-0001";
        }
        
        bean.setModuleWidth(UnitConv.in2mm(2.0f / dpi));

        bean.doQuietZone(false);

        try {

            
            canvas = new BitmapCanvasProvider(
                    dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            

            //Generate the barcode
            bean.generateBarcode(canvas, barcodeValue);

            image = canvas.getBufferedImage();
            
            //Signal end of generation
        //    canvas.finish();
            
        } finally {
            
             canvas.finish();
        }

        
        printImage(image);
        
        int stop = 0;

    }
    
    public void printImage(BufferedImage arg_image){
        
        _bImage = arg_image;
        createLabel();
        
    }



    private void createLabel() {
        
        PrinterJob printJob = PrinterJob.getPrinterJob();
        Book book = new Book();
        
        PageFormat format = new PageFormat();
        
        format.setOrientation(PageFormat.PORTRAIT);
        
        java.awt.print.Paper paper = new java.awt.print.Paper();
        

        paper.setSize(612.0, 792.0); //portrait
        
        double hgt = paper.getHeight();
        double wdth = paper.getWidth();
        
        paper.setImageableArea(0, 0, wdth, hgt);
        
        format.setPaper(paper);
        
        book.append(this, format);
        
        printJob.setPageable(book);

            if (printJob.printDialog()) {
                try {

                    printJob.print();

                } catch (PrinterException ex) {

                    ex.printStackTrace();
                }
            }
   


}

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        
        Graphics2D graphics2d = (Graphics2D)graphics;
        graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        
        graphics.setColor(Color.black);
        graphics2d.setColor(Color.black);
        
        int imageWidth = _bImage.getWidth(null);    // 378
        int imageHeight = _bImage.getHeight(null);  // 110
        
        int imageableWidth = (int)pageFormat.getImageableWidth();   // 612
        int imageableHeight = (int)pageFormat.getImageableHeight(); // 792
        

        
        graphics2d.drawString("Print barcode between this and...", 10, 30);
        
        graphics2d.drawRect(10, 40, imageWidth, imageHeight);
        
        graphics2d.drawImage(_bImage, 10, 60, imageWidth, imageHeight, null);
                
        graphics2d.drawString("This.............................", 10, 180);
                
        graphics2d.dispose();
        return Printable.PAGE_EXISTS;
        
    }
        
}

我不能說我知道為什么,Barcode4J 可能正在使用他們自己的BufferedImage實現或其他東西,但是,如果我復制從 Barcode4J 返回的BufferedImage ,我可以讓它工作。

我的意思是,如果我創建一個新的BufferedImage實例並將 Barcode4J 中的實例繪制到它上面並使用它,我可以讓它工作......

canvas = new BitmapCanvasProvider(
        dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
bean.generateBarcode(canvas, barcodeValue);
BufferedImage bardcode = canvas.getBufferedImage();

image = new BufferedImage(bardcode.getWidth(), bardcode.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.drawImage(bardcode, 0, 0, null);
g2d.dispose();

graphics2d.dispose(); ← 這可能是問題所在。 除非您創建了圖形,否則切勿丟棄它。

真是個好建議,不幸的是,在這種情況下它沒有幫助,但無論如何你應該聽它!

此外,這return Printable.PAGE_EXISTS; ,可以讓你繞圈子跑。 當沒有更多頁面要打印時,您需要告訴調用者。

可運行的示例...

在此處輸入圖像描述 在此處輸入圖像描述

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage barcodeImage;

        public TestPane() throws IOException {
            setLayout(new GridBagLayout());
            barcodeImage = createBarcode2Print(new Code128Bean(), "0123456789-000-0001");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel(new ImageIcon(barcodeImage)), gbc);

            JButton print = new JButton("Print");
            add(print, gbc);

            print.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    printIt();
                }
            });
        }

        protected void printIt() {
            PrinterJob pj = PrinterJob.getPrinterJob();
            if (pj.printDialog()) {
                PageFormat pf = pj.defaultPage();
                Paper paper = pf.getPaper();
                pf.setOrientation(PageFormat.PORTRAIT);
                pf.setPaper(paper);
                PageFormat validatePage = pj.validatePage(pf);
                //                System.out.println("Valid- " + dump(validatePage));
                pj.setPrintable(new BarCodePrintable(barcodeImage), validatePage);
                try {
                    pj.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    public class BarCodePrintable implements Printable {

        private BufferedImage barcodeImage;

        public BarCodePrintable(BufferedImage barcodeImage) {
            this.barcodeImage = barcodeImage;
        }

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            Graphics2D graphics2d = (Graphics2D) graphics;
            graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            graphics.setColor(Color.black);
            graphics2d.setColor(Color.black);

            int imageWidth = barcodeImage.getWidth();    // 378
            int imageHeight = barcodeImage.getHeight();  // 110

            int imageableWidth = (int) pageFormat.getImageableWidth();   // 612
            int imageableHeight = (int) pageFormat.getImageableHeight(); // 792

            graphics2d.drawString("Print barcode between this and...", 10, 30);

            graphics2d.drawRect(10, 40, imageWidth, imageHeight);

            graphics2d.drawImage(barcodeImage, 10, 60, null);

            graphics2d.drawString("This.............................", 10, 180);

            //graphics2d.dispose();
            return pageIndex == 0 ? Printable.PAGE_EXISTS : Printable.NO_SUCH_PAGE;
        }

    }

    public BufferedImage createBarcode2Print(Code128Bean bean, String barcodeValue) throws FileNotFoundException, IOException {
        final int dpi = 300;

        BufferedImage image = null;
        BitmapCanvasProvider canvas = null;

        if (barcodeValue == null) {
            barcodeValue = "0123456789-000-0001";
        }

        bean.setModuleWidth(UnitConv.in2mm(2.0f / dpi));
        bean.doQuietZone(false);

        try {
            canvas = new BitmapCanvasProvider(
                    dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            bean.generateBarcode(canvas, barcodeValue);
            BufferedImage bardcode = canvas.getBufferedImage();

            image = new BufferedImage(bardcode.getWidth(), bardcode.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = image.createGraphics();
            g2d.drawImage(bardcode, 0, 0, null);
            g2d.dispose();
        } finally {
            canvas.finish();
        }

        return image;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM