网上一堆教程:略。
网上一堆教程:略。
到opencv官网下载地址下载opencv4android包。这边下载3.4.4版本的Android pack。
下载后解压可以看到以下几个文件。
apk:opencv已经编译好的Android可执行程序,拷贝到手机上即可运行。
samples:生成这些apk的Android代码工程。
sdk:里面是opencv的Java的接口和JNI接口等。
1.打开Android studio新建一个新的Android空工程。
2.在Android Studio的菜单选择File-->New-->Import Module。
3.选择opencv android pack解压出来的sdk目录下的java文件夹,点击下一步,出现三个复选框,都选上,点击Finish。
4.菜单栏选择File-->Project Structure。
5.按照如下图步骤操作,选择app-->Dependencies-->+-->3 Module dependency,点击ok退出,然后再点击ok退出。
6.修改build.gradle(Module:openCVLibrary344)文件,将compileSdkVersion,minSdkVersion,targetSdkVersion改成你对应安卓版本,这边目标为Android8.0,最低为Android7.0.
7.在工程目录下的app/src/main目录下新建文件夹jniLibs,将我们下载解压的OpenCV-android-sdk下的sdk/native/libs下的所有文件拷贝到jniLibs下。
8.到此,opencv环境配置完成。
package com.example.administrator.cv4andoird;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private double max_size = 1024;
private int PICK_IMAGE_REQUEST = 1;
private ImageView myImageView;
private Bitmap selectbp;
private Bitmap selectbptmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
staticLoadCVLibraries();
myImageView = (ImageView)findViewById(R.id.imageView);
myImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Button selectImageBtn = (Button)findViewById(R.id.select_btn);
selectImageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// makeText(MainActivity.this.getApplicationContext(), "start to browser image", Toast.LENGTH_SHORT).show();
selectImage();
}
});
Button processBtn = (Button)findViewById(R.id.process_btn);
processBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// makeText(MainActivity.this.getApplicationContext(), "hello, image process", Toast.LENGTH_SHORT).show();
convertGray();
}
});
Button cannyBtn = (Button)findViewById(R.id.canny_btn);
cannyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// makeText(MainActivity.this.getApplicationContext(), "hello, image process", Toast.LENGTH_SHORT).show();
kayCanny();
}
});
Button srcBtn = (Button)findViewById(R.id.src_btn);
srcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// makeText(MainActivity.this.getApplicationContext(), "hello, image process", Toast.LENGTH_SHORT).show();
kaySrc();
}
});
Button hsvBtn = (Button)findViewById(R.id.hsv_btn);
hsvBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// makeText(MainActivity.this.getApplicationContext(), "hello, image process", Toast.LENGTH_SHORT).show();
kayHsv();
}
});
}
private void staticLoadCVLibraries(){
boolean load = OpenCVLoader.initDebug();
if(load) {
Log.i("CV", "Open CV Libraries loaded...");
}
}
private void convertGray() {
Mat src = new Mat();
Mat temp = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(selectbp, src);
Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
Log.i("CV", "image type:" + (temp.type() == CvType.CV_8UC3));
Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);
Utils.matToBitmap(dst, selectbptmp);
myImageView.setImageBitmap(selectbptmp);
}
private void kayCanny() {
Mat src = new Mat();
Mat temp = new Mat();
Mat gray = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(selectbp, src);
Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
Imgproc.cvtColor(temp, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(gray, dst, 80, 90);
Utils.matToBitmap(dst, selectbptmp);
myImageView.setImageBitmap(selectbptmp);
}
private void kaySrc() {
myImageView.setImageBitmap(selectbp);
}
private void kayHsv() {
Mat src = new Mat();
Mat temp = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(selectbp, src);
Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
Log.i("CV", "image type:" + (temp.type() == CvType.CV_8UC3));
Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2HSV);
Utils.matToBitmap(dst, selectbptmp);
myImageView.setImageBitmap(selectbptmp);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Log.d("image-tag", "start to decode selected image now...");
InputStream input = getContentResolver().openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, options);
int raw_width = options.outWidth;
int raw_height = options.outHeight;
int max = Math.max(raw_width, raw_height);
int newWidth = raw_width;
int newHeight = raw_height;
int inSampleSize = 1;
if(max > max_size) {
newWidth = raw_width / 2;
newHeight = raw_height / 2;
while((newWidth/inSampleSize) > max_size || (newHeight/inSampleSize) > max_size) {
inSampleSize *=2;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
selectbp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
selectbptmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
myImageView.setImageBitmap(selectbptmp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"选择图像..."), PICK_IMAGE_REQUEST);
}
}
https://www.cnblogs.com/ckb58/p/6242040.html
http://xinzhi.wenda.so.com/a/1522931091614032
https://blog.csdn.net/qq_36992688/article/details/79214273