Android 프로그래밍 2

본문 바로가기
사이트 내 전체검색


Android 프로그래밍 2
Android 프로그래밍 2

5. OpenCV with Kotlin

페이지 정보

작성자 관리자 댓글 0건 조회 2,255회 작성일 20-03-15 20:46

본문

5. OpenCV with Kotlin

1. 새프로젝트 생성



1.PNG

 

2. OpenCV 라이브러리 import


안드로드 스튜디오 프젝트에서 OpenCV라이브러리를 사용하기 위해서 import가 필요하다.

File -> New -> Import Module
 


2.PNG



C:\Android\OpenCV-android-sdk\sdk 폴더를 선택한다.

Module name은 :opencv 를 넣어준다.




다음은 OpenCV 모듈 Dependencies를 설정한다.


File -> Project Structure -> Dependencies 를 선택한다.





3.PNG


Modules에서 app 선택 후 Dependencies에서 plus( + )를 클릭하여 Module Dependecy 를 추가한다.





4.PNG


opencv를 체크하고 OK를 클릭한다.




3. CMakeLists.txt 수정


# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)


set(pathPROJECT D:/Android6/OpencvTest)

# 프로젝트 폴더를 입력한다.


set(pathOPENCV ${pathPROJECT}/opencv)
set(pathLIBOPENCV_JAVA ${pathOPENCV}/native/libs/${ANDROID_ABI}/libopencv_java4.so)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(${pathOPENCV}/native/jni/include)

 

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)


add_library(lib_opencv SHARED  IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathLIBOPENCV_JAVA})

 

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib
        lib_opencv

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})



4. Native-lib.cpp에 함수 추가

 

extern "C"
JNIEXPORT void JNICALL
Java_kr_co_leelab_opencvtest_MainActivity_helloworldImage(JNIEnv *env, jobject thiz, jlong input, jlong output) {
   // TODO
}
 

// kr_co_leelab_opencvtest : 패키지명에서 (.)점을 (_)언더바로 수정하면 된다.

// helloworldImage : 함수명입니다.
// TODO 부분에 실제 내용을 적어준다.
여기는 OpenCV CPP  사용하는 것처럼 사용하면 된다.


    Mat &inMat = *(Mat *)input;
    Mat &resultMat = *(Mat *)output;

    cvtColor(inMat, resultMat, COLOR_RGB2GRAY);
    putText(resultMat,"Hello, OpenCV",Point(40,100), 2, 2, Scalar(255,0,0));


카메라에서 받은 이미지를 그레이로 변환하고 문자를 추가하도록 수정합니다.



5. MainActivity.kt 에 사용할 함수

 

    external fun helloworldImage(): String
    companion object {
        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
            System.loadLibrary("opencv_java4");
        }
    }



6. 프로젝트 권한 설정
 

AndroidManifest.xml에서 외부 파일 읽기를 허용한다.


<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus"  android:required="false"/>




댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.