简体   繁体   中英

IText - BarcodeQRCode making the background color transparent

I'm trying to set the background color of my QR Code using iText into a transparent background, however it does not work. Shows only white bars and black background.

What i have done so far:

My Code Snippet:

    PdfContentByte cb = writer.getDirectContent();
    BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
    java.awt.Image qrImage = qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
    Image finalImage = Image.getInstance(writer, qrImage, 1);
    finalImage.setAbsolutePosition(positionX, positionY);
    cb.addImage(finalImage);

I have already generated my QR code and produced a PDF, however, when using

qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));

It does not produce an alpha background, instead it only shows a black background color.

I have also tried:

java.awt.Image qrImage = qrcode.createAwtImage(Color.WHITE,Color.OPAQUE);

But obviously, my arguments are incorrect.

Help will be most appreciated, i've been working on this for a day now.

I have also tried Graphics, Graphics2g, converting it into BufferedImage.

Changing the assignment of finalImage to the following works:

Image finalImage = Image.getInstance(qrImage, null)

I don't know why using the getInstance method that takes a PdfWriter as first argument ruins the transparency, though...

I would solve this problem like this:

BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
Image image = qrcode.getImage();
Image mask = qrcode.getImage();
mask.makeMask();
image.setImageMask(mask);
document.add(image);

There may be an AWT solution too, but I'm more familiar with native PDF solutions than with using an AWT workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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