Tool -> SDK Manager
SDK Platforms에 "Android 11.0 (R)"을 체크하고 OK
SDK Tools에 "Android SDK Build-Tools 33-rc1"과 "NDK (Side by side)"와 "CMake"가 표시하고 설치한 다음에 Apply 만약 이미 되어 있다면 OK
opencv GitHub에서 SDK를 다운받아야 한다.
최신 버전이라는 OpenCV 4.5.5의 "opencv-4.5.5-android-sdk.zip"을 다운로드 한다.
다운로드가 끝나면 압출풀기를 해주고, 폴더 경로를 확인한다.
File -> New -> New Project...
Native C++을 선택하고 Next
프로젝트 이름을 지어주고, Language는 Java, Minimum SDK는 API 30: Android 11.0 (R)을 선택한 후 Next
Finish
File -> New -> Import Module...
Source directory에 아까 다운로드 받은 SDK의 경로를 선택 OK
Finish
Android를 Project로 바꾸어 보면 sdk를 찾아 볼 수 있다.
File -> Project Structure...
Dependencies -> app -> + -> 3 Module Dependency
sdk 체크 -> OK
Apply -> OK
build.gradle에서 오류가 뜨네...ㅎ
res폴더 -> themes -> themes.xml에 코드 추가
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
res폴더 -> layout -> activity_main.xml 전체 삭제 후 코드 붙여넣기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<org.opencv.android.JavaCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
opencv:camera_id="any"
android:visibility="gone"
android:id="@+id/activity_surface_view" />
</LinearLayout>
얘도 오류가 나네..
Suppress: Add tools:ignore ~ 를 클릭
이건 일단 둠!
manifests -> 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.autofocus" android:required="false"/>
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
manifests -> AndroidManifest.xml에 코드 추가
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
MainActivity의 코드 중에 package 코드(맨 첫 줄)을 제외한 코드를 삭제 후 아래의 코드 붙여 넣기
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import java.util.Collections;
import java.util.List;
import static android.Manifest.permission.CAMERA;
public class MainActivity extends AppCompatActivity
implements CameraBridgeViewBase.CvCameraViewListener2 {
private static final String TAG = "opencv";
private Mat matInput;
private Mat matResult;
private CameraBridgeViewBase mOpenCvCameraView;
public native void ConvertRGBtoGray(long matAddrInput, long matAddrResult);
static {
System.loadLibrary("opencv_java4");
System.loadLibrary("native-lib");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.activity_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setCameraIndex(0); // front-camera(1), back-camera(0)
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d(TAG, "onResume :: Internal OpenCV library not found.");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
} else {
Log.d(TAG, "onResum :: OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
matInput = inputFrame.rgba();
if ( matResult == null )
matResult = new Mat(matInput.rows(), matInput.cols(), matInput.type());
ConvertRGBtoGray(matInput.getNativeObjAddr(), matResult.getNativeObjAddr());
return matResult;
}
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
return Collections.singletonList(mOpenCvCameraView);
}
//여기서부턴 퍼미션 관련 메소드
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
protected void onCameraPermissionGranted() {
List<? extends CameraBridgeViewBase> cameraViews = getCameraViewList();
if (cameraViews == null) {
return;
}
for (CameraBridgeViewBase cameraBridgeViewBase: cameraViews) {
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.setCameraPermissionGranted();
}
}
}
@Override
protected void onStart() {
super.onStart();
boolean havePermission = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
havePermission = false;
}
}
if (havePermission) {
onCameraPermissionGranted();
}
}
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onCameraPermissionGranted();
}else{
showDialogForPermission("앱을 실행하려면 퍼미션을 허가하셔야합니다.");
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@TargetApi(Build.VERSION_CODES.M)
private void showDialogForPermission(String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this);
builder.setTitle("알림");
builder.setMessage(msg);
builder.setCancelable(false);
builder.setPositiveButton("예", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
});
builder.setNegativeButton("아니오", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
builder.create().show();
}
}
오류가 엄청 나네...
1. ConvertRGBtoGray() 오류
Create JNI function ~ 클릭
2. native-lib.cpp 코드 삭제
드레그된 부분 삭제
3. native-lib.cpp 코드 추가
#include <opencv2/opencv.hpp>
using namespace cv;
Mat &matInput = *(Mat *)mat_addr_input;
Mat &matResult = *(Mat *)mat_addr_result;
cvtColor(matInput, matResult, COLOR_RGBA2GRAY);
project("opencvapp")
부분에 코드를 추가 및 수정한다.
cmpileSdkVersion
, minSdkVersion
, targetSdkVersion
을 build.gradle(:app)에 맞춰 각각 32, 30, 32로 바꿔주었다.apply plugin: 'kotlin-android'
를 주석처리하고 Try Again을 했더니 평화롭게 해결.실행을 시켜보도록 하겠다.
<clinit>
오류로 검색해보니 Edit Configurations..를 확인해보라는 글 발견.
Enable additional support ~ 를 체크하고 Apply -> OK
다시 실행했는데 안되자너 ㅠㅠ
걍 주석처리하고 실행
안된다..
이 프로젝트는 우선 버리고 새로 다시 시작해보자...!
도전 2로 이어짐...
시부레 실패한 걸 올리면 어떻게 수정을해야지