Sunday, 20 November 2016

Barcode and QR Scanner - Android

MaterialBarcodeScanner: Easy to use barcode reader for your Android Project (Uses Google Mobile Vision API).

Overview
 - Integrate in a few minutes
 - Quick and simple api
 - No external apps required
 - Uses Google Mobile Vision API (fast, local and rotation free)
 - Automatically parses QR Codes, Data Matrix, PDF-417, and Aztec values
 - Supports 1D barcodes: EAN-13, EAN-8, UPC-A, UPC-E, Code-39, Code-93, Code-128, ITF,
   Codabar
 - Supports 2D barcodes: QR Code, Data Matrix, PDF-417, Aztec

Setup:

0. Provide gradle dependency
    compile 'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA'

1. Build a MaterialBarcodeScanner
    private void startScan() {
        /**
         * Build a new MaterialBarcodeScanner
         */
        final MaterialBarcodeScanner mBarcodeScanner 
                 = new MaterialBarcodeScannerBuilder()
                .withActivity(MainActivity.this)
                .withEnableAutoFocus(true)
                .withBleepEnabled(true)
                .withBackfacingCamera()
                .withText("Scanning...")
                .withResultListener(new MaterialBarcodeScanner.OnResultListener() {
                    @Override
                    public void onResult(Barcode barcode) {
                        barcodeResult = barcode;
                        result.setText(barcode.rawValue);
                    }
                })
                .build();
        mBarcodeScanner.startScan();
    }

2. Hook it up to a button
    fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startScan();
            }
        });

3. Start scanning!

Additional Setup

Center tracking mode
By default a barcode is tracked/highlighted at the location at which it was found.  With center tracking mode a square image will be shown during scanning that will turn green when a barcode is found. Please note that you can still scan a barcode outside the center tracker! This is purely a visual change.

To activate center tracking mode simply call the following builder method:
.withCenterTracker()

If you want to provide your own image for the tracker you can use:
.withCenterTracker(R.drawable.your_tracker_image,R.drawable.your_detected_state_tracker_image)

Exclusive barcode scanning
In some situations you might want to scan for only a certain type of barcode like QR-Codes or 2D barcodes. You can do this with the following builder methods:
.withOnlyQRCodeScanning()
.withOnly3DScanning()
.withOnly2DScanning()
If you want to scan for a very specific combination of barcodes you can setup the builder like this:
.withBarcodeFormats(Barcode.AZTEC | Barcode.EAN_13 | Barcode.CODE_93)

*Check out the full example project for code required for camera permissions on Android 6.0 Marshmallow

Sunday, 6 November 2016

PARCELABLE VS JAVA SERIALIZATION JAVA / ANDROID

Android developers often face a predicament while passing object references to activities of whether to go with the Java Serialization method or opt for Android Parcelable. This blog is my attempt to compare the two techniques and cite an example to help decide which one of these is the best approach for passing an object from one activity to another.

Passing primitive data types like string, integer, float, etc. through intents is quite easy in Android. All you have to do is put the data with unique key in intents and send it to another activity. If a user wants to send Java objects through intent, Java class should be implemented using the Parcelable interface. Serialization, on the other hand, is a Java interface that allows users to implement the interface which gets marked as Serializable.

During the Android application development process, developers often have to send Java class objects from one activity to another activity using the intent. Developers can opt from the two types of object passing techniques, i.e. Serialization and Parcelable of object.  The fact that Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object. Here’s why:

Implementation:
Android Parcelable implementation allows objects to read and write from Parcels which can contain flattened data inside message containers.  If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and  write all object members into parcel objects. The second is to create a static Parcelable.Creator object to de-serialize the Java object.

Differences between Serialization and Parcelable:
Parcelable and Serialization are used for marshaling and unmarshaling Java objects.  Differences between the two are often cited around implementation techniques and performance results. From my experience, I have come to identify the following differences in both the approaches:
  • Parcelable is well documented in the Android SDK; serialization on the other hand is available in Java. It is for this very reason that Android developers prefer Parcelable over the Serialization technique.
  • In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.
  • Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.