パーミッションの設定
マニュフェストの設定
今回はカメラからの入力画像を使用するため、Permissionの追加をAndroidManifest.xmlに行います。app/src/main/AndroidManifest.xml
に以下の3行を追加します。
package="faithit.image.opencv45fullscreenjava">
<!--↓↓↓↓追加↓↓↓↓ -->
<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"/>
<!--↑↑↑↑追加↑↑↑↑-->
<application
CameraViewの配置
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:background="?attr/fullscreenBackgroundColor"
android:theme="@style/ThemeOverlay.OpenCv45FullScreenJava.FullscreenContainer"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="?attr/fullscreenTextColor"
android:textSize="50sp"
android:textStyle="bold" />
<org.opencv.android.JavaCameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"
opencv:show_fps="true"/>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="@style/Widget.Theme.OpenCv45FullScreenJava.ButtonBar.Fullscreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight
="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
ライブラリの読み込み
//OpenCv+Camera用に追加
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraActivity;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
<!--↓↓↓↓変更↓↓↓↓ -->
//public class FullscreenActivity extends AppCompatActivity {
public class FullscreenActivity extends CameraActivity implements CvCameraViewListener2 {
<!--↑↑↑↑変更↑↑↑↑-->
<!--↓↓↓↓追加↓↓↓↓ --> //プレビュー要素保存用
private CameraBridgeViewBase mOpenCvCameraView;
<!--↑↑↑↑追加↑↑↑↑-->
OpenCvライブラリの初期化後に呼び出されるコールバックメソッドの追加
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;
}
}
}; // BaseLoaderCallback
//**********************************************************************************************
//opencv+camera
//**********************************************************************************************
//==============================================================================================
// onPause
//==============================================================================================
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
//==============================================================================================
// onResume
//==============================================================================================
@Override
public void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
//==============================================================================================
// getCameraViewList
//==============================================================================================
@Override
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
return Collections.singletonList(mOpenCvCameraView);
}
//==============================================================================================
// onDestroy
//==============================================================================================
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
//==============================================================================================
//onCameraViewStarted
//==============================================================================================
public void onCameraViewStarted(int width, int height) {
}
//==============================================================================================
// onCameraViewStopped
//==============================================================================================
public void onCameraViewStopped() {
}
//==============================================================================================
// onCameraFrame
//==============================================================================================
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
return inputFrame.rgba();
}
書き換えるとエラーが発生するので、修正を行う。
<!--↓↓↓↓変更↓↓↓↓ -->
//ActionBar actionBar = getSupportActionBar();
android.app.ActionBar actionBar = getActionBar();
<!--↑↑↑↑変更↑↑↑↑-->
参考サイト

Android で OpenCV のサンプル camerapreview を試す - Qiita
AndroidStudio で OpenCV をインポートする
の続きです。
概要
Android 用 OpenCV には、
8個のサンプルが同封されている。
そのうち camerapreview は、
基本となるサンプルなの...

Androidで OpenCV 4を使う方法とカメラライブビューの表示 - Qiita
この記事について
カメラ画像をネガポジ反転して出力するだけの簡単なAndroidアプリを作ります。
カメラ入力にはOpenCV4を使用します。
カメラ画像を用いて何らかのアプリを作るためのベースとなるプロジェクトとして利用できま...

AndroidでOpenCV3.0を使ってカメラ映像をグレースケールにする - Qiita
やりたいこと
画像認識、画像処理ライブラリOpenCVをAndroidのプロジェクトに組み込んで、カメラ映像に簡単な処理を加えてみます。一度組み込みに成功すれば、画像処理は変更可能と思いますので、簡単な例としてカメラ映像をグレー...
AndroidでOpenCVを使ってみよう - プロノワ -
OpenCVを利用したAndroidプログラミングの記事

androidのgetSupportActionBarのエラー
下記のコードのgetSupportActionBarのところにエラー(cannot resolve method)が出てしまいます。
何故でしょうか。
詳しい方、ご教示ください。
public class MainActivity extends Activity {
private WebView myW...
コメント