1、按上节配置项目
2、添加布局文件
<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"
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">
<LinearLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_rgb"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_rgb"
android:layout_weight="1"
android:text="原图" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_gray"
android:layout_weight="1"
android:text="灰度图" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_gray"
android:layout_weight="1"
android:text="灰度图" />
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/layout_button"/>
</RelativeLayout>
3、添加代码
package com.lingyun.myopencvtest;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity {
private Button btn_rgb;
private Button btn_gray;
private Bitmap srcBitmap;
private Bitmap grayBitmap;
private ImageView iv_image;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity {
private Button btn_rgb;
private Button btn_gray;
private Bitmap srcBitmap;
private Bitmap grayBitmap;
private ImageView iv_image;
static {
if(!OpenCVLoader.initDebug()){
Log.d("MyDebug","Falied");
}else{
Log.d("MyDebug","success");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
setAction();
}
public void findView(){
btn_rgb = (Button)findViewById(R.id.button_rgb);
btn_gray = (Button)findViewById(R.id.button_gray);
iv_image = (ImageView)findViewById(R.id.image);
}
public void setAction(){
btn_rgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (srcBitmap == null) {
srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_cat);
}
iv_image.setImageBitmap(srcBitmap);
}
});
btn_gray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
if (srcBitmap == null) {
srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_cat);
}
Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
iv_image.setImageBitmap(grayBitmap);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
if(!OpenCVLoader.initDebug()){
Log.d("MyDebug","Falied");
}else{
Log.d("MyDebug","success");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
setAction();
}
public void findView(){
btn_rgb = (Button)findViewById(R.id.button_rgb);
btn_gray = (Button)findViewById(R.id.button_gray);
iv_image = (ImageView)findViewById(R.id.image);
}
public void setAction(){
btn_rgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (srcBitmap == null) {
srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_cat);
}
iv_image.setImageBitmap(srcBitmap);
}
});
btn_gray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
if (srcBitmap == null) {
srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_cat);
}
Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
iv_image.setImageBitmap(grayBitmap);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
4、测试