/** * 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) { // 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(); } }
// Briefly show the contents of the barcode, then handle the result outside Barcode Scanner. private void handleDecodeExternally(Result rawResult, Bitmap barcode) { viewfinderView.drawResultBitmap(barcode); // Since this message will only be shown for a second, just tell the user what kind of // barcode was found (e.g. contact info) rather than the full contents, which they won't // have time to read. ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult); statusView.setText(getString(resultHandler.getDisplayTitle())); if (copyToClipboard) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(resultHandler.getDisplayContents()); } if (source == Source.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()); // crop barcode bitmap by 5pixels on each side barcode = Bitmap.createBitmap(barcode, 5, 5, barcode.getWidth() - 10, barcode.getHeight() - 10); ByteArrayOutputStream bitmapOutStream = new ByteArrayOutputStream(); barcode.compress(Bitmap.CompressFormat.JPEG, 35, bitmapOutStream); intent.putExtra(Intents.Scan.RESULT_BITMAP_BYTES, bitmapOutStream.toByteArray()); Message message = Message.obtain(handler, R.id.return_scan_result); message.obj = intent; handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } else if (source == Source.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. Message message = Message.obtain(handler, R.id.launch_product_query); int end = sourceUrl.lastIndexOf("/scan"); message.obj = sourceUrl.substring(0, end) + "?q=" + resultHandler.getDisplayContents().toString() + "&source=zxing"; handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } else if (source == Source.ZXING_LINK) { // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate // with the scanned code. This allows both queries and REST-style URLs to work. Message message = Message.obtain(handler, R.id.launch_product_query); message.obj = returnUrlTemplate.replace( RETURN_CODE_PLACEHOLDER, resultHandler.getDisplayContents().toString()); handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } }
// Briefly show the contents of the barcode, then handle the result outside Barcode Scanner. private void handleDecodeExternally( Result rawResult, ResultHandler resultHandler, Bitmap barcode) { viewfinderView.drawResultBitmap(barcode); // Since this message will only be shown for a second, just tell the user what kind of // barcode was found (e.g. contact info) rather than the full contents, which they won't // have time to read. // TODO // TODO statusView.setText(getString(resultHandler.getDisplayTitle())); if (copyToClipboard && !resultHandler.areContentsSecure()) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(resultHandler.getDisplayContents()); } if (source == Source.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("RTN_CONTENT", resultHandler.getDisplayContents()); 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); } Message message = Message.obtain(handler, R.id.return_scan_result); message.obj = intent; handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } else if (source == Source.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. Message message = Message.obtain(handler, R.id.launch_product_query); int end = sourceUrl.lastIndexOf("/scan"); message.obj = sourceUrl.substring(0, end) + "?q=" + resultHandler.getDisplayContents().toString() + "&source=zxing"; handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } else if (source == Source.ZXING_LINK) { // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate // with the scanned code. This allows both queries and REST-style URLs to work. Message message = Message.obtain(handler, R.id.launch_product_query); message.obj = returnUrlTemplate.replace( RETURN_CODE_PLACEHOLDER, resultHandler.getDisplayContents().toString()); handler.sendMessageDelayed(message, INTENT_RESULT_DURATION); } }
/** * 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; // XXX 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]; } int tmp = width; // Here we are swapping, that's the difference to #11 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.zandroid_zxing_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.zandroid_zxing_decode_failed); message.sendToTarget(); } }
// 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); sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS); } } }
// 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); } // Since this message will only be shown for a second, just tell the user what kind of // barcode was found (e.g. contact info) rather than the full contents, which they won't // have time to read. if (resultDurationMS > 0) { statusView.setText(getString(resultHandler.getDisplayTitle())); } if (copyToClipboard && !resultHandler.areContentsSecure()) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); CharSequence text = resultHandler.getDisplayContents(); if (text != null) { clipboard.setText(text); } } 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()); } Integer orientation = (Integer) 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); } 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(fakeR.getId("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(fakeR.getId("id", "launch_product_query"), replyURL, resultDurationMS); } else if (source == IntentSource.ZXING_LINK) { // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate // with the scanned code. This allows both queries and REST-style URLs to work. if (returnUrlTemplate != null) { CharSequence codeReplacement = returnRaw ? rawResult.getText() : resultHandler.getDisplayContents(); try { codeReplacement = URLEncoder.encode(codeReplacement.toString(), "UTF-8"); } catch (UnsupportedEncodingException e) { // can't happen; UTF-8 is always supported. Continue, I guess, without encoding } String replyURL = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER, codeReplacement); sendReplyMessage(fakeR.getId("id", "launch_product_query"), replyURL, resultDurationMS); } } }