Data Matrix Scan takes too long

I’m currently working on a Java application that needs to scan Data Matrix QR-Codes, specifically to extract and display XML data in a text box. The QR-Codes I’m dealing with contain BMP (German medication plans). For testing purposes, I’m using examples from this document: Link
im running this on a microsoft surface go 4 business webcam specs are :
Rear Camera:

Resolution: 8.0 megapixels
Video Resolution: 1080p HD video

Logging in german btw

Here’s the code I have so far:


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import javax.swing.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;

public class QRCodeScanner extends JFrame implements Runnable, ThreadFactory {

    private static final Logger LOGGER = Logger.getLogger(QRCodeScanner.class.getName());
    
    private static final long serialVersionUID = 1L;

    private BlockingQueue<String> decodedResultsQueue = new LinkedBlockingQueue<>();

    
    private Executor executor = Executors.newSingleThreadExecutor(this);
    private Webcam webcam = null;
    private WebcamPanel panel = null;
    private JTextArea textarea = null;
    private JScrollPane scrollPane = null;

    public QRCodeScanner() {
        super();

        setLayout(new BorderLayout());
        setTitle("QR Code Scanner");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension size = WebcamResolution.VGA.getSize();

        webcam = Webcam.getWebcams().get(1);
        webcam.setViewSize(size);

        panel = new WebcamPanel(webcam);
        panel.setPreferredSize(size);

        textarea = new JTextArea();
        textarea.setEditable(false);
        scrollPane = new JScrollPane(textarea);
        scrollPane.setPreferredSize(size);

        add(panel, BorderLayout.WEST);
        add(scrollPane, BorderLayout.CENTER);

        pack();
        setVisible(true);

        executor.execute(this);
    }

    @Override
    public void run() {
        do {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                LOGGER.severe("Thread interrupted: " + e.getMessage());
            }

            BufferedImage image = null;

            if (webcam.isOpen()) {
                LOGGER.info("Webcam is open.");
                if ((image = webcam.getImage()) == null) {
                    LOGGER.warning("Webcam image is null.");
                    continue;
                }
            } else {
                LOGGER.warning("Webcam is not open.");
            }

            Result result = processImageAndDecodeQRCode(image);

            if (result != null) {
                if (result.getBarcodeFormat() == BarcodeFormat.UPC_E) {
                    LOGGER.info("Detected UPC_E format, rescanning...");
                    continue;
                }
                result.getBarcodeFormat();
                LOGGER.info("QR code format: " + result.getBarcodeFormat());
                result.getNumBits();
                LOGGER.info("QR code num bits: " + result.getNumBits());
                result.getResultMetadata();
                LOGGER.info("QR code result metadata: " + result.getResultMetadata());
                result.getRawBytes();
                LOGGER.info("QR code raw bytes: " + result.getRawBytes());
                String text = result.getText();
                LOGGER.info("QR code text: " + text);
                decodedResultsQueue.add(text);
            }
        } while (true);
    }

    private Result processImageAndDecodeQRCode(BufferedImage image) {
        // Preprocess image: convert to grayscale and apply thresholding
        BufferedImage grayscaleImage = new BufferedImage(
            image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = grayscaleImage.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        LuminanceSource source = new BufferedImageLuminanceSource(grayscaleImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Result result = null;
        try {
            result = new MultiFormatReader().decode(bitmap);
        } catch (NotFoundException e) {
            LOGGER.warning("No QR code found.");
            e.getCause();
        }

        return result;
    }

    public void startUIUpdateThread() {
        new Thread(() -> {
            while (true) {
                try {
                    // Take the next decoded result from the queue and update the UI
                    String text = decodedResultsQueue.take();
                    textarea.append(text + "n");
                } catch (InterruptedException e) {
                    LOGGER.severe("UI update thread interrupted: " + e.getMessage());
                }
            }
        }).start();
    }
    
    
    
    
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r, "example-runner");
        t.setDaemon(true);
        return t;
    }

    public static void main(String[] args) {
        QRCodeScanner scanner = new QRCodeScanner();
        scanner.startUIUpdateThread();  // Start the UI update thread
    }
}

Tried it on online generated Data Matrix QR Codes and it works really well.
(Qrcodegenerator) Sometimes I’m able to scan those example codes from the PDF, but it takes too long, if it even works. I want this app to perform as well as those QR code scanners from the Play Store.

Maybe someone with more knowledge in computer vision can help me optimize the scanning process or suggest a more efficient approach?

Any assistance or pointers would be greatly appreciated. Thanks in advance!

New contributor

SmiSmiSmi42 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật