Slide 1

Slide 1 text

How to effectively scan barcodes on Android renaud_mathieu

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

The history of barcodes

Slide 4

Slide 4 text

Thesis of Wallace Flint 1932 Norman Woodland and Bernard Silver patented the barcode idea 1949 Philco purchased the barcode patent then sold it to RCA 1952 UPC 1973 First produc scanned at a check-out counter 1974

Slide 5

Slide 5 text

Philco purchased the barcode patent then sold it to RCA 1952 UPC 1973 First product scanned at a check-out counter 1974 EAN 1975 DataMatrix 1993 QR Code 1994

Slide 6

Slide 6 text

Symbologies

Slide 7

Slide 7 text

The mapping between messages and barcodes

Slide 8

Slide 8 text

More than 100 different barcode symbologies.

Slide 9

Slide 9 text

Linear Stacked 2D EAN8 EAN13 UPC Code 11 Code 39 Code 93 PDF 417 Code 49 DataMatrix QR Code Aztec

Slide 10

Slide 10 text

Reading UPC barcodes

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Left and Right Patterns for UPC Symbology

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

CALCULATE A UPC CHECK DIGIT

Slide 22

Slide 22 text

•Add the digits in the odd-numbered positions together and multiply the total by three 6+2+7+9+1+6=31 31x3=93 •Add the digits in the even-numbered positions 9+7+1+8+1=26 •Add the two results together 93+26=119 •Now what single digit number makes the total a multiple of 10? That’s the check digit. 119 + 1 = 120 Data = 69277198116

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Barcode Scanning Use Cases

Slide 25

Slide 25 text

Scan & Go

Slide 26

Slide 26 text

Loyalty programs

Slide 27

Slide 27 text

Lyf Pay

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Design guidelines

Slide 30

Slide 30 text

I/O 2019 - ML Kit x Material Design: Design Patterns for Mobile Machine Learning

Slide 31

Slide 31 text

Utiliser la caméra pour visualiser le code-barres

Slide 32

Slide 32 text

Détecter un code-barres

Slide 33

Slide 33 text

Afficher le résultat

Slide 34

Slide 34 text

https:"//material.io/collections/machine-learning/

Slide 35

Slide 35 text

Implement a barcode scanner

Slide 36

Slide 36 text

Camera Frame Processor Engine

Slide 37

Slide 37 text

Camera •android.hardware.Camera •android.hardware.Camera2 •CameraX •natario1/CameraView

Slide 38

Slide 38 text

Frame Processor • CameraX • natario1/CameraView

Slide 39

Slide 39 text

Engine • ZXing • Firebase ML Kit

Slide 40

Slide 40 text

Camera Engine CameraView ZXing CameraX MLKit

Slide 41

Slide 41 text

Camera Engine CameraView ZXing CameraX MLKit Alpha Beta Maintenance ?

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-rc03' implementation 'androidx.camera:camera-core:1.0.0-alpha09' implementation 'androidx.camera:camera-camera2:1.0.0-alpha09' implementation 'androidx.camera:camera-lifecycle:1.0.0-alpha02' implementation 'androidx.camera:camera-view:1.0.0-alpha05' implementation 'com.google.firebase:firebase-analytics:17.2.2' implementation 'com.google.firebase:firebase-ml-vision:24.0.1' implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.2' implementation 'com.otaliastudios:cameraview:2.4.0' implementation 'com.google.zxing:core:3.3.3' implementation 'io.reactivex.rxjava2:rxjava:2.2.9' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

Slide 44

Slide 44 text

@Parcelize data class BarcodeModel( val value: String, val symbology: Symbology ) : Parcelable Models

Slide 45

Slide 45 text

enum class Symbology(val value: String) { AZTEC("aztec"), CODE128("code128"), CODE39("code39"), CODE93("code93"), CODABAR("codabar"), DATAMATRIX("datamatrix"), EAN13("ean13"), EAN8("ean8"), ITF14("itf14"), QR("qr"), UPCA("upca"), UPCE("upce"), PDF417("pdf417"); } Models

Slide 46

Slide 46 text

CameraView + ZXing

Slide 47

Slide 47 text

