Last active
August 6, 2018 09:00
-
-
Save Sren-Hmkn/29ce253bb5c70d5a122ccfaf89402703 to your computer and use it in GitHub Desktop.
CameraActivity initial setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView | |
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" | |
android:orientation="vertical" | |
tools:context=".CameraActivity"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
// this is the camera! | |
<org.opencv.android.JavaCameraView | |
android:layout_height="300dp" | |
android:layout_width="200dp" | |
android:id="@+id/cam_view" | |
android:layout_alignParentTop="true" | |
/> | |
</RelativeLayout> | |
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.myapplication; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.SurfaceView; | |
import org.opencv.android.BaseLoaderCallback; | |
import org.opencv.android.CameraBridgeViewBase; | |
import org.opencv.android.JavaCameraView; | |
import org.opencv.android.LoaderCallbackInterface; | |
import org.opencv.android.OpenCVLoader; | |
import org.opencv.core.CvType; | |
import org.opencv.core.Mat; | |
public class CameraActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener { | |
private static final String TAG = "CameraActivity"; | |
// Initialize OpenCVLoader | |
static { | |
if (!(OpenCVLoader.initDebug())) { | |
Log.d(TAG, " OpenCVLoader.initDebug(), working."); | |
} else { | |
Log.d(TAG, " OpenCVLoader.initDebug(), not working."); | |
} | |
} | |
JavaCameraView javaCameraView; | |
Mat usedMat; | |
// Override the BaseLoaderCallback-method from OpenCV, to manage initialization properly | |
BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) { | |
@Override | |
public void onManagerConnected(int status) { | |
switch (status) { | |
case BaseLoaderCallback.SUCCESS: | |
javaCameraView.enableView(); | |
break; | |
default: | |
super.onManagerConnected(status); | |
break; | |
} | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_camera); | |
javaCameraView = findViewById(R.id.cam_view); | |
javaCameraView.setVisibility(SurfaceView.VISIBLE); | |
javaCameraView.setCvCameraViewListener(this); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
if (javaCameraView != null) { | |
javaCameraView.disableView(); | |
} | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallBack)) { | |
Log.e(TAG, " OpenCVLoader.initAsync(), not working."); | |
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS); | |
} else { | |
Log.d(TAG, " OpenCVLoader.initAsync(), working."); | |
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallBack); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (javaCameraView != null) { | |
javaCameraView.disableView(); | |
} | |
} | |
@Override | |
public void onCameraViewStarted(int width, int height) { | |
usedMat = new Mat(height, width, CvType.CV_8UC4); | |
} | |
@Override | |
public void onCameraViewStopped() { | |
javaCameraView.disableView(); | |
} | |
@Override | |
public Mat onCameraFrame(Mat inputFrame) { | |
usedMat = inputFrame; | |
return usedMat; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment