第九章 Android Studio 配置 OpenCV (不使用OpenCvManager)

首先安裝好NDK, File-> Setting -Android SDK -> SDK Tools->NDK


配置

步驟:
1.創(chuàng)建一個項目,勾選 Include C++ support,然后一路next 下去


創(chuàng)建Android 項目
  1. 最后勾選 C++。等待項目創(chuàng)建完成。(勾選C++ 是因為需要用到C++的配置)


    創(chuàng)建Android 項目

3.接下來是導入model 項目,添加項目依賴包。方便大家,我直接放在我的云盤上。
鏈接:https://pan.baidu.com/s/1sH9PvgoZFI3Igy5knUY1MA
提取碼:x5e0

導入項目依賴
導入項目依賴
  1. 然后添加項目依賴, File - > Project Structure -> Dependencies - >Module dependency
添加依賴
  1. 選擇 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)效果

github地址:https://github.com/wangxin3119/openCvDemo01

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容