package org.opencv.samples.cameracalibration;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by mattluedke on 12/8/16.
 */

public class SomeOtherActivity extends Activity {

  private static final int CAMERA_PIC_REQUEST = 1987;
  private static final int PICK_IMAGE = 1776;

  @BindView(R.id.img_original)
  ImageView originalImageView;

  @BindView(R.id.img_edited1)
  ImageView editedImageView1;

  @BindView(R.id.img_edited2)
  ImageView editedImageView2;

  private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
      switch (status) {
        case LoaderCallbackInterface.SUCCESS: {
          Log.e("MYTEST", "OpenCV loaded successfully");
          processAndLoadIfNeeded();
        }
        break;
        default: {
          super.onManagerConnected(status);
        }
        break;
      }
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_some_other);

    ButterKnife.bind(this);
  }

  @Override
  public void onResume() {
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
      Log.e("MYTEST", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
      OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
      Log.e("MYTEST", "OpenCV library found inside package. Using it!");
      mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
  }

  @OnClick(R.id.btn_camera)
  public void camera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
      Bitmap image = (Bitmap) data.getExtras().get("data");
      originalImageView.setImageBitmap(image);

    } else if (requestCode == PICK_IMAGE) {
      Uri selectedImage = data.getData();
      String[] filePathColumn = { MediaStore.Images.Media.DATA };
      Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
      if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        Bitmap bm = BitmapFactory.decodeFile(picturePath, options);
        originalImageView.setImageBitmap(bm);
      }
    }
  }

  @OnClick(R.id.btn_gallery)
  public void gallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
  }

  @OnClick(R.id.btn_process)
  public void process() {
    processAndLoadIfNeeded();
  }

  private void processAndLoadIfNeeded() {

    BitmapDrawable drawable = (BitmapDrawable) originalImageView.getDrawable();

    if (drawable != null) {
      Bitmap bitmap = drawable.getBitmap();

      if (bitmap != null) {

        Mat mat = new Mat();

        Utils.bitmapToMat(bitmap, mat);

        Mat outmat1 = new Mat();
        Imgproc.cvtColor(mat, outmat1, Imgproc.COLOR_RGB2GRAY);
        Bitmap newImage1 = Bitmap.createBitmap(
          (int)mat.size().width,
          (int)mat.size().height, Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(outmat1, newImage1);
        editedImageView1.setImageBitmap(newImage1);

        Mat outmat2 = new Mat();
        Imgproc.cvtColor(mat, outmat2, Imgproc.COLOR_BGR2GRAY);
        Bitmap newImage2 = Bitmap.createBitmap(
          (int)mat.size().width,
          (int)mat.size().height, Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(outmat1, newImage2);
        editedImageView2.setImageBitmap(newImage2);
      }
    }
  }
}

@@ -3917,25 +3917,24 @@
 null) %7B%0A
-%0A
         
 Mat mat 
@@ -3929,141 +3929,60 @@
     
-Mat mat = new Mat();%0A%0A        Utils.bitmapToMat(bitmap, mat);%0A%0A        Mat outmat1 = new Mat();%0A        Imgproc.cvtColor(mat, outmat1
+editedImageView1.setImageBitmap(convertToGray(bitmap
 , Im
@@ -3998,24 +3998,25 @@
 OR_RGB2GRAY)
+)
 ;%0A        Bi
@@ -4017,249 +4017,230 @@
     
-Bitmap new
+editedImageView2.set
 Image
-1 = 
 Bitmap
-.createBitmap(
+(convertToGray(bitmap, Imgproc.COLOR_BGR2GRAY));
 %0A      
+%7D%0A
     
-(int)mat.size().width,%0A          (int)mat.size().height, Bitmap.Config.ARGB_8888);%0A        Utils.matToBitmap(outmat1,
+%7D%0A  %7D%0A%0A  private Bitmap convertToGray(Bitmap rgb, int conversion) %7B%0A    Mat mat =
  new
-Image1);%0A        editedImageView1.setImageBitmap(newImage1
+ Mat();%0A    Utils.bitmapToMat(rgb, mat
 );%0A
-%0A 
     
-   
 Mat 
@@ -4245,17 +4245,16 @@
 t outmat
-2
  = new M
@@ -4255,28 +4255,24 @@
  new Mat();%0A
-    
     Imgproc.
@@ -4295,44 +4295,27 @@
 tmat
-2
 , 
-Imgproc.COLOR_BGR2GRAY);%0A   
+conversion);%0A
     
- 
 Bitm
@@ -4325,17 +4325,16 @@
 newImage
-2
  = Bitma
@@ -4351,28 +4351,24 @@
 tmap(%0A      
-    
 (int)mat.siz
@@ -4384,20 +4384,16 @@
 ,%0A      
-    
 (int)mat
@@ -4434,20 +4434,16 @@
 _8888);%0A
-    
     Util
@@ -4462,17 +4462,16 @@
 p(outmat
-1
 , newIma
@@ -4476,77 +4476,31 @@
 mage
-2
 );%0A    
-    editedImageView2.setImageBitmap(newImage2);%0A      %7D%0A    %7D
+return newImage;
 %0A  %7D

clean up gray conversion code