Search in sources :

Example 86 with Result

use of com.google.zxing.Result in project QRCode by 5peak2me.

the class MipcaActivityCapture method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch(requestCode) {
            case REQUEST_CODE:
                //获取选中图片的路径
                Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
                if (cursor.moveToFirst()) {
                    photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }
                cursor.close();
                mProgress = new ProgressDialog(MipcaActivityCapture.this);
                mProgress.setMessage("正在扫描...");
                mProgress.setCancelable(false);
                mProgress.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Result result = scanningImage(photo_path);
                        if (result != null) {
                            Message m = mHandler.obtainMessage();
                            m.what = PARSE_BARCODE_SUC;
                            m.obj = result.getText();
                            mHandler.sendMessage(m);
                        } else {
                            Message m = mHandler.obtainMessage();
                            m.what = PARSE_BARCODE_FAIL;
                            m.obj = "Scan failed!";
                            mHandler.sendMessage(m);
                        }
                    }
                }).start();
                break;
        }
    }
}
Also used : Message(android.os.Message) Cursor(android.database.Cursor) ProgressDialog(android.app.ProgressDialog) Result(com.google.zxing.Result)

Example 87 with Result

use of com.google.zxing.Result in project QRCode by 5peak2me.

the class DecodeHandler 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;
    // modify here
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    // Here we are swapping, that's the difference to #11
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        // Log.d(TAG, "Sending decode succeeded message...");
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.zxing.camera.PlanarYUVLuminanceSource) 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 88 with Result

use of com.google.zxing.Result in project QRCode by 5peak2me.

the class DecodeHandler 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;
    //modify here
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    // Here we are swapping, that's the difference to #11
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        //Log.d(TAG, "Sending decode succeeded message...");
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.camera.PlanarYUVLuminanceSource) 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 89 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 90 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)116 ResultPoint (com.google.zxing.ResultPoint)43 BinaryBitmap (com.google.zxing.BinaryBitmap)36 ReaderException (com.google.zxing.ReaderException)35 HybridBinarizer (com.google.zxing.common.HybridBinarizer)29 Bundle (android.os.Bundle)19 Message (android.os.Message)17 DecoderResult (com.google.zxing.common.DecoderResult)14 ArrayList (java.util.ArrayList)14 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 MultiFormatReader (com.google.zxing.MultiFormatReader)10 Cursor (android.database.Cursor)9 NotFoundException (com.google.zxing.NotFoundException)9 Handler (android.os.Handler)8 LuminanceSource (com.google.zxing.LuminanceSource)8 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