com.google.zxing.NotFoundException exception comes when core java program executed?

前端 未结 9 1566
情书的邮戳
情书的邮戳 2020-12-16 16:56

I have a jpeg file which has 2D bar code. Image resolution is 1593X1212. I am using xing library to decode this barcode from image. I got following code on net.



        
相关标签:
9条回答
  • 2020-12-16 17:22

    It is normal; it just means no barcode was found. You haven't provided the image, so I can't say whether your image is even readable, let alone has a supported barcode format.

    0 讨论(0)
  • 2020-12-16 17:22

    Already this code if you use,

    public static String readQRCode(String filePath, String charset, Map hintMap)
    throws FileNotFoundException, IOException, NotFoundException {
    
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                ImageIO.read(new FileInputStream(filePath)))));
    
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
    
        return qrCodeResult.getText();
    }
    
    public static String readQRCode(String filePath, String charset, Map hintMap)
    throws FileNotFoundException, IOException, NotFoundException {
    
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                ImageIO.read(new FileInputStream(filePath)))));
    
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
    
        return qrCodeResult.getText();
    }
    

    To make changes of this code. its conformly working,

    public static String readQRCode(String filePath, String charset, Map hintMap)
    throws FileNotFoundException, IOException, NotFoundException {
        Map < DecodeHintType, Object > tmpHintsMap = new EnumMap < DecodeHintType, Object > (
            DecodeHintType.class);
    
        //tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.FALSE);
        //tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
        tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
    
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                ImageIO.read(new FileInputStream(filePath)))));
    
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);
    
        return qrCodeResult.getText();
    }
    
    0 讨论(0)
  • 2020-12-16 17:26

    I have adjusted target resolution in ImageAnalysis and it started to work.

    From

    ImageAnalysis imageAnalysis =
            new ImageAnalysis.Builder()
                    .setTargetResolution(new Size(mySurfaceView.getWidth(), mySurfaceView.getHeight()))
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                    .build();
    

    to this one

        ImageAnalysis imageAnalysis =
                new ImageAnalysis.Builder()
                        .setTargetResolution(new Size(700, 500))
                        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                        .build();
    
    0 讨论(0)
  • 2020-12-16 17:30

    This solution works for me. I hope this help you. I replace reader.decode(...) with reader.decodeWithState(...)

            MultiFormatReader reader = new MultiFormatReader();// use this otherwise
    
            Result result = reader.decodeWithState(bitmap);
    
    0 讨论(0)
  • 2020-12-16 17:35

    That exception is thrown when no barcode is found in the image:

    http://zxing.org/w/docs/javadoc/com/google/zxing/NotFoundException.html

    0 讨论(0)
  • 2020-12-16 17:38

    I had the same problem. I used an image that I knew had a valid QR code and I also got the com.google.zxing.NotFoundException.

    The problem is that the image you use as a source is to large for the library to decode. After I reduced the size of my image the QR code decoder worked.

    For the purpose of my application, the QR code on the image would always be more or less in the same area, so I used the getSubimage function of the BufferedImage class to isolate the QR code.

         BufferedImage image;
         image = ImageIO.read(imageFile);
         BufferedImage cropedImage = image.getSubimage(0, 0, 914, 400);
         // using the cropedImage instead of image
         LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
         // barcode decoding
         QRCodeReader reader = new QRCodeReader();
         Result result = null;
         try 
         {
             result = reader.decode(bitmap);
         } 
         catch (ReaderException e) 
         {
             return "reader error";
         }
    
    0 讨论(0)
提交回复
热议问题