使用itext7创建二维码的示例

91zkwejq  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(707)

我正在寻找一个如何使用itext7创建二维码的示例。我看到itext5的例子不再适用。我可以很容易地使用zxing库和itext来实现这一点,但是因为我们已经有itext了,所以我不希望再添加一个库。
如果它能帮助其他人,下面是使用zxing和itext7的代码。

File file = new File("qrCodeTest.pdf");

    // Create PDF with IText7
    PdfWriter pdfWriter = new PdfWriter(file);
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);

    Document document = new Document(pdfDocument);

    // Use ZXing to create the QR Code
    ByteArrayOutputStream baos = generateEAN13BarcodeImageBytes("some text");
    // Convert ZXing output to IText Image to add to PDF
    byte[] bytes = baos.toByteArray();
    ImageData imageData = ImageDataFactory.create(bytes);
    Image pdfImg = new Image(imageData);

    document.add(pdfImg);
    document.close();
mnowg1ta

mnowg1ta1#

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BarcodeService {

    private final OrderItemRepository orderItemRepository;
    private final OrderRepository orderRepository;
    private final BarcodeHistoryRepository barcodeHistoryRepository;

    @Transactional
    public byte[] getBarcodeForOrderItem(UUID orderItemId, Integer appId) throws WriterException, IOException {
        Optional<OrderItem> orderItemOptional = orderItemRepository.findById(orderItemId);
        if(!orderItemOptional.isPresent())
            throw new NoSuchElementException("OrderItem not found: " + orderItemId);
        BarcodeHistory barcodeHistory = BarcodeHistory.builder().build();
        barcodeHistory.setAppId(appId);
        barcodeHistory = barcodeHistoryRepository.save(barcodeHistory);
        OrderItem orderItem = orderItemOptional.get();
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(orderItem.getId() + ","
                + orderItem.getSkuId() + "," + orderItem.getQuantity() + "," + orderItem.getSpjOrder().getCustomerId()
                + "," + barcodeHistory.getToken(),
                BarcodeFormat.QR_CODE, 350, 350);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "JPG", outputStream);
        return outputStream.toByteArray();
    }

    @Transactional
    public byte[] getBarcodeForOrder(UUID orderId, Integer appId) throws WriterException, IOException {
        Optional<Order> orderOptional = orderRepository.findById(orderId);
        if(!orderOptional.isPresent())
            throw new NoSuchElementException("Order not found: " + orderId);
        BarcodeHistory barcodeHistory = BarcodeHistory.builder().build();
        barcodeHistory.setAppId(appId);
        barcodeHistory = barcodeHistoryRepository.save(barcodeHistory);
        Order order = orderOptional.get();
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(order.getId() + ","
                        + order.getCustomerId() + "," + barcodeHistory.getToken(),
                BarcodeFormat.QR_CODE, 350, 350);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(matrix, "JPG", outputStream);
        return outputStream.toByteArray();
    }
}
k2arahey

k2arahey2#

下面是一个仅使用itext7的示例

File file = new File("qrCodeExample.pdf");
    PdfWriter writer = new PdfWriter(file);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document document = new Document(pdfDoc);
    BarcodeQRCode qrCode = new BarcodeQRCode("Example QR Code Creation in iText7");
    PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
    Image barcodeImage = new Image(barcodeObject).setWidth(100f).setHeight(100f);
    document.add(new Paragraph().add(barcodeImage));
    document.close();

相关问题