• opencv3.0 在 android 上的使用


    1. 下载 OpenCV-3.0.0-android-sdk-1.zip
    2. 打开 intellj,新建立一个 android 工程后选择工程属性,导入模块(Import module from external model" with Eclipse option),选择文件夹 OpenCV-3.0.0-android-sdksdkjava
    3. 设置工程依赖
    package com.example.testopencv;
    
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.view.View;
    import android.widget.Button;
    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.android.Utils;
    import org.opencv.core.Mat;
    import org.opencv.core.Point;
    import org.opencv.core.Scalar;
    
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import org.opencv.imgproc.Imgproc;
    
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    
    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
    
        private static final int SELECT_PHOTO = 100;
        private Button selectbt;
        private Button processbt;
        private Bitmap image;
        private ImageView iv;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            selectbt = (Button) findViewById(R.id.button);
            processbt = (Button) findViewById(R.id.start);
            iv = (ImageView) findViewById(R.id.imageView);
    
    //选择图片
            selectbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, SELECT_PHOTO);
                }
            });
    
    
            processbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, MyActivity.this, mLoaderCallback);
                }
            });
    
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
            switch(requestCode) {
                case SELECT_PHOTO:
                    if(resultCode == RESULT_OK){
                        Uri selectedImage = imageReturnedIntent.getData();
                        InputStream imageStream = null;
                        try {
                            imageStream = getContentResolver().openInputStream(selectedImage);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        image = BitmapFactory.decodeStream(imageStream);
                    }
            }
        }
    
    //process button
        private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
            @Override
            public void onManagerConnected(int status) {
                if (status == LoaderCallbackInterface.SUCCESS ) {
                    mainProcess();
                } else {
                    super.onManagerConnected(status);
                }
            }
        };
    
    // 增加文字在图像上
        private void mainProcess() {
            Mat m = new Mat();
            Utils.bitmapToMat(this.image, m);
            Imgproc.putText(m, "=====OPENCV PUTTEXT TEST=====", new Point(20, 40), 3, 1, new Scalar(0, 255, 0, 255), 2);
            Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(m, bm);
            iv.setImageBitmap(bm);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
        }
    }
    
    

    增加权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.testopencv"
              android:versionCode="1"
              android:versionName="1.0">
    	<uses-sdk android:minSdkVersion="17"/>
    	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    	<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    		<activity android:name="MyActivity"
    		          android:label="@string/app_name">
    			<intent-filter>
    				<action android:name="android.intent.action.MAIN"/>
    				<category android:name="android.intent.category.LAUNCHER"/>
    			</intent-filter>
    		</activity>
    	</application>
    </manifest>
    
    

    ref

    http://answers.opencv.org/question/16993/display-image/

    APK download

    http://pan.baidu.com/s/1ntN9KBv

    source code

    http://pan.baidu.com/s/1i3D8Aqh

  • 相关阅读:
    移动端应用rem定义相对长度单位
    ionic4(angular) 生成browser平台的(webApp)在手机QQ浏览器不更新页面
    解决 git bash命令行执行git命令一直报错 segmentation fault
    MACBOOK OSX升级到10.15.3 Catalina 后 photoshop CS6(32位)不能用了
    自制操作系统笔记-第三章
    自制操作系统笔记-第2章
    自制操作系统笔记-第一章
    Vue学习笔记
    解决 MAC 终端上每次打开新窗口手动执行source ~/.bash_profile导出环境变量
    HTTPS的安全性
  • 原文地址:https://www.cnblogs.com/yuliyang/p/4861491.html
Copyright © 2020-2023  润新知