MainActivity.java
package com.firstopencvapp;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity {
Mat imageMat;
private ImageView picture;
private static final String TAG = "OCVSample::Activity";
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
imageMat=new Mat();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
public void onResume()
{
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mLoaderCallback);
} else {
Log.d("OpenCV", "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picture = (ImageView)findViewById(R.id.ivImageProcessed);
Button buttonOne = (Button)findViewById(R.id.button1);
buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Show Source Image!", Toast.LENGTH_SHORT).show();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testimage1);
picture.setImageBitmap(bitmap);
}
});
Button buttonTwo = (Button)findViewById(R.id.button2);
buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Show Gray Image!", Toast.LENGTH_SHORT).show();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testimage1);
picture.setImageBitmap(bitmap);
Mat mat_src = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, mat_src);
Mat mat_gray = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1);
Imgproc.cvtColor(mat_src, mat_gray, Imgproc.COLOR_BGRA2GRAY, 1);
Bitmap bmp_dst = Bitmap.createBitmap(mat_gray.cols(), mat_gray.rows(), Bitmap.Config.ARGB_8888 );
Utils.matToBitmap(mat_gray, bmp_dst);
picture.setImageBitmap(bmp_dst);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.firstopencvapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/ivImageProcessed"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_above="@+id/ivImageProcessed"
android:layout_alignRight="@+id/ivImageProcessed"
android:layout_alignEnd="@+id/ivImageProcessed"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp" />
</RelativeLayout>