class OCameraView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : CameraView(context, attrs) { private var isPaused: Boolean = false private val frameProcessor = FrameProcessor { if (!isPaused) { "// I’ve got a new frame } } init { setLifecycleOwner(context as LifecycleOwner) }

Slide 48

Slide 48 text

} init { setLifecycleOwner(context as LifecycleOwner) } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) private fun resumeScan() { isPaused = false addFrameProcessor(frameProcessor) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) private fun pauseScan() { isPaused = true removeFrameProcessor(frameProcessor) }

Slide 49

Slide 49 text

interface BarcodeAnalyzer { fun onBarcodeScanned(): Observable }

Slide 50

Slide 50 text

!!/** * MultiFormatReader is a convenience class and the main entry point into the library for most uses. * By default it attempts to decode all barcode formats that the library supports. Optionally, you * can provide a hints object to request different behavior, for example only decoding QR codes. * * @author Sean Owen * @author dswitkin@google.com (Daniel Switkin) !*/ public final class MultiFormatReader implements Reader { … }

Slide 51

Slide 51 text

!!/** * Decode an image using the state set up by calling setHints() previously. Continuous scan * clients will get a large! speed increase by using this instead of decode(). * * @param image The pixel data to decode * @return The contents of the image * @throws NotFoundException Any errors which occurred !*/ public Result decodeWithState(BinaryBitmap image) throws NotFoundException { "// Make sure to set up the default state so we don't crash if (readers "== null) { setHints(null); } return decodeInternal(image); }

Slide 52

Slide 52 text

!!/** * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects * accept a BinaryBitmap and attempt to decode it. * * @author dswitkin@google.com (Daniel Switkin) !*/ public final class BinaryBitmap { private final Binarizer binarizer; private BitMatrix matrix; public BinaryBitmap(Binarizer binarizer) { if (binarizer "== null) { throw new IllegalArgumentException("Binarizer must be non-null."); } this.binarizer = binarizer; }

Slide 53

Slide 53 text

!!/** * This class implements a local thresholding algorithm, which while slower than the * GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for * high frequency images of barcodes with black data on white backgrounds. For this application, * it does a much better job than a global blackpoint with severe shadows and gradients. * However it tends to produce artifacts on lower frequency images and is therefore not * a good general purpose binarizer for uses outside ZXing. * * This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers, * and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already * inherently local, and only fails for horizontal gradients. We can revisit that problem later, * but for now it was not a win to use local blocks for 1D. * * This Binarizer is the default for the unit tests and the recommended class for library users. * * @author dswitkin@google.com (Daniel Switkin) !*/ public final class HybridBinarizer extends GlobalHistogramBinarizer { public HybridBinarizer(LuminanceSource source) { super(source); } }

Slide 54

Slide 54 text

!!/** * The purpose of this class hierarchy is to abstract different bitmap implementations across * platforms into a standard interface for requesting greyscale luminance values. The interface * only provides immutable methods; therefore crop and rotation create copies. This is to ensure * that one Reader does not modify the original luminance source and leave it in an unknown state * for other Readers in the chain. * * @author dswitkin@google.com (Daniel Switkin) !*/ public abstract class LuminanceSource { private final int width; private final int height; protected LuminanceSource(int width, int height) { this.width = width; this.height = height; }

Slide 55

Slide 55 text

!!/** * This object extends LuminanceSource around an array of YUV data returned from the camera driver, * with the option to crop to a rectangle within the full data. This can be used to exclude * superfluous pixels around the perimeter and speed up decoding. * * It works for any pixel format where the Y channel is planar and appears first, including * YCbCr_420_SP and YCbCr_422_SP. * * @author dswitkin@google.com (Daniel Switkin) !*/ public final class PlanarYUVLuminanceSource extends LuminanceSource {

Slide 56

Slide 56 text

private fun detectInImageSingleOrientation( data: ByteArray, width: Int, height: Int, rotation: Int ): Result? { val source = getPlanarYUVLuminanceSource(data, width, height, rotation) val bitmap = BinaryBitmap(HybridBinarizer(source)) var result: Result? = null try { result = multiFormatReader.decodeWithState(bitmap) } catch (re: NotFoundException) { } finally { multiFormatReader.reset() } return result }

Slide 57

Slide 57 text

private fun detectInImage(data: ByteArray, width: Int, height: Int): Observable = Observable.create { emitter "-> val result = detectInImageSingleOrientation(data, width, height, ROTATION_90) "?: detectInImageSingleOrientation(data, width, height, ROTATION_0) if (result "!= null) { emitter.onNext(result) } }

Slide 58

Slide 58 text

class ZXingImageAnalyzer : BarcodeAnalyzer { companion object { const val ROTATION_0 = 0 const val ROTATION_90 = 90 } private val multiFormatReader: MultiFormatReader = MultiFormatReader() val onFrameProcessorPublisher: PublishSubject = PublishSubject.create() override fun onBarcodeScanned(): Observable = onFrameProcessorPublisher .flatMap { detectInImage(it.data, it.size.width, it.size.height) } .map { BarcodeModel(it.text, Symbology.CODE128) }

Slide 59

Slide 59 text

class OCameraView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : CameraView(context, attrs), BarcodeProcessor { private var isPaused: Boolean = false private val zxingImageAnalyzer = ZXingImageAnalyzer() private val frameProcessor = FrameProcessor { if (!isPaused) { zxingImageAnalyzer.onFrameProcessorPublisher.onNext(it) } }

Slide 60

Slide 60 text

CameraX ♥ MLKit

Slide 61

Slide 61 text

The CameraX library is in alpha stage, as its API surfaces aren't yet finalized.

Slide 62

Slide 62 text

CameraX is an addition to Jetpack that makes it easier to leverage the capabilities of Camera2 APIs.

Slide 63

Slide 63 text

Use cases : • Implement preview • Analyze images • Take a photo

Slide 64

Slide 64 text

Use cases can be combined and active concurrently.

Slide 65

Slide 65 text

class ScanApplication : Application(), CameraXConfig.Provider { override fun getCameraXConfig(): CameraXConfig = Camera2Config.defaultConfig() }

Slide 66

Slide 66 text

!!/** * Convenience class for generating a pre-populated Camera2 {@link CameraXConfig}. !*/ public final class Camera2Config { private Camera2Config() { } !!/** * Creates a {@link CameraXConfig} containing the default Camera2 implementation for CameraX. !*/ @NonNull public static CameraXConfig defaultConfig() { "// Create the camera factory for creating Camera2 camera objects CameraFactory.Provider cameraFactoryProvider = Camera2CameraFactory"::new; "// Create the DeviceSurfaceManager for Camera2 CameraDeviceSurfaceManager.Provider surfaceManagerProvider = Camera2DeviceSurfaceManager"::new; "// Create default configuration factory UseCaseConfigFactory.Provider configFactoryProvider = context "-> { ExtendableUseCaseConfigFactory factory = new ExtendableUseCaseConfigFactory(); factory.installDefaultProvider( ImageAnalysisConfig.class, new ImageAnalysisConfigProvider(context));

Slide 67

Slide 67 text

CameraDeviceSurfaceManager.Provider surfaceManagerProvider = Camera2DeviceSurfaceManager"::new; "// Create default configuration factory UseCaseConfigFactory.Provider configFactoryProvider = context "-> { ExtendableUseCaseConfigFactory factory = new ExtendableUseCaseConfigFactory(); factory.installDefaultProvider( ImageAnalysisConfig.class, new ImageAnalysisConfigProvider(context)); factory.installDefaultProvider( ImageCaptureConfig.class, new ImageCaptureConfigProvider(context)); factory.installDefaultProvider( VideoCaptureConfig.class, new VideoCaptureConfigProvider(context)); factory.installDefaultProvider( PreviewConfig.class, new PreviewConfigProvider(context)); return factory; }; CameraXConfig.Builder appConfigBuilder = new CameraXConfig.Builder() .setCameraFactoryProvider(cameraFactoryProvider) .setDeviceSurfaceManagerProvider(surfaceManagerProvider) .setUseCaseConfigFactoryProvider(configFactoryProvider); return appConfigBuilder.build(); } }

Slide 68

Slide 68 text

class XCameraView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : PreviewView(context, attrs), BarcodeProcessor { private var cameraProviderFuture: ListenableFuture = ProcessCameraProvider.getInstance(context) private val cameraSelector: CameraSelector = CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build() init { cameraProviderFuture.addListener( Runnable { bindToLifecycle(cameraProviderFuture.get()) }, ContextCompat.getMainExecutor(context) ) }

Slide 69

Slide 69 text

public Camera bindToLifecycle(@NonNull LifecycleOwner lifecycleOwner, @NonNull CameraSelector cameraSelector, @NonNull UseCase""... useCases) { return CameraX.bindToLifecycle(lifecycleOwner, cameraSelector, useCases); }

Slide 70

Slide 70 text

private fun bindToLifecycle(cameraProvider: ProcessCameraProvider) { val preview: Preview = Preview.Builder() .setTargetName("Preview") .build() preview.previewSurfaceProvider = previewSurfaceProvider cameraProvider.bindToLifecycle( context as LifecycleOwner, cameraSelector, preview ) }

Slide 71

Slide 71 text

ML Kit's barcode scanning API •Reads most standard formats •Automatic format detection •Extracts structured data •Works with any orientation •Runs on the device

Slide 72

Slide 72 text

Version 24.0.0 of firebase-ml- vision introduces a new barcode scanning model

Slide 73

Slide 73 text

ML Kit is still in Beta ML Kit still requires a Firebase project

Slide 74

Slide 74 text

class FirebaseImageAnalyzer : BarcodeAnalyzer { private val onBarcodeScannedPublisher: PublishSubject = PublishSubject.create() override fun onBarcodeScanned(): Observable = onBarcodeScannedPublisher

Slide 75

Slide 75 text

class FirebaseImageAnalyzer : BarcodeAnalyzer { private val options = FirebaseVisionBarcodeDetectorOptions.Builder() .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_ALL_FORMATS) .build() private val detector = FirebaseVision.getInstance() .getVisionBarcodeDetector(options) private val onBarcodeScannedPublisher: PublishSubject = PublishSubject.create() override fun onBarcodeScanned(): Observable = onBarcodeScannedPublisher

Slide 76

Slide 76 text

class FirebaseImageAnalyzer : ImageAnalysis.Analyzer, BarcodeAnalyzer { private val options = FirebaseVisionBarcodeDetectorOptions.Builder() .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_ALL_FORMATS) .build() private val detector = FirebaseVision.getInstance() .getVisionBarcodeDetector(options) private val onBarcodeScannedPublisher: PublishSubject = PublishSubject.create() override fun analyze(image: ImageProxy) { } override fun onBarcodeScanned(): Observable = onBarcodeScannedPublisher private fun degreesToFirebaseRotation(degrees: Int): Int = when (degrees) { 0 "-> FirebaseVisionImageMetadata.ROTATION_0 90 "-> FirebaseVisionImageMetadata.ROTATION_90 180 "-> FirebaseVisionImageMetadata.ROTATION_180 270 "-> FirebaseVisionImageMetadata.ROTATION_270 else "-> throw Exception("Rotation must be 0, 90, 180, or 270.") }

Slide 77

Slide 77 text

private fun bindToLifecycle(cameraProvider: ProcessCameraProvider) { val preview: Preview = Preview.Builder() .setTargetName("Preview") .build() preview.previewSurfaceProvider = previewSurfaceProvider val imageAnalysis = ImageAnalysis.Builder() .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) .setTargetResolution((Size(1280, 720))) .build() imageAnalysis.setAnalyzer( Executors.newSingleThreadExecutor(), firebaseImageAnalyzer ) cameraProvider.bindToLifecycle( context as LifecycleOwner, cameraSelector, preview, imageAnalysis ) }

Slide 78

Slide 78 text

override fun analyze(image: ImageProxy) { val mediaImage = image.image val imageRotation = degreesToFirebaseRotation(image.imageInfo.rotationDegrees) if (mediaImage "!= null) { val firebaseVisionImage = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) detector.detectInImage(firebaseVisionImage) .addOnSuccessListener { barcodes "-> if (barcodes.isEmpty()) return@addOnSuccessListener for (barcode in barcodes) { barcode.displayValue"?.let { value "-> val model = BarcodeModel( value = value, symbology = barcodeFormatToSymbology(barcode.format) ) onBarcodeScannedPublisher.onNext(model) } } } .addOnFailureListener { onBarcodeScannedPublisher.onError(it) } } image.close() }

Slide 79

Slide 79 text

override fun analyze(image: ImageProxy) { val mediaImage = image.image val imageRotation = degreesToFirebaseRotation(image.imageInfo.rotationDegrees) if (mediaImage "!= null) { val firebaseVisionImage = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) detector.detectInImage(firebaseVisionImage) .addOnSuccessListener { barcodes "-> if (barcodes.isEmpty()) return@addOnSuccessListener for (barcode in barcodes) { barcode.displayValue"?.let { value "-> val model = BarcodeModel( value = value, symbology = barcodeFormatToSymbology(barcode.format) ) onBarcodeScannedPublisher.onNext(model) } } } .addOnFailureListener { onBarcodeScannedPublisher.onError(it) } } image.close() }

Slide 80

Slide 80 text

override fun analyze(image: ImageProxy) { val mediaImage = image.image val imageRotation = degreesToFirebaseRotation(image.imageInfo.rotationDegrees) if (mediaImage "!= null) { val firebaseVisionImage = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) detector.detectInImage(firebaseVisionImage) .addOnSuccessListener { barcodes "-> if (barcodes.isEmpty()) return@addOnSuccessListener for (barcode in barcodes) { barcode.displayValue"?.let { value "-> val model = BarcodeModel( value = value, symbology = barcodeFormatToSymbology(barcode.format) ) onBarcodeScannedPublisher.onNext(model) } } } .addOnFailureListener { onBarcodeScannedPublisher.onError(it) } } image.close() }

Slide 81

Slide 81 text

override fun analyze(image: ImageProxy) { val mediaImage = image.image val imageRotation = degreesToFirebaseRotation(image.imageInfo.rotationDegrees) if (mediaImage "!= null) { val firebaseVisionImage = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) detector.detectInImage(firebaseVisionImage) .addOnSuccessListener { barcodes "-> if (barcodes.isEmpty()) return@addOnSuccessListener for (barcode in barcodes) { barcode.displayValue"?.let { value "-> val model = BarcodeModel( value = value, symbology = barcodeFormatToSymbology(barcode.format) ) onBarcodeScannedPublisher.onNext(model) } } } .addOnFailureListener { onBarcodeScannedPublisher.onError(it) } } image.close() }

Slide 82

Slide 82 text

!!/** * Only deliver the latest image to the analyzer, dropping images as they arrive. * *

This strategy ignores the value set by {@link Builder#setImageQueueDepth(int)}. * Only one image will be delivered for analysis at a time. If more images are produced * while that image is being analyzed, they will be dropped and not queued for delivery. * Once the image being analyzed is closed by calling {@link ImageProxy#close()}, the * next latest image will be delivered. * *

Internally this strategy may make use of an internal {@link Executor} to receive * and drop images from the producer. A performance-tuned executor will be created * internally unless one is explicitly provided by * {@link Builder#setBackgroundExecutor(Executor)}. In order to * ensure smooth operation of this backpressure strategy, any user supplied * {@link Executor} must be able to quickly respond to tasks posted to it, so setting * the executor manually should only be considered in advanced use cases. * * @see Builder#setBackgroundExecutor(Executor) !*/ public static final int STRATEGY_KEEP_ONLY_LATEST = 0;

Slide 83

Slide 83 text

class ScanActivity : AppCompatActivity() { companion object { const val VIBRATION_DURATION: Long = 250 } private val compositeDisposable = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) compositeDisposable.add(previewView.onBarcodeScanned() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { vibrate() }, { Log.e("ScanActivity", "Scanning error", it) } ) ) }

Slide 84

Slide 84 text

"

Slide 85

Slide 85 text

"

Slide 86

Slide 86 text

Go beyond…

Slide 87

Slide 87 text

Android Dev Summit ’19 https:"//www.youtube.com/watch?v=CSJMxEOgiJQ

Slide 88

Slide 88 text

• Barcode is far away • Barcode is distorted source: Android Dev Summit ’19

Slide 89

Slide 89 text

source: Android Dev Summit ’19

Slide 90

Slide 90 text

source: Android Dev Summit ’19

Slide 91

Slide 91 text

source: Android Dev Summit ’19

Slide 92

Slide 92 text

source: Android Dev Summit ’19

Slide 93

Slide 93 text

Machine Learning?

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Reading barcodes with neural networks Department of Electrical Engineering Linköping University SE-581 83 Linköping, Sweden Copyright © 2017 Fredrik Fridborn

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Thank you