Search in sources :

Example 1 with ResultMetadataType

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

the class AbstractBlackBoxTestCase method decode.

private boolean decode(BinaryBitmap source, float rotation, String expectedText, Map<?, ?> expectedMetadata, boolean tryHarder) throws ReaderException {
    String suffix = String.format(" (%srotation: %d)", tryHarder ? "try harder, " : "", (int) rotation);
    Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
    if (tryHarder) {
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    }
    // Try in 'pure' mode mostly to exercise PURE_BARCODE code paths for exceptions;
    // not expected to pass, generally
    Result result = null;
    try {
        Map<DecodeHintType, Object> pureHints = new EnumMap<>(hints);
        pureHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        result = barcodeReader.decode(source, pureHints);
    } catch (ReaderException re) {
    // continue
    }
    if (result == null) {
        result = barcodeReader.decode(source, hints);
    }
    if (expectedFormat != result.getBarcodeFormat()) {
        log.info(String.format("Format mismatch: expected '%s' but got '%s'%s", expectedFormat, result.getBarcodeFormat(), suffix));
        return false;
    }
    String resultText = result.getText();
    if (!expectedText.equals(resultText)) {
        log.info(String.format("Content mismatch: expected '%s' but got '%s'%s", expectedText, resultText, suffix));
        return false;
    }
    Map<ResultMetadataType, ?> resultMetadata = result.getResultMetadata();
    for (Map.Entry<?, ?> metadatum : expectedMetadata.entrySet()) {
        ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString());
        Object expectedValue = metadatum.getValue();
        Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
        if (!expectedValue.equals(actualValue)) {
            log.info(String.format("Metadata mismatch for key '%s': expected '%s' but got '%s'", key, expectedValue, actualValue));
            return false;
        }
    }
    return true;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) ResultMetadataType(com.google.zxing.ResultMetadataType) EnumMap(java.util.EnumMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 2 with ResultMetadataType

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

the class CaptureActivity method handleDecodeExternally.

// Briefly show the contents of the barcode, then handle the result outside Barcode Scanner.
private void handleDecodeExternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
    if (barcode != null) {
        viewfinderView.drawResultBitmap(barcode);
    }
    long resultDurationMS;
    if (getIntent() == null) {
        resultDurationMS = DEFAULT_INTENT_RESULT_DURATION_MS;
    } else {
        resultDurationMS = getIntent().getLongExtra(Intents.Scan.RESULT_DISPLAY_DURATION_MS, DEFAULT_INTENT_RESULT_DURATION_MS);
    }
    if (resultDurationMS > 0) {
        String rawResultString = String.valueOf(rawResult);
        if (rawResultString.length() > 32) {
            rawResultString = rawResultString.substring(0, 32) + " ...";
        }
        statusView.setText(getString(resultHandler.getDisplayTitle()) + " : " + rawResultString);
    }
    maybeSetClipboard(resultHandler);
    if (source == IntentSource.NATIVE_APP_INTENT) {
        // Hand back whatever action they requested - this can be changed to Intents.Scan.ACTION when
        // the deprecated intent is retired.
        Intent intent = new Intent(getIntent().getAction());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
        intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
        byte[] rawBytes = rawResult.getRawBytes();
        if (rawBytes != null && rawBytes.length > 0) {
            intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
        }
        Map<ResultMetadataType, ?> metadata = rawResult.getResultMetadata();
        if (metadata != null) {
            if (metadata.containsKey(ResultMetadataType.UPC_EAN_EXTENSION)) {
                intent.putExtra(Intents.Scan.RESULT_UPC_EAN_EXTENSION, metadata.get(ResultMetadataType.UPC_EAN_EXTENSION).toString());
            }
            Number orientation = (Number) metadata.get(ResultMetadataType.ORIENTATION);
            if (orientation != null) {
                intent.putExtra(Intents.Scan.RESULT_ORIENTATION, orientation.intValue());
            }
            String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
            if (ecLevel != null) {
                intent.putExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL, ecLevel);
            }
            @SuppressWarnings("unchecked") Iterable<byte[]> byteSegments = (Iterable<byte[]>) metadata.get(ResultMetadataType.BYTE_SEGMENTS);
            if (byteSegments != null) {
                int i = 0;
                for (byte[] byteSegment : byteSegments) {
                    intent.putExtra(Intents.Scan.RESULT_BYTE_SEGMENTS_PREFIX + i, byteSegment);
                    i++;
                }
            }
        }
        sendReplyMessage(R.id.return_scan_result, intent, resultDurationMS);
    } else if (source == IntentSource.PRODUCT_SEARCH_LINK) {
        // Reformulate the URL which triggered us into a query, so that the request goes to the same
        // TLD as the scan URL.
        int end = sourceUrl.lastIndexOf("/scan");
        String replyURL = sourceUrl.substring(0, end) + "?q=" + resultHandler.getDisplayContents() + "&source=zxing";
        sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
    } else if (source == IntentSource.ZXING_LINK) {
        if (scanFromWebPageManager != null && scanFromWebPageManager.isScanFromWebPage()) {
            String replyURL = scanFromWebPageManager.buildReplyURL(rawResult, resultHandler);
            scanFromWebPageManager = null;
            sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
        }
    }
}
Also used : ResultMetadataType(com.google.zxing.ResultMetadataType) Intent(android.content.Intent) Paint(android.graphics.Paint) ResultPoint(com.google.zxing.ResultPoint)

