Search in sources :

Example 56 with Result

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

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)

Example 57 with Result

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

the class HistoryManager method buildHistoryItem.

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        return new HistoryItem(result, display, details);
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 58 with Result

use of com.google.zxing.Result in project titanium-barcode by mwaylabs.

the class DecodeThread method decode.

/**
	 * Decode the data within the viewfinder rectangle, and time how long it
	 * took. For efficiency, reuse the same reader objects from one decode to
	 * the next.
	 * 
	 * @param data
	 *            The YUV preview frame.
	 * @param width
	 *            The width of the preview frame.
	 * @param height
	 *            The height of the preview frame.
	 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
        // Log and continue
        Log.d("DecodeThread", re.toString());
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), Id.DECODE_SUCCEEDED, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), Id.DECODE_FAILED);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 59 with Result

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

the class Code128Reader method decodeRow.

@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException, FormatException, ChecksumException {
    boolean convertFNC1 = hints != null && hints.containsKey(DecodeHintType.ASSUME_GS1);
    int[] startPatternInfo = findStartPattern(row);
    int startCode = startPatternInfo[2];
    List<Byte> rawCodes = new ArrayList<>(20);
    rawCodes.add((byte) startCode);
    int codeSet;
    switch(startCode) {
        case CODE_START_A:
            codeSet = CODE_CODE_A;
            break;
        case CODE_START_B:
            codeSet = CODE_CODE_B;
            break;
        case CODE_START_C:
            codeSet = CODE_CODE_C;
            break;
        default:
            throw FormatException.getFormatInstance();
    }
    boolean done = false;
    boolean isNextShifted = false;
    StringBuilder result = new StringBuilder(20);
    int lastStart = startPatternInfo[0];
    int nextStart = startPatternInfo[1];
    int[] counters = new int[6];
    int lastCode = 0;
    int code = 0;
    int checksumTotal = startCode;
    int multiplier = 0;
    boolean lastCharacterWasPrintable = true;
    boolean upperMode = false;
    boolean shiftUpperMode = false;
    while (!done) {
        boolean unshift = isNextShifted;
        isNextShifted = false;
        // Save off last code
        lastCode = code;
        // Decode another code from image
        code = decodeCode(row, counters, nextStart);
        rawCodes.add((byte) code);
        // Remember whether the last code was printable or not (excluding CODE_STOP)
        if (code != CODE_STOP) {
            lastCharacterWasPrintable = true;
        }
        // Add to checksum computation (if not CODE_STOP of course)
        if (code != CODE_STOP) {
            multiplier++;
            checksumTotal += multiplier * code;
        }
        // Advance to where the next code will to start
        lastStart = nextStart;
        for (int counter : counters) {
            nextStart += counter;
        }
        // Take care of illegal start codes
        switch(code) {
            case CODE_START_A:
            case CODE_START_B:
            case CODE_START_C:
                throw FormatException.getFormatInstance();
        }
        switch(codeSet) {
            case CODE_CODE_A:
                if (code < 64) {
                    if (shiftUpperMode == upperMode) {
                        result.append((char) (' ' + code));
                    } else {
                        result.append((char) (' ' + code + 128));
                    }
                    shiftUpperMode = false;
                } else if (code < 96) {
                    if (shiftUpperMode == upperMode) {
                        result.append((char) (code - 64));
                    } else {
                        result.append((char) (code + 64));
                    }
                    shiftUpperMode = false;
                } else {
                    // code was printable or not.
                    if (code != CODE_STOP) {
                        lastCharacterWasPrintable = false;
                    }
                    switch(code) {
                        case CODE_FNC_1:
                            if (convertFNC1) {
                                if (result.length() == 0) {
                                    // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
                                    // is FNC1 then this is GS1-128. We add the symbology identifier.
                                    result.append("]C1");
                                } else {
                                    // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
                                    result.append((char) 29);
                                }
                            }
                            break;
                        case CODE_FNC_2:
                        case CODE_FNC_3:
                            // do nothing?
                            break;
                        case CODE_FNC_4_A:
                            if (!upperMode && shiftUpperMode) {
                                upperMode = true;
                                shiftUpperMode = false;
                            } else if (upperMode && shiftUpperMode) {
                                upperMode = false;
                                shiftUpperMode = false;
                            } else {
                                shiftUpperMode = true;
                            }
                            break;
                        case CODE_SHIFT:
                            isNextShifted = true;
                            codeSet = CODE_CODE_B;
                            break;
                        case CODE_CODE_B:
                            codeSet = CODE_CODE_B;
                            break;
                        case CODE_CODE_C:
                            codeSet = CODE_CODE_C;
                            break;
                        case CODE_STOP:
                            done = true;
                            break;
                    }
                }
                break;
            case CODE_CODE_B:
                if (code < 96) {
                    if (shiftUpperMode == upperMode) {
                        result.append((char) (' ' + code));
                    } else {
                        result.append((char) (' ' + code + 128));
                    }
                    shiftUpperMode = false;
                } else {
                    if (code != CODE_STOP) {
                        lastCharacterWasPrintable = false;
                    }
                    switch(code) {
                        case CODE_FNC_1:
                            if (convertFNC1) {
                                if (result.length() == 0) {
                                    // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
                                    // is FNC1 then this is GS1-128. We add the symbology identifier.
                                    result.append("]C1");
                                } else {
                                    // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
                                    result.append((char) 29);
                                }
                            }
                            break;
                        case CODE_FNC_2:
                        case CODE_FNC_3:
                            // do nothing?
                            break;
                        case CODE_FNC_4_B:
                            if (!upperMode && shiftUpperMode) {
                                upperMode = true;
                                shiftUpperMode = false;
                            } else if (upperMode && shiftUpperMode) {
                                upperMode = false;
                                shiftUpperMode = false;
                            } else {
                                shiftUpperMode = true;
                            }
                            break;
                        case CODE_SHIFT:
                            isNextShifted = true;
                            codeSet = CODE_CODE_A;
                            break;
                        case CODE_CODE_A:
                            codeSet = CODE_CODE_A;
                            break;
                        case CODE_CODE_C:
                            codeSet = CODE_CODE_C;
                            break;
                        case CODE_STOP:
                            done = true;
                            break;
                    }
                }
                break;
            case CODE_CODE_C:
                if (code < 100) {
                    if (code < 10) {
                        result.append('0');
                    }
                    result.append(code);
                } else {
                    if (code != CODE_STOP) {
                        lastCharacterWasPrintable = false;
                    }
                    switch(code) {
                        case CODE_FNC_1:
                            if (convertFNC1) {
                                if (result.length() == 0) {
                                    // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
                                    // is FNC1 then this is GS1-128. We add the symbology identifier.
                                    result.append("]C1");
                                } else {
                                    // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
                                    result.append((char) 29);
                                }
                            }
                            break;
                        case CODE_CODE_A:
                            codeSet = CODE_CODE_A;
                            break;
                        case CODE_CODE_B:
                            codeSet = CODE_CODE_B;
                            break;
                        case CODE_STOP:
                            done = true;
                            break;
                    }
                }
                break;
        }
        // Unshift back to another code set if we were shifted
        if (unshift) {
            codeSet = codeSet == CODE_CODE_A ? CODE_CODE_B : CODE_CODE_A;
        }
    }
    int lastPatternSize = nextStart - lastStart;
    // Check for ample whitespace following pattern, but, to do this we first need to remember that
    // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
    // to read off. Would be slightly better to properly read. Here we just skip it:
    nextStart = row.getNextUnset(nextStart);
    if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) {
        throw NotFoundException.getNotFoundInstance();
    }
    // Pull out from sum the value of the penultimate check code
    checksumTotal -= multiplier * lastCode;
    // lastCode is the checksum then:
    if (checksumTotal % 103 != lastCode) {
        throw ChecksumException.getChecksumInstance();
    }
    // Need to pull out the check digits from string
    int resultLength = result.length();
    if (resultLength == 0) {
        // false positive
        throw NotFoundException.getNotFoundInstance();
    }
    // be a printable character. If it was just interpreted as a control code, nothing to remove.
    if (resultLength > 0 && lastCharacterWasPrintable) {
        if (codeSet == CODE_CODE_C) {
            result.delete(resultLength - 2, resultLength);
        } else {
            result.delete(resultLength - 1, resultLength);
        }
    }
    float left = (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
    float right = lastStart + lastPatternSize / 2.0f;
    int rawCodesSize = rawCodes.size();
    byte[] rawBytes = new byte[rawCodesSize];
    for (int i = 0; i < rawCodesSize; i++) {
        rawBytes[i] = rawCodes.get(i);
    }
    return new Result(result.toString(), rawBytes, new ResultPoint[] { new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber) }, BarcodeFormat.CODE_128);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ArrayList(java.util.ArrayList) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 60 with Result

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

the class Code39Reader method decodeRow.

@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    int[] theCounters = counters;
    Arrays.fill(theCounters, 0);
    StringBuilder result = decodeRowResult;
    result.setLength(0);
    int[] start = findAsteriskPattern(row, theCounters);
    // Read off white space    
    int nextStart = row.getNextSet(start[1]);
    int end = row.getSize();
    char decodedChar;
    int lastStart;
    do {
        recordPattern(row, nextStart, theCounters);
        int pattern = toNarrowWidePattern(theCounters);
        if (pattern < 0) {
            throw NotFoundException.getNotFoundInstance();
        }
        decodedChar = patternToChar(pattern);
        result.append(decodedChar);
        lastStart = nextStart;
        for (int counter : theCounters) {
            nextStart += counter;
        }
        // Read off white space
        nextStart = row.getNextSet(nextStart);
    } while (decodedChar != '*');
    // remove asterisk
    result.setLength(result.length() - 1);
    // Look for whitespace after pattern:
    int lastPatternSize = 0;
    for (int counter : theCounters) {
        lastPatternSize += counter;
    }
    int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
    // (but if it's whitespace to the very end of the image, that's OK)
    if (nextStart != end && (whiteSpaceAfterEnd * 2) < lastPatternSize) {
        throw NotFoundException.getNotFoundInstance();
    }
    if (usingCheckDigit) {
        int max = result.length() - 1;
        int total = 0;
        for (int i = 0; i < max; i++) {
            total += CHECK_DIGIT_STRING.indexOf(decodeRowResult.charAt(i));
        }
        if (result.charAt(max) != CHECK_DIGIT_STRING.charAt(total % 43)) {
            throw ChecksumException.getChecksumInstance();
        }
        result.setLength(max);
    }
    if (result.length() == 0) {
        // false positive
        throw NotFoundException.getNotFoundInstance();
    }
    String resultString;
    if (extendedMode) {
        resultString = decodeExtended(result);
    } else {
        resultString = result.toString();
    }
    float left = (start[1] + start[0]) / 2.0f;
    float right = lastStart + lastPatternSize / 2.0f;
    return new Result(resultString, null, new ResultPoint[] { new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber) }, BarcodeFormat.CODE_39);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Aggregations

Result (com.google.zxing.Result)117 ResultPoint (com.google.zxing.ResultPoint)43 BinaryBitmap (com.google.zxing.BinaryBitmap)37 ReaderException (com.google.zxing.ReaderException)35 HybridBinarizer (com.google.zxing.common.HybridBinarizer)30 Bundle (android.os.Bundle)19 Message (android.os.Message)17 DecoderResult (com.google.zxing.common.DecoderResult)14 ArrayList (java.util.ArrayList)14 MultiFormatReader (com.google.zxing.MultiFormatReader)11 NotFoundException (com.google.zxing.NotFoundException)11 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 Cursor (android.database.Cursor)9 LuminanceSource (com.google.zxing.LuminanceSource)9 BitArray (com.google.zxing.common.BitArray)8 DetectorResult (com.google.zxing.common.DetectorResult)8 BufferedImage (java.awt.image.BufferedImage)8 Test (org.junit.Test)8 Bitmap (android.graphics.Bitmap)7 Handler (android.os.Handler)7