首先安裝好NDK, File-> Setting -Android SDK -> SDK Tools->NDK
配置
步驟:
1.創(chuàng)建一個項目,勾選 Include C++ support,然后一路next 下去
創(chuàng)建Android 項目
-
最后勾選 C++咽白。等待項目創(chuàng)建完成。(勾選C++ 是因為需要用到C++的配置)
創(chuàng)建Android 項目
3.接下來是導入model 項目非洲,添加項目依賴包。方便大家蜕径,我直接放在我的云盤上两踏。
鏈接:https://pan.baidu.com/s/1sH9PvgoZFI3Igy5knUY1MA
提取碼:x5e0
導入項目依賴
導入項目依賴
- 然后添加項目依賴, File - > Project Structure -> Dependencies - >Module dependency
添加依賴
-
選擇 openCVLibrary 添加依賴
完成依賴
注意:依賴的項目包可能和你的項目的版本不一致兜喻,你更新一下就好了梦染。
注意事項
到這里,基本的OpenCV就配置好了朴皆。接下來就是項目的編寫帕识。用于檢驗配置是否完成。
實現(xiàn)的項目效果:
1.從相冊中選擇一張照片遂铡,然后進行灰度處理肮疗。
activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="原圖"
android:textSize="16sp" />
<Button
android:id="@+id/btn_deals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="處理"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:id="@+id/img_original"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_margin="16dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/img_deals"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_margin="16dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
//最大
private double max_size = 1024;
//回調(diào)
private int PICK_IMAGE_REQUEST = 1;
//原圖, 處理后的圖片
private ImageView mImgOriginal, mImgDeals;
//原始Bitmap扒接、處理后的Bitmap
private Bitmap mOriginalBitmap, mDealBitmap;
//選擇圖片伪货、處理
private Button mBtnChoose, mBtnDeals;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onLoadOpenCVLibrary();
initView();
}
/**
* OpenCV庫靜態(tài)加載并初始化
*/
private void onLoadOpenCVLibrary() {
boolean load = OpenCVLoader.initDebug();
if (load) {
Log.e("CV", "Open CV Libraries loaded...");
}
}
private void initView() {
mImgDeals = findViewById(R.id.img_deals);
mImgOriginal = findViewById(R.id.img_original);
mBtnChoose = findViewById(R.id.btn_choose);
mBtnDeals = findViewById(R.id.btn_deals);
mBtnDeals.setOnClickListener(this);
mBtnChoose.setOnClickListener(this);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_choose: {//選擇
selectImage();
break;
}
case R.id.btn_deals: {//處理
convertGray();
break;
}
}
}
//選擇圖片
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "選擇圖像"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 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.e("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;
mOriginalBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),
null, options);
mDealBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),
null, options);
mImgOriginal.setImageBitmap(mOriginalBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void convertGray() {
Mat src = new Mat();
Mat temp = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(mDealBitmap, src);
Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);
Utils.matToBitmap(dst, mDealBitmap);
mImgDeals.setImageBitmap(mDealBitmap);
}
}
最終實現(xiàn)效果:
實現(xiàn)效果