Category: Google Services

  • Barcode Detector – Simple example

    1. Dependencies:

    dependencies {
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.google.android.gms:play-services:8.1.0'
    }
    

    2. Initialization code:

        private BarcodeDetector detector;
        ...
        private void initAndCheckDetector() {
            detector = new BarcodeDetector.Builder(getActivity().getApplicationContext())
                    .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE | Barcode.EAN_13)
                    .build();
            if (!detector.isOperational()) {
                Toast.makeText(getActivity().getApplicationContext(), "Could not set up the detector!", Toast.LENGTH_SHORT).show();
                detector = null;
            }
        }
    

    Recognize Barcode:

        private void recognizeBarcode(Bitmap myBitmap ) {
            if (detector!= null) {
                Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
                SparseArray<Barcode> barcodes = detector.detect(frame);
                Barcode code = barcodes.valueAt(0);
                String result = code.rawValue;
            }
        }
    

    Barcode Detection with the Mobile Vision API

  • GCM – Push notification code (php)

    An example that I used for tests.

    define( 'API_ACCESS_KEY', 'fill_here_your_api_access_key' ); 
    $registrationIds = array("fill_here_registered_id_of_your _device"); 
    
    $msg = array ( 
        'parameter1' =>; 'Some data that you want to send',
        'parameter2' =>; 'Some additional data...',
    );
    
    $fields = array
    (
        'registration_ids'  =>; $registrationIds,
        'data'              =>; $msg
    );
    
    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );
    
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );
    
    echo $result;