public class MainActivity extends AppCompatActivity {
  
  private ImageView imageView;
  private Paint txtPaint;
  private Paint roundPaint;
  private Paint rectPaint;
  private Bitmap defaultBitmap;
  private Bitmap temporaryBitmap;
  private Bitmap eyePatchBitmap;
  private Canvas canvas;
  private boolean withEyes;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.image_view);
    initPainters();
    initBitmaps();
  }
  
  public void processImage(View view) {
    withEyes = false;
    process();
  }
  
  public void processImageWithEyes(View view) {
    withEyes = true;
    process();
  }

  public void process() {
    canvas = new Canvas(temporaryBitmap);
    canvas.drawBitmap(defaultBitmap, 0, 0, null);
    
    FaceDetector faceDetector = new FaceDetector.Builder(this)
    .setTrackingEnabled(false)
    .setLandmarkType(FaceDetector.ALL_LANDMARKS)
    .build();
    
    if (!faceDetector.isOperational()) {
      new AlertDialog.Builder(this)
      .setMessage("Face Detector couldn't be set up on your device")
      .show();
    } else {
      Frame frame = new Frame.Builder().setBitmap(defaultBitmap).build();
      SparseArray<Face> sparseArray = faceDetector.detect(frame);
      detectFaces(sparseArray);
      imageView.setImageDrawable(new BitmapDrawable(getResources(), temporaryBitmap));
      faceDetector.release();
    }
  }
  
  private void initBitmaps() {
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inMutable = true;
    
    defaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bill_gates,
    bitmapOptions);
    temporaryBitmap = Bitmap.createBitmap(defaultBitmap.getWidth(), defaultBitmap
    .getHeight(), Bitmap.Config.RGB_565);
    eyePatchBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.eye,
    bitmapOptions);
  }
  
  private void initPainters() {
    roundPaint = new Paint();
    roundPaint.setColor(Color.RED);
    roundPaint.setStyle(Paint.Style.FILL);
    
    rectPaint = new Paint();
    rectPaint.setStrokeWidth(10);
    rectPaint.setColor(Color.RED);
    rectPaint.setStyle(Paint.Style.STROKE);
    
    txtPaint = new Paint();
    txtPaint.setColor(Color.BLACK);
    txtPaint.setStyle(Paint.Style.FILL);
  }
  
  private void detectFaces(SparseArray<Face> sparseArray) {
    for (int i = 0; i < sparseArray.size(); i++) {
      Face face = sparseArray.valueAt(i);
      float left = face.getPosition().x;
      float top = face.getPosition().y;
      float right = left + face.getWidth();
      float bottom = right + face.getHeight();
      
      RectF rectF = new RectF(left, top, right, bottom);
      canvas.drawRect(rectF, rectPaint);
      
      detectLandmarks(face);
    }
  }
  
  private void detectLandmarks(Face face) {
    for (Landmark landmark : face.getLandmarks()) {
      int cx = (int) (landmark.getPosition().x);
      int cy = (int) (landmark.getPosition().y);
      canvas.drawCircle(cx, cy, 10, roundPaint);
      
      drawLandmarkType(landmark.getType(), cx, cy);
      
      if (withEyes) {
        drawEyeBitmap(landmark.getType(), cx, cy);
      }
    }
  }
  
  private void drawLandmarkType(int landmarkType, float cx, float cy) {
    String type = String.valueOf(landmarkType);
    txtPaint.setTextSize(70);
    canvas.drawText(type, cx + 20, cy, txtPaint);
  }
  
  private void drawEyeBitmap(int landmarkType, float cx, float cy) {
    if (landmarkType == Landmark.LEFT_EYE  ||  landmarkType == Landmark.RIGHT_EYE) {
      int scaledWidth = eyePatchBitmap.getScaledWidth(canvas);
      int scaledHeight = eyePatchBitmap.getScaledHeight(canvas);
      canvas.drawBitmap(eyePatchBitmap, cx - (scaledWidth / 2), cy - (scaledHeight / 2), null);
    }
  }
}

public class MainActivity extends AppCompatActivity {
  
  ImageView imageView;
  TextView detectedTextView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    imageView = (ImageView) findViewById(R.id.image);
    detectedTextView = (TextView) findViewById(R.id.detected_text);
  }
  
  public void detectText(View view) {
    Bitmap textBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bill_gates);
    TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
    
    if (!textRecognizer.isOperational()) {
      new AlertDialog.Builder(this)
      .setMessage("Text recognizer couldn't be set up on your device")
      .show();
      return;
    }
    
    Frame frame = new Frame.Builder().setBitmap(textBitmap).build();
    SparseArray<TextBlock> texts = textRecognizer.detect(frame);
    StringBuilder builder = new StringBuilder();
    
    for (int i = 0; i < texts.size(); ++i) {
      TextBlock item = texts.valueAt(i);
      
      if (item != null && item.getValue() != null) {
        builder.append(item.getValue()).append("\n");
      }
    }
    
    detectedTextView.setText(builder.toString());
    textRecognizer.release(); 
  }
}

public class MainActivity extends AppCompatActivity {
  
  private Bitmap barcodeBitmap;
  private TextView textView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    textView = (TextView) findViewById(R.id.text_view);
    barcodeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
    
    imageView.setImageBitmap(barcodeBitmap);
  }
  
  public void processBarcode(View view) {
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
    .setBarcodeFormats(Barcode.ALL_FORMATS)
    .build();
    
    if (!barcodeDetector.isOperational()) {
      new AlertDialog.Builder(this)
      .setMessage("Barcode detector couldn't be set up on your device")
      .show();
      return;
    }
    
    Frame frame = new Frame.Builder().setBitmap(barcodeBitmap).build();
    SparseArray<Barcode> barcodes = barcodeDetector.detect(frame);
    
    int size = barcodes.size();
    
    if (size == 0) {
      textView.setText("No information available");
    } else {
      StringBuilder barcodeValue = new StringBuilder();
      
      for (int i = 0; i < size; i++) {
        barcodeValue.append(barcodes.valueAt(i).displayValue).append("\n");
      }
      
      textView.setText(barcodeValue.toString());
    }
    
    barcodeDetector.release();
  }
}