title: Scanning QR code using Android's Mobile Vision API
body: <p>I followed this <a href="http://code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680">tutorial</a> on how to build an Android app that can scan QR codes.</p>
  
  <p>Here's the full code. I added the Google Play services using grade like so <code>compile 'com.google.android.gms:play-services:7.8.0'</code>.</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="bitinvent.io.qrscanner" &gt;
  
      &lt;meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/&gt;
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
      &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme" &gt;
          &lt;activity android:name=".MainActivity" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
      &lt;SurfaceView
          android:id="@+id/cameraView"
          android:layout_width="640px"
          android:layout_height="480px"
          android:layout_centerVertical="true"
          android:layout_alignParentLeft="true"/&gt;
  
      &lt;TextView
          android:id="@+id/infoTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/cameraView"
          android:layout_marginLeft="16dp"
          android:text="Nothing to read"
          android:textSize="20sp"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package bitinvent.io.qrscanner;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.util.SparseArray;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.widget.TextView;
  
  import com.google.android.gms.vision.CameraSource;
  import com.google.android.gms.vision.Detector;
  import com.google.android.gms.vision.barcode.Barcode;
  import com.google.android.gms.vision.barcode.BarcodeDetector;
  
  import java.io.IOException;
  
  public class MainActivity extends Activity {
  
      private SurfaceView cameraView;
      private TextView barcodeInfo;
      private CameraSource cameraSource;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          cameraView = (SurfaceView) findViewById(R.id.cameraView);
          barcodeInfo = (TextView) findViewById(R.id.infoTextView);
  
          BarcodeDetector detector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
          cameraSource = new CameraSource.Builder(this, detector).setRequestedPreviewSize(640, 480).build();
  
          cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
              @Override
              public void surfaceCreated(SurfaceHolder holder) {
                  try {
                      cameraSource.start(cameraView.getHolder());
                  } catch (IOException e) {
                      Log.e("CAMERA SOURCE", e.getMessage());
                  }
              }
  
              @Override
              public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
              }
  
              @Override
              public void surfaceDestroyed(SurfaceHolder holder) {
                  cameraSource.stop();
              }
          });
  
          detector.setProcessor(new Detector.Processor&lt;Barcode&gt;() {
              @Override
              public void release() {
  
              }
  
              @Override
              public void receiveDetections(Detector.Detections&lt;Barcode&gt; detections) {
                  final SparseArray&lt;Barcode&gt; barcodes = detections.getDetectedItems();
                  if (barcodes.size() != 0) {
                      barcodeInfo.post(new Runnable() {
                          @Override
                          public void run() {
                              barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                          }
                      });
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>I tested this on a HTC Desire 816 running Android 4.4.2. But it doesn't seem to work. The camera view is active but when pointed at a QR code, it doesn't detect anything. But no errors or crashes occur either.</p>
  
  <p>Am I missing something?</p>