Created
December 29, 2014 16:27
-
-
Save wllmtrng/e874c1b59b3add14bade to your computer and use it in GitHub Desktop.
OpenCV Camera Android Java Example
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/HelloOpenCvView" | |
opencv:show_fps="true" | |
opencv:camera_id="any" /> | |
</LinearLayout> |
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.helloopencvcamera; | |
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.Mat; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.SurfaceView; | |
import android.view.WindowManager; | |
public class MainActivity extends Activity implements CvCameraViewListener2 { | |
private CameraBridgeViewBase mOpenCvCameraView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | |
setContentView(R.layout.activity_main); | |
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView); | |
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); | |
mOpenCvCameraView.setCvCameraViewListener(this); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
if (mOpenCvCameraView != null) | |
mOpenCvCameraView.disableView(); | |
} | |
public void onDestroy() { | |
super.onDestroy(); | |
if (mOpenCvCameraView != null) | |
mOpenCvCameraView.disableView(); | |
} | |
public void onCameraViewStarted(int width, int height) { | |
} | |
public void onCameraViewStopped() { | |
} | |
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { | |
return inputFrame.rgba(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
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 | |
public void onResume() { | |
super.onResume(); | |
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, | |
mLoaderCallback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment