Search in sources :

Example 1 with Result

use of com.google.zxing.Result 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 Result

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

the class RSSExpandedStackedInternalTestCase method testDecodingRowByRow.

@Test
public void testDecodingRowByRow() throws Exception {
    RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
    BinaryBitmap binaryMap = TestCaseUtil.getBinaryBitmap("src/test/resources/blackbox/rssexpandedstacked-2/1000.png");
    int firstRowNumber = binaryMap.getHeight() / 3;
    BitArray firstRow = binaryMap.getBlackRow(firstRowNumber, null);
    try {
        rssExpandedReader.decodeRow2pairs(firstRowNumber, firstRow);
        fail(NotFoundException.class.getName() + " expected");
    } catch (NotFoundException nfe) {
    // ok
    }
    assertEquals(1, rssExpandedReader.getRows().size());
    ExpandedRow firstExpandedRow = rssExpandedReader.getRows().get(0);
    assertEquals(firstRowNumber, firstExpandedRow.getRowNumber());
    assertEquals(2, firstExpandedRow.getPairs().size());
    firstExpandedRow.getPairs().get(1).getFinderPattern().getStartEnd()[1] = 0;
    int secondRowNumber = 2 * binaryMap.getHeight() / 3;
    BitArray secondRow = binaryMap.getBlackRow(secondRowNumber, null);
    secondRow.reverse();
    List<ExpandedPair> totalPairs = rssExpandedReader.decodeRow2pairs(secondRowNumber, secondRow);
    Result result = RSSExpandedReader.constructResult(totalPairs);
    assertEquals("(01)98898765432106(3202)012345(15)991231", result.getText());
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) Result(com.google.zxing.Result) Test(org.junit.Test)

Example 3 with Result

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

the class RSSExpandedStackedInternalTestCase method testCompleteDecode.

@Test
public void testCompleteDecode() throws Exception {
    OneDReader rssExpandedReader = new RSSExpandedReader();
    BinaryBitmap binaryMap = TestCaseUtil.getBinaryBitmap("src/test/resources/blackbox/rssexpandedstacked-2/1000.png");
    Result result = rssExpandedReader.decode(binaryMap);
    assertEquals("(01)98898765432106(3202)012345(15)991231", result.getText());
}
Also used : OneDReader(com.google.zxing.oned.OneDReader) BinaryBitmap(com.google.zxing.BinaryBitmap) Result(com.google.zxing.Result) Test(org.junit.Test)

Example 4 with Result

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

the class DecodeWorker method dumpResult.

private static void dumpResult(URI input, Result... results) throws IOException {
    Collection<String> resultTexts = new ArrayList<>();
    for (Result result : results) {
        resultTexts.add(result.getText());
    }
    Files.write(buildOutputPath(input, ".txt"), resultTexts, StandardCharsets.UTF_8);
}
Also used : ArrayList(java.util.ArrayList) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult)

Example 5 with Result

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

the class HistoryItemAdapter method getView.

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    View layout;
    if (view instanceof LinearLayout) {
        layout = view;
    } else {
        LayoutInflater factory = LayoutInflater.from(activity);
        layout = factory.inflate(R.layout.history_list_item, viewGroup, false);
    }
    HistoryItem item = getItem(position);
    Result result = item.getResult();
    CharSequence title;
    CharSequence detail;
    if (result != null) {
        title = result.getText();
        detail = item.getDisplayAndDetails();
    } else {
        Resources resources = getContext().getResources();
        title = resources.getString(R.string.history_empty);
        detail = resources.getString(R.string.history_empty_detail);
    }
    ((TextView) layout.findViewById(R.id.history_title)).setText(title);
    ((TextView) layout.findViewById(R.id.history_detail)).setText(detail);
    return layout;
}
Also used : LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) Resources(android.content.res.Resources) TextView(android.widget.TextView) View(android.view.View) LinearLayout(android.widget.LinearLayout) Result(com.google.zxing.Result)

Aggregations

Result (com.google.zxing.Result)204 ResultPoint (com.google.zxing.ResultPoint)85 BinaryBitmap (com.google.zxing.BinaryBitmap)64 ReaderException (com.google.zxing.ReaderException)60 HybridBinarizer (com.google.zxing.common.HybridBinarizer)55 Bundle (android.os.Bundle)36 Message (android.os.Message)30 DecoderResult (com.google.zxing.common.DecoderResult)28 ArrayList (java.util.ArrayList)25 NotFoundException (com.google.zxing.NotFoundException)23 MultiFormatReader (com.google.zxing.MultiFormatReader)21 DecodeHintType (com.google.zxing.DecodeHintType)18 LuminanceSource (com.google.zxing.LuminanceSource)17 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)17 DetectorResult (com.google.zxing.common.DetectorResult)16 BufferedImage (java.awt.image.BufferedImage)14 Cursor (android.database.Cursor)13 Handler (android.os.Handler)13 BarcodeFormat (com.google.zxing.BarcodeFormat)13 BitMatrix (com.google.zxing.common.BitMatrix)13