Is it possible to set background transparency, dpi and color space?

2,238 views
Skip to first unread message

Henrik Kristiansen

unread,
Jan 20, 2013, 6:06:12 AM1/20/13
to zx...@googlegroups.com
I'm using the following code to generate a QR code:

        String input = "hello world";
        QRCodeWriter writer = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 0);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        BitMatrix mtx = writer.encode(input, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);

        if (mtx != null) {
            MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000000, -1);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(mtx, matrixToImageConfig);
            ImageIO.write(image, "png", new File("C:\\New folder\\output.png"));
        }

Is it somehow possible to set a transparent background? When I enable the alpha channel on the white color in the MatrixToImageConfig the whole image turns black.
Is it somehow possible to specify color space and image dpi?

Sean Owen

unread,
Jan 20, 2013, 6:07:40 AM1/20/13
to zx...@googlegroups.com
Not directly though you can change that code to do whatever you want. 0 alpha means completely transparent which is usually interpreted as "black" if there is no other layer in the image, yes. Open it in an image editor where it puts a checkered background in the back and you will probably see what you want.

I don't think there's a need to change the color space. It's already ARGB.
There's no such thing as DPI in this context. The image has a dimension in pixels only.

Henrik Kristiansen

unread,
Jan 20, 2013, 6:31:57 AM1/20/13
to zx...@googlegroups.com
I just tried to open the image with gimp 2.8, the background is just black.
Regarding the color space question, I just wanted to know if it is possible to save the image as CMYK (converted with an ICC profile). If not I'll just use imagemagick afterwards

Sean Owen

unread,
Jan 20, 2013, 7:06:16 AM1/20/13
to zx...@googlegroups.com
Either it's rendering it that way because the layer background is black, or because it wasn't actually set to transparent. I don't know, you'd have to check your modification. The white may well be transparent.
I don't think any of the Java libraries support CMYK, as it's more a color space for printing. I think you would have to convert externally. But, the image has one color by definition... both color spaces would represent it exactly and there's not an inherent reason you need to work in CMYK for this image by itself.

Melvin

unread,
Nov 22, 2013, 3:24:20 AM11/22/13
to zx...@googlegroups.com
I just came across this discussion and it is helpful to me. So I guess I should share the answer :) ..

If you look into the the method in MatrixToImageWriter class:
     public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config)
you will found the call "config.getBufferedImageColorModel()". This call made a quick guess of the color model (if it is not black and white, it will use BufferedImage.TYPE_INT_RGB!) Instead it should use BufferedImage.TYPE_INT_ARGB. A bug :)

Sean Owen

unread,
Nov 22, 2013, 12:47:19 PM11/22/13
to zx...@googlegroups.com
No, no alpha channel is needed or used here. The high byte is 0xFF anyway.
Reply all
Reply to author
Forward
0 new messages