Example 3 with ResultMetadataType

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

the class CaptureActivity method handleDecodeExternally.

// Briefly show the contents of the barcode, then handle the result outside
// Barcode Scanner.
private void handleDecodeExternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
    if (barcode != null) {
        viewfinderView.drawResultBitmap(barcode);
    }
    long resultDurationMS;
    if (getIntent() == null) {
        resultDurationMS = DEFAULT_INTENT_RESULT_DURATION_MS;
    } else {
        resultDurationMS = getIntent().getLongExtra(Intents.Scan.RESULT_DISPLAY_DURATION_MS, DEFAULT_INTENT_RESULT_DURATION_MS);
    }
    if (resultDurationMS > 0) {
        String rawResultString = String.valueOf(rawResult);
        if (rawResultString.length() > 32) {
            rawResultString = rawResultString.substring(0, 32) + " ...";
        }
        statusView.setText(getString(resultHandler.getDisplayTitle()) + " : " + rawResultString);
    }
    if (copyToClipboard && !resultHandler.areContentsSecure()) {
        CharSequence text = resultHandler.getDisplayContents();
        ClipboardInterface.setText(text, this);
    }
    if (source == IntentSource.NATIVE_APP_INTENT) {
        // Hand back whatever action they requested - this can be changed to
        // Intents.Scan.ACTION when
        // the deprecated intent is retired.
        Intent intent = new Intent(getIntent().getAction());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
        intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
        byte[] rawBytes = rawResult.getRawBytes();
        if (rawBytes != null && rawBytes.length > 0) {
            intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
        }
        Map<ResultMetadataType, ?> metadata = rawResult.getResultMetadata();
        if (metadata != null) {
            if (metadata.containsKey(ResultMetadataType.UPC_EAN_EXTENSION)) {
                intent.putExtra(Intents.Scan.RESULT_UPC_EAN_EXTENSION, metadata.get(ResultMetadataType.UPC_EAN_EXTENSION).toString());
            }
            Number orientation = (Number) metadata.get(ResultMetadataType.ORIENTATION);
            if (orientation != null) {
                intent.putExtra(Intents.Scan.RESULT_ORIENTATION, orientation.intValue());
            }
            String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
            if (ecLevel != null) {
                intent.putExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL, ecLevel);
            }
            @SuppressWarnings("unchecked") Iterable<byte[]> byteSegments = (Iterable<byte[]>) metadata.get(ResultMetadataType.BYTE_SEGMENTS);
            if (byteSegments != null) {
                int i = 0;
                for (byte[] byteSegment : byteSegments) {
                    intent.putExtra(Intents.Scan.RESULT_BYTE_SEGMENTS_PREFIX + i, byteSegment);
                    i++;
                }
            }
        }
        sendReplyMessage(R.id.return_scan_result, intent, resultDurationMS);
    } else if (source == IntentSource.PRODUCT_SEARCH_LINK) {
        // Reformulate the URL which triggered us into a query, so that the
        // request goes to the same
        // TLD as the scan URL.
        int end = sourceUrl.lastIndexOf("/scan");
        String replyURL = sourceUrl.substring(0, end) + "?q=" + resultHandler.getDisplayContents() + "&source=zxing";
        sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
    } else if (source == IntentSource.ZXING_LINK) {
        if (scanFromWebPageManager != null && scanFromWebPageManager.isScanFromWebPage()) {
            String replyURL = scanFromWebPageManager.buildReplyURL(rawResult, resultHandler);
            scanFromWebPageManager = null;
            sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
        }
    }
}
Also used : ResultMetadataType(com.google.zxing.ResultMetadataType) Intent(android.content.Intent) ResultPoint(com.google.zxing.ResultPoint) Paint(android.graphics.Paint)

Example 4 with ResultMetadataType

use of com.google.zxing.ResultMetadataType in project android-zxing by PearceXu.

the class CaptureActivity method handleDecodeInternally.

// Put up our own UI for how to handle the decoded contents.
private void handleDecodeInternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
    maybeSetClipboard(resultHandler);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (resultHandler.getDefaultButtonID() != null && prefs.getBoolean(PreferencesActivity.KEY_AUTO_OPEN_WEB, false)) {
        resultHandler.handleButtonPress(resultHandler.getDefaultButtonID());
        return;
    }
    statusView.setVisibility(View.GONE);
    viewfinderView.setVisibility(View.GONE);
    resultView.setVisibility(View.VISIBLE);
    ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
    if (barcode == null) {
        barcodeImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.launcher_icon));
    } else {
        barcodeImageView.setImageBitmap(barcode);
    }
    TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
    formatTextView.setText(rawResult.getBarcodeFormat().toString());
    TextView typeTextView = (TextView) findViewById(R.id.type_text_view);
    typeTextView.setText(resultHandler.getType().toString());
    DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    TextView timeTextView = (TextView) findViewById(R.id.time_text_view);
    timeTextView.setText(formatter.format(rawResult.getTimestamp()));
    TextView metaTextView = (TextView) findViewById(R.id.meta_text_view);
    View metaTextViewLabel = findViewById(R.id.meta_text_view_label);
    metaTextView.setVisibility(View.GONE);
    metaTextViewLabel.setVisibility(View.GONE);
    Map<ResultMetadataType, Object> metadata = rawResult.getResultMetadata();
    if (metadata != null) {
        StringBuilder metadataText = new StringBuilder(20);
        for (Map.Entry<ResultMetadataType, Object> entry : metadata.entrySet()) {
            if (DISPLAYABLE_METADATA_TYPES.contains(entry.getKey())) {
                metadataText.append(entry.getValue()).append('\n');
            }
        }
        if (metadataText.length() > 0) {
            metadataText.setLength(metadataText.length() - 1);
            metaTextView.setText(metadataText);
            metaTextView.setVisibility(View.VISIBLE);
            metaTextViewLabel.setVisibility(View.VISIBLE);
        }
    }
    CharSequence displayContents = resultHandler.getDisplayContents();
    TextView contentsTextView = (TextView) findViewById(R.id.contents_text_view);
    contentsTextView.setText(displayContents);
    int scaledSize = Math.max(22, 32 - displayContents.length() / 4);
    contentsTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);
    TextView supplementTextView = (TextView) findViewById(R.id.contents_supplement_text_view);
    supplementTextView.setText("");
    supplementTextView.setOnClickListener(null);
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(PreferencesActivity.KEY_SUPPLEMENTAL, true)) {
        SupplementalInfoRetriever.maybeInvokeRetrieval(supplementTextView, resultHandler.getResult(), historyManager, this);
    }
    int buttonCount = resultHandler.getButtonCount();
    ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
    buttonView.requestFocus();
    for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) {
        TextView button = (TextView) buttonView.getChildAt(x);
        if (x < buttonCount) {
            button.setVisibility(View.VISIBLE);
            button.setText(resultHandler.getButtonText(x));
            button.setOnClickListener(new ResultButtonListener(resultHandler, x));
        } else {
            button.setVisibility(View.GONE);
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) ViewGroup(android.view.ViewGroup) SurfaceView(android.view.SurfaceView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) ResultPoint(com.google.zxing.ResultPoint) ResultButtonListener(com.google.zxing.client.android.result.ResultButtonListener) DateFormat(java.text.DateFormat) ResultMetadataType(com.google.zxing.ResultMetadataType) TextView(android.widget.TextView) ImageView(android.widget.ImageView) Map(java.util.Map)

Example 5 with ResultMetadataType

use of com.google.zxing.ResultMetadataType in project android-zxing by PearceXu.

the class UPCEANExtension5Support method decodeRow.

Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {
    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);
    String resultString = result.toString();
    Map<ResultMetadataType, Object> extensionData = parseExtensionString(resultString);
    Result extensionResult = new Result(resultString, null, new ResultPoint[] { new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, rowNumber), new ResultPoint(end, rowNumber) }, BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
        extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultMetadataType(com.google.zxing.ResultMetadataType) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Aggregations

ResultMetadataType (com.google.zxing.ResultMetadataType)20 ResultPoint (com.google.zxing.ResultPoint)17 Result (com.google.zxing.Result)10 Paint (android.graphics.Paint)8 Intent (android.content.Intent)7 Map (java.util.Map)5 DateFormat (java.text.DateFormat)4 SurfaceView (android.view.SurfaceView)3 View (android.view.View)3 ViewGroup (android.view.ViewGroup)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 ResultButtonListener (com.google.zxing.client.android.result.ResultButtonListener)3 SharedPreferences (android.content.SharedPreferences)2 Date (java.util.Date)2 Bundle (android.os.Bundle)1 BinaryBitmap (com.google.zxing.BinaryBitmap)1 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)1 DecodeHintType (com.google.zxing.DecodeHintType)1 LuminanceSource (com.google.zxing.LuminanceSource)1