Created
March 28, 2013 19:00
-
-
Save jayrambhia/5265868 to your computer and use it in GitHub Desktop.
First Android OpenCV Application.
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"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.imgloader" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="17" /> | |
<application | |
android:label="@string/app_name" | |
android:icon="@drawable/icon" | |
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > | |
<activity android:name="MainActivity" | |
android:label="@string/app_name" | |
android:screenOrientation="landscape" | |
android:configChanges="keyboardHidden|orientation"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
<supports-screens android:resizeable="true" | |
android:smallScreens="true" | |
android:normalScreens="true" | |
android:largeScreens="true" | |
android:anyDensity="true" /> | |
<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"/> | |
</manifest> |
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.imgloader; | |
import org.opencv.android.BaseLoaderCallback; | |
import org.opencv.android.CameraBridgeViewBase; | |
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; | |
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; | |
import org.opencv.android.LoaderCallbackInterface; | |
import org.opencv.android.OpenCVLoader; | |
import org.opencv.core.*; | |
//import org.opencv.highgui.Highgui; | |
import org.opencv.imgproc.Imgproc; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.SurfaceView; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.view.WindowManager; | |
public class MainActivity extends Activity implements CvCameraViewListener2, OnTouchListener { | |
private static final String TAG = "OCVSample::Activity"; | |
private Mat mRgba; | |
private Mat mGray; | |
private CameraBridgeViewBase mOpenCvCameraView; | |
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { | |
@Override | |
public void onManagerConnected(int status) { | |
switch (status) { | |
case LoaderCallbackInterface.SUCCESS: | |
{ | |
Log.i(TAG, "OpenCV loaded successfully"); | |
mOpenCvCameraView.enableView(); | |
mOpenCvCameraView.setOnTouchListener(MainActivity.this); | |
} break; | |
default: | |
{ | |
super.onManagerConnected(status); | |
} break; | |
} | |
} | |
}; | |
public MainActivity() { | |
Log.i(TAG, "Instantiated new " + this.getClass()); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
Log.i(TAG, "called onCreate"); | |
super.onCreate(savedInstanceState); | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | |
setContentView(R.layout.tutorial1_surface_view); | |
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view); | |
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); | |
mOpenCvCameraView.setCvCameraViewListener(this); | |
} | |
@Override | |
public void onPause() | |
{ | |
super.onPause(); | |
if (mOpenCvCameraView != null) | |
mOpenCvCameraView.disableView(); | |
} | |
@Override | |
public void onResume() | |
{ | |
super.onResume(); | |
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); | |
} | |
public void onDestroy() { | |
super.onDestroy(); | |
if (mOpenCvCameraView != null) | |
mOpenCvCameraView.disableView(); | |
} | |
public void onCameraViewStarted(int width, int height) { | |
mGray = new Mat(); | |
mRgba = new Mat(); | |
} | |
public void onCameraViewStopped() { | |
} | |
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { | |
mRgba = inputFrame.rgba(); | |
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGRA2GRAY); | |
return mGray; | |
} | |
} |
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:opencv="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<org.opencv.android.JavaCameraView | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:visibility="gone" | |
android:id="@+id/tutorial1_activity_java_surface_view" | |
opencv:show_fps="true" | |
opencv:camera_id="any" /> | |
<org.opencv.android.NativeCameraView | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:visibility="gone" | |
android:id="@+id/tutorial1_activity_native_surface_view" | |
opencv:show_fps="true" | |
opencv:camera_id="any" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment