【问题标题】:How to read datamatrix embedded in document?如何读取嵌入在文档中的数据矩阵?
【发布时间】:2020-04-05 15:35:52
【问题描述】:

我尝试读取嵌入到文档中的数据矩阵。 这是一个有助于创建和阅读 2DCODE 标准的开源项目。

我尝试使用此代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.junit.Test;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ZxingTest {

    @Test
    public void test() {

        readQRCode("sfr-facture-1048469311-1.jpg");
        readQRCode("sfr-facture-1048469311-1.png");

    }

    public static void readQRCode(String fileName) {

        System.out.println("Try reading " + fileName);

        File file = new File(fileName);
        BufferedImage image = null;

        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (image == null)
            return;

        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        // hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, Arrays.asList(BarcodeFormat.DATA_MATRIX));

        decode(image, hints);

    }

    public static void decode(BufferedImage tmpBfrImage, Hashtable<DecodeHintType, Object> hintsMap) {
        if (tmpBfrImage == null)
            throw new IllegalArgumentException("Could not decode image.");
        LuminanceSource tmpSource = new BufferedImageLuminanceSource(tmpBfrImage);
        BinaryBitmap tmpBitmap = new BinaryBitmap(new HybridBinarizer(tmpSource));
        MultiFormatReader tmpBarcodeReader = new MultiFormatReader();

        Result tmpResult;
        String tmpFinalResult;
        try {
            
            if (hintsMap != null && !hintsMap.isEmpty())
                tmpResult = tmpBarcodeReader.decode(tmpBitmap, hintsMap);
            else
                tmpResult = tmpBarcodeReader.decode(tmpBitmap);
            
            // setting results.
            tmpFinalResult = String.valueOf(tmpResult.getText());
            System.out.println("tmpFinalResult=" + tmpFinalResult);
        } catch (Exception tmpExcpt) {
            tmpExcpt.printStackTrace();
        }
    }

}

还有那些在网上找到的图片:

sfr-facture-1048469311-1.jpg
sfr-facture-1048469311-1.png

但无论图像格式如何,我都会收到此异常:com.google.zxing.NotFoundException

您能否建议我一个解析页面并检测数据矩阵坐标以进行预处理裁剪的库?

或者更好的读取数据矩阵的代码示例?

【问题讨论】:

    标签: zxing detect datamatrix


    【解决方案1】:

    我有解决问题的方法。我使用opencv定位任何条码,然后根据返回的坐标提取后,我用zxing读取它们。

    我的解决方案基于http://karthikj1.github.io/BarcodeLocalizer/ 的工作

    这是我使用的代码:

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.EnumMap;
    import java.util.List;
    import java.util.Map;
    
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.DecodeHintType;
    import com.google.zxing.LuminanceSource;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.Reader;
    import com.google.zxing.ReaderException;
    import com.google.zxing.Result;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    
    import barcodelocalizer.Barcode;
    import barcodelocalizer.CandidateResult;
    import barcodelocalizer.ImageDisplay;
    import barcodelocalizer.MatrixBarcode;
    import barcodelocalizer.TryHarderFlags;
    
    public class OpencvUtils {
    
        private static boolean SHOW_INTERMEDIATE_STEPS = false;
        private static boolean showImages = false;
    
        public static String process_bufferedImage(BufferedImage bufferedImage) {
            Barcode barcode;
    
            String barcodeText = null;
    
            // instantiate a class of type MatrixBarcode with the image filename
            try {
                barcode = new MatrixBarcode(bufferedImage, SHOW_INTERMEDIATE_STEPS, TryHarderFlags.VERY_SMALL_MATRIX);
    
                // locateBarcode() returns a List<CandidateResult> with all possible candidate
                // barcode regions from
                // within the image. These images then get passed to a decoder(we use ZXing here
                // but could be any decoder)
                List<CandidateResult> results = barcode.locateBarcode();
                System.out.println("Decoding buffered image " + results.size() + " candidate codes found");
    
                String barcodeName = barcode.getName();
    
                barcodeText = decodeBarcode(results, barcodeName, "Localizer");
    
            } catch (IOException ioe) {
                System.out.println("IO Exception when finding barcode " + ioe.getMessage());
            }
    
            return barcodeText;
        }
    
        public static String process_image(String imgFile) {
            Barcode barcode;
    
            String barcodeText = null;
    
            // instantiate a class of type MatrixBarcode with the image filename
            try {
                barcode = new MatrixBarcode(imgFile, SHOW_INTERMEDIATE_STEPS, TryHarderFlags.VERY_SMALL_MATRIX);
    
                // locateBarcode() returns a List<CandidateResult> with all possible candidate
                // barcode regions from
                // within the image. These images then get passed to a decoder(we use ZXing here
                // but could be any decoder)
                List<CandidateResult> results = barcode.locateBarcode();
                System.out.println("Decoding " + imgFile + " " + results.size() + " candidate codes found");
    
                String barcodeName = barcode.getName();
    
                barcodeText = decodeBarcode(results, barcodeName, "Localizer");
    
            } catch (IOException ioe) {
                System.out.println("IO Exception when finding barcode " + ioe.getMessage());
            }
    
            return barcodeText;
        }
    
        private static String decodeBarcode(List<CandidateResult> candidateCodes, String filename, String caption) {
            // decodes barcode using ZXing and either print the barcode text or says no
            // barcode found
            BufferedImage decodedBarcode = null;
            String title = null;
            Result result = null;
    
            String barcodeText = null;
    
            for (CandidateResult cr : candidateCodes) {
                BufferedImage candidate = cr.candidate;
                decodedBarcode = null;
                LuminanceSource source = new BufferedImageLuminanceSource(candidate);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                Reader reader = new MultiFormatReader();
    
                Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
                hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    
                try {
                    result = reader.decode(bitmap, hints);
                    decodedBarcode = candidate;
                    title = filename + " " + caption + " - barcode text " + result.getText() + " " + cr.getROI_coords();
                } catch (ReaderException re) {
                }
                if (decodedBarcode == null) {
                    title = filename + " - no barcode found - " + cr.getROI_coords();
                    if (showImages)
                        ImageDisplay.showImageFrame(candidate, title);
                } else {
                    if (showImages)
                        ImageDisplay.showImageFrame(decodedBarcode, title);
    
                    System.out.println("Barcode text for " + filename + " is " + result.getText());
                    barcodeText = result.getText();
                }
            }
    
            return barcodeText;
    
        }
    
    }
    

    我添加了处理字符串文件名的 java.awt.image.BufferedImage 的方法 process_bufferedImage。

    还有这个子方法得到BufferedImage bi的矩阵。

    protected Mat loadBufferedImage(BufferedImage bi) throws IOException {
            // reads the BufferedImage passed in parameter
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", byteArrayOutputStream);
            byteArrayOutputStream.flush();
    
            Mat mat = Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.IMREAD_UNCHANGED);
    
            return mat;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2018-05-01
      • 1970-01-01
      • 2016-05-20
      相关资源
      最近更新 更多