Search in sources :

Example 1 with ChecksumException

use of com.google.zxing.ChecksumException in project zxing by zxing.

the class DecodeServlet method processImage.

private static void processImage(BufferedImage image, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
    Collection<Result> results = new ArrayList<>(1);
    try {
        Reader reader = new MultiFormatReader();
        ReaderException savedException = null;
        try {
            // Look for multiple barcodes
            MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
            Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
            if (theResults != null) {
                results.addAll(Arrays.asList(theResults));
            }
        } catch (ReaderException re) {
            savedException = re;
        }
        if (results.isEmpty()) {
            try {
                // Look for pure barcode
                Result theResult = reader.decode(bitmap, HINTS_PURE);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Look for normal barcode in photo
                Result theResult = reader.decode(bitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Try again with other binarizer
                BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
                Result theResult = reader.decode(hybridBitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
            } catch (FormatException | ChecksumException e) {
                log.info(e.toString());
                errorResponse(request, response, "format");
            } catch (ReaderException e) {
                // Including NotFoundException
                log.info(e.toString());
                errorResponse(request, response, "notfound");
            }
            return;
        }
    } catch (RuntimeException re) {
        // Call out unexpected errors in the log clearly
        log.log(Level.WARNING, "Unexpected exception from library", re);
        throw new ServletException(re);
    }
    String fullParameter = request.getParameter("full");
    boolean minimalOutput = fullParameter != null && !Boolean.parseBoolean(fullParameter);
    if (minimalOutput) {
        response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try (Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
            for (Result result : results) {
                out.write(result.getText());
                out.write('\n');
            }
        }
    } else {
        request.setAttribute("results", results);
        request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
    }
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) MultiFormatReader(com.google.zxing.MultiFormatReader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) ChecksumException(com.google.zxing.ChecksumException) ArrayList(java.util.ArrayList) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) ImageReader(com.google.zxing.client.j2se.ImageReader) MultiFormatReader(com.google.zxing.MultiFormatReader) Reader(com.google.zxing.Reader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException) ServletException(javax.servlet.ServletException) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) OutputStreamWriter(java.io.OutputStreamWriter) BinaryBitmap(com.google.zxing.BinaryBitmap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with ChecksumException

use of com.google.zxing.ChecksumException in project zxing by zxing.

the class ErrorCorrectionTestCase method testTooManyErasures.

@Ignore("Erasures not implemented yet")
@Test
public void testTooManyErasures() {
    Random random = getRandom();
    int[] received = PDF417_TEST_WITH_EC.clone();
    int[] erasures = erase(received, MAX_ERASURES + 1, random);
    try {
        checkDecode(received, erasures);
        fail("Should not have decoded");
    } catch (ChecksumException ce) {
    // good
    }
}
Also used : Random(java.util.Random) ChecksumException(com.google.zxing.ChecksumException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ChecksumException

use of com.google.zxing.ChecksumException in project zxing by zxing.

the class ErrorCorrectionTestCase method testTooManyErrors.

@Test
public void testTooManyErrors() {
    int[] received = PDF417_TEST_WITH_EC.clone();
    Random random = getRandom();
    corrupt(received, MAX_ERRORS + 1, random);
    try {
        checkDecode(received);
        fail("Should not have decoded");
    } catch (ChecksumException ce) {
    // good
    }
}
Also used : Random(java.util.Random) ChecksumException(com.google.zxing.ChecksumException) Test(org.junit.Test)

Example 4 with ChecksumException

use of com.google.zxing.ChecksumException in project zxing by zxing.

the class PDF417ScanningDecoder method createDecoderResultFromAmbiguousValues.

/**
   * This method deals with the fact, that the decoding process doesn't always yield a single most likely value. The
   * current error correction implementation doesn't deal with erasures very well, so it's better to provide a value
   * for these ambiguous codewords instead of treating it as an erasure. The problem is that we don't know which of
   * the ambiguous values to choose. We try decode using the first value, and if that fails, we use another of the
   * ambiguous values and try to decode again. This usually only happens on very hard to read and decode barcodes,
   * so decoding the normal barcodes is not affected by this. 
   *
   * @param erasureArray contains the indexes of erasures
   * @param ambiguousIndexes array with the indexes that have more than one most likely value
   * @param ambiguousIndexValues two dimensional array that contains the ambiguous values. The first dimension must
   * be the same length as the ambiguousIndexes array
   */
private static DecoderResult createDecoderResultFromAmbiguousValues(int ecLevel, int[] codewords, int[] erasureArray, int[] ambiguousIndexes, int[][] ambiguousIndexValues) throws FormatException, ChecksumException {
    int[] ambiguousIndexCount = new int[ambiguousIndexes.length];
    int tries = 100;
    while (tries-- > 0) {
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            codewords[ambiguousIndexes[i]] = ambiguousIndexValues[i][ambiguousIndexCount[i]];
        }
        try {
            return decodeCodewords(codewords, ecLevel, erasureArray);
        } catch (ChecksumException ignored) {
        //
        }
        if (ambiguousIndexCount.length == 0) {
            throw ChecksumException.getChecksumInstance();
        }
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            if (ambiguousIndexCount[i] < ambiguousIndexValues[i].length - 1) {
                ambiguousIndexCount[i]++;
                break;
            } else {
                ambiguousIndexCount[i] = 0;
                if (i == ambiguousIndexCount.length - 1) {
                    throw ChecksumException.getChecksumInstance();
                }
            }
        }
    }
    throw ChecksumException.getChecksumInstance();
}
Also used : ChecksumException(com.google.zxing.ChecksumException) ResultPoint(com.google.zxing.ResultPoint)

Example 5 with ChecksumException

use of com.google.zxing.ChecksumException in project weex-example by KalicyZhou.

the class PDF417ScanningDecoder method createDecoderResultFromAmbiguousValues.

/**
   * This method deals with the fact, that the decoding process doesn't always yield a single most likely value. The
   * current error correction implementation doesn't deal with erasures very well, so it's better to provide a value
   * for these ambiguous codewords instead of treating it as an erasure. The problem is that we don't know which of
   * the ambiguous values to choose. We try decode using the first value, and if that fails, we use another of the
   * ambiguous values and try to decode again. This usually only happens on very hard to read and decode barcodes,
   * so decoding the normal barcodes is not affected by this. 
   *
   * @param erasureArray contains the indexes of erasures
   * @param ambiguousIndexes array with the indexes that have more than one most likely value
   * @param ambiguousIndexValues two dimensional array that contains the ambiguous values. The first dimension must
   * be the same length as the ambiguousIndexes array
   */
private static DecoderResult createDecoderResultFromAmbiguousValues(int ecLevel, int[] codewords, int[] erasureArray, int[] ambiguousIndexes, int[][] ambiguousIndexValues) throws FormatException, ChecksumException {
    int[] ambiguousIndexCount = new int[ambiguousIndexes.length];
    int tries = 100;
    while (tries-- > 0) {
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            codewords[ambiguousIndexes[i]] = ambiguousIndexValues[i][ambiguousIndexCount[i]];
        }
        try {
            return decodeCodewords(codewords, ecLevel, erasureArray);
        } catch (ChecksumException ignored) {
        //
        }
        if (ambiguousIndexCount.length == 0) {
            throw ChecksumException.getChecksumInstance();
        }
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            if (ambiguousIndexCount[i] < ambiguousIndexValues[i].length - 1) {
                ambiguousIndexCount[i]++;
                break;
            } else {
                ambiguousIndexCount[i] = 0;
                if (i == ambiguousIndexCount.length - 1) {
                    throw ChecksumException.getChecksumInstance();
                }
            }
        }
    }
    throw ChecksumException.getChecksumInstance();
}
Also used : ChecksumException(com.google.zxing.ChecksumException) ResultPoint(com.google.zxing.ResultPoint)

Aggregations

ChecksumException (com.google.zxing.ChecksumException)19 FormatException (com.google.zxing.FormatException)13 NotFoundException (com.google.zxing.NotFoundException)8 BinaryBitmap (com.google.zxing.BinaryBitmap)7 HybridBinarizer (com.google.zxing.common.HybridBinarizer)7 DecodeHintType (com.google.zxing.DecodeHintType)5 DecoderResult (com.google.zxing.common.DecoderResult)5 BitmapFactory (android.graphics.BitmapFactory)4 ResultPoint (com.google.zxing.ResultPoint)4 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)4 Hashtable (java.util.Hashtable)4 Result (com.google.zxing.Result)3 Bitmap (android.graphics.Bitmap)2 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)2 IOException (java.io.IOException)2 Random (java.util.Random)2 Test (org.junit.Test)2 Uri (android.net.Uri)1 Nullable (android.support.annotation.Nullable)1 Nullable (androidx.annotation.Nullable)1