Git Product home page Git Product logo

Comments (4)

andreasrosdal avatar andreasrosdal commented on August 27, 2024 1

Hello! OpenPDF 2.0.2 is possibly the best PDF library for Java with a LGPL and MPL license.

See: https://github.com/zxing/zxing or find some other Java QR code library.

BarcodeQRCode was added in itext 5.0.2, while OpenPDF is a fork of iText 4 a long time ago.
https://github.com/itext/itextpdf/blob/master/itext/src/main/java/com/itextpdf/text/pdf/BarcodeQRCode.java

from openpdf.

ctoabidmaqbool avatar ctoabidmaqbool commented on August 27, 2024

Hi! Someone was provide me very nice response / answer, which was a lot helpfull, I i think this is removed, why?

from openpdf.

ctoabidmaqbool avatar ctoabidmaqbool commented on August 27, 2024

Hi! I have copied iTextPDF -> BarcodeQRCode and do changes what are required!

and implement here: OpenPDF\openpdf\src\main\java\com\lowagie\text\pdf\BarcodeQRCode.java

Note: It solve my problme, currently, but it will be more better, if this class official suported and implemented in OpenPDF Library, please!

package com.lowagie.text.pdf;

import java.awt.Color;
import java.awt.image.MemoryImageSource;
import java.util.Map;

import com.lowagie.text.BadElementException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.codec.CCITTG4Encoder;
import com.lowagie.text.Rectangle;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.WriterException;

public class BarcodeQRCode {

    private final BitMatrix bm;

    public BarcodeQRCode(String content, int width, int height, Map<EncodeHintType, Object> hints) {
        try {
            QRCodeWriter qc = new QRCodeWriter();
            bm = qc.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException ex) {
            throw new ExceptionConverter(ex);
        }
    }

    private byte[] getBitMatrix() {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int stride = (width + 7) / 8;
        byte[] b = new byte[stride * height];
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                if (bm.get(x, y)) {
                    int offset = stride * y + x / 8;
                    b[offset] |= (byte) (0x80 >> (x % 8));
                }
            }
        }
        return b;
    }

    public Image getImage() throws BadElementException {
        byte[] b = getBitMatrix();
        byte g4[] = CCITTG4Encoder.compress(b, bm.getWidth(), bm.getHeight());
        return Image.getInstance(bm.getWidth(), bm.getHeight(), false, Image.CCITTG4, Image.CCITT_BLACKIS1, g4, null);
    }

    // AWT related methods (remove this if you port to Android / GAE)

    public java.awt.Image createAwtImage(Color foreground, Color background) {
        int f = foreground.getRGB();
        int g = background.getRGB();
        java.awt.Canvas canvas = new java.awt.Canvas();

        int width = bm.getWidth();
        int height = bm.getHeight();
        int pix[] = new int[width * height];
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                pix[y * width + x] = bm.get(x, y) ? f : g;
            }
        }

        java.awt.Image img = canvas.createImage(new MemoryImageSource(width, height, pix, 0, width));
        return img;
    }

    public void placeBarcode(PdfContentByte cb, Color foreground, float moduleSide) {
        int width = bm.getWidth();
        int height = bm.getHeight();

        cb.setColorFill(foreground);

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                if (bm.get(x, y)) {
                    cb.rectangle(x * moduleSide, (height - y - 1) * moduleSide, moduleSide, moduleSide);
                }
            }
        }
        cb.fill();
    }

    public Rectangle getBarcodeSize() {
        return new Rectangle(0, 0, bm.getWidth(), bm.getHeight());
    }
}

And maven dependency: OpenPDF\openpdf\pom.xml

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.5.3</version>
  </dependency>

from openpdf.

asturio avatar asturio commented on August 27, 2024

@ctoabidmaqbool
just to be sure. You want to create QR-Codes, right? There are many implementations of Barcodes in OpenPDF, but as @andreasrosdal mentioned, the QR-Code aren't part of OpenPDF.
And copying the implementation of iText is not an option. So we need a new fresh implementation, where we can probably use some library.
There are some libraries which implement already QR-Codes. ZXing seems to be the most popular one.

At the other hand we want to keep the number of dependencies small.

So we need a fresh implementation using an optional dependency, or let the users use ZXing on their side, using e.g. a code like the one you posted here.

from openpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.