Fix Android samples for devices having front camera only (Nexus 7)
This commit is contained in:
parent
fc93518449
commit
067744d071
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample-15puzzle::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -26,34 +26,28 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public synchronized boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
synchronized (this) {
|
|
||||||
releaseCamera();
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||||
if (!mCamera.isOpened()) {
|
if (!mCamera.isOpened()) {
|
||||||
releaseCamera();
|
releaseCamera();
|
||||||
Log.e(TAG, "Failed to open native camera");
|
Log.e(TAG, "Can't open native camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public synchronized void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
@ -75,27 +69,24 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (true) {
|
while (true) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
@ -120,7 +111,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
Log.i(TAG, "Finishing processing thread");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,9 +15,8 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
/** Activity class implements LoaderCallbackInterface to handle OpenCV initialization status **/
|
/** Activity class implements LoaderCallbackInterface to handle OpenCV initialization status **/
|
||||||
public class puzzle15Activity extends Activity
|
public class puzzle15Activity extends Activity {
|
||||||
{
|
private static final String TAG = "OCVSample::Activity";
|
||||||
private static final String TAG = "Sample::Activity";
|
|
||||||
|
|
||||||
private MenuItem mItemNewGame;
|
private MenuItem mItemNewGame;
|
||||||
private MenuItem mItemToggleNumbers;
|
private MenuItem mItemToggleNumbers;
|
||||||
@ -33,6 +32,7 @@ public class puzzle15Activity extends Activity
|
|||||||
// Create and set View
|
// Create and set View
|
||||||
mView = new puzzle15View(mAppContext);
|
mView = new puzzle15View(mAppContext);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -47,6 +47,7 @@ public class puzzle15Activity extends Activity
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -76,7 +77,7 @@ public class puzzle15Activity extends Activity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -84,12 +85,11 @@ public class puzzle15Activity extends Activity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,16 +97,15 @@ public class puzzle15Activity extends Activity
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemNewGame = menu.add("Start new game");
|
mItemNewGame = menu.add("Start new game");
|
||||||
mItemToggleNumbers = menu.add("Show/hide tile numbers");
|
mItemToggleNumbers = menu.add("Show/hide tile numbers");
|
||||||
return true;
|
return true;
|
||||||
@ -114,12 +113,10 @@ public class puzzle15Activity extends Activity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemNewGame) {
|
if (item == mItemNewGame)
|
||||||
synchronized (mView) {
|
|
||||||
mView.startNewGame();
|
mView.startNewGame();
|
||||||
}
|
else if (item == mItemToggleNumbers)
|
||||||
} else if (item == mItemToggleNumbers)
|
|
||||||
mView.tolggleTileNumbers();
|
mView.tolggleTileNumbers();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@ import android.view.View;
|
|||||||
import android.view.View.OnTouchListener;
|
import android.view.View.OnTouchListener;
|
||||||
|
|
||||||
public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mRgba15;
|
private Mat mRgba15;
|
||||||
private Mat[] mCells;
|
private Mat[] mCells;
|
||||||
@ -42,14 +44,17 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|||||||
mTextHeights[i] = (int) s.height;
|
mTextHeights[i] = (int) s.height;
|
||||||
mTextWidths[i] = (int) s.width;
|
mTextWidths[i] = (int) s.width;
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "called surfaceCreated");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mat before usage
|
// initialize Mat before usage
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
}
|
}
|
||||||
|
|
||||||
super.surfaceCreated(holder);
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +107,14 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|||||||
startNewGame();
|
startNewGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startNewGame() {
|
private void drawGrid(int cols, int rows) {
|
||||||
|
for (int i = 1; i < gridSize; i++) {
|
||||||
|
Core.line(mRgba15, new Point(0, i * rows / gridSize), new Point(cols, i * rows / gridSize), new Scalar(0, 255, 0, 255), 3);
|
||||||
|
Core.line(mRgba15, new Point(i * cols / gridSize, 0), new Point(i * cols / gridSize, rows), new Scalar(0, 255, 0, 255), 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void startNewGame() {
|
||||||
do {
|
do {
|
||||||
shuffle(mIndexses);
|
shuffle(mIndexses);
|
||||||
} while (!isPuzzleSolvable());
|
} while (!isPuzzleSolvable());
|
||||||
@ -154,13 +166,6 @@ public class puzzle15View extends SampleCvViewBase implements OnTouchListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawGrid(int cols, int rows) {
|
|
||||||
for (int i = 1; i < gridSize; i++) {
|
|
||||||
Core.line(mRgba15, new Point(0, i * rows / gridSize), new Point(cols, i * rows / gridSize), new Scalar(0, 255, 0, 255), 3);
|
|
||||||
Core.line(mRgba15, new Point(i * cols / gridSize, 0), new Point(i * cols / gridSize, rows), new Scalar(0, 255, 0, 255), 3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
super.run();
|
super.run();
|
||||||
|
@ -13,8 +13,8 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class ColorBlobDetectionActivity extends Activity {
|
public class ColorBlobDetectionActivity extends Activity {
|
||||||
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private static final String TAG = "Sample-ColorBlobDetection::Activity";
|
|
||||||
private ColorBlobDetectionView mView;
|
private ColorBlobDetectionView mView;
|
||||||
|
|
||||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||||
@ -27,6 +27,7 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
// Create and set View
|
// Create and set View
|
||||||
mView = new ColorBlobDetectionView(mAppContext);
|
mView = new ColorBlobDetectionView(mAppContext);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -41,6 +42,7 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -70,7 +72,7 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -78,12 +80,11 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,7 +92,7 @@ public class ColorBlobDetectionActivity extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
|
@ -22,29 +22,27 @@ import android.view.View;
|
|||||||
import android.view.View.OnTouchListener;
|
import android.view.View.OnTouchListener;
|
||||||
|
|
||||||
public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchListener {
|
public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchListener {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
|
|
||||||
private boolean mIsColorSelected = false;
|
private boolean mIsColorSelected = false;
|
||||||
private Scalar mBlobColorRgba = new Scalar(255);
|
private Scalar mBlobColorRgba = new Scalar(255);
|
||||||
private Scalar mBlobColorHsv = new Scalar(255);
|
private Scalar mBlobColorHsv = new Scalar(255);
|
||||||
private ColorBlobDetector mDetector = new ColorBlobDetector();
|
private ColorBlobDetector mDetector = new ColorBlobDetector();
|
||||||
private Mat mSpectrum = new Mat();
|
private Mat mSpectrum = new Mat();
|
||||||
private static Size SPECTRUM_SIZE = new Size(200, 32);
|
private static Size SPECTRUM_SIZE = new Size(200, 32);
|
||||||
|
|
||||||
// Logcat tag
|
|
||||||
private static final String TAG = "Sample-ColorBlobDetection::View";
|
|
||||||
|
|
||||||
private static final Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
|
private static final Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
|
||||||
|
|
||||||
|
|
||||||
public ColorBlobDetectionView(Context context) {
|
public ColorBlobDetectionView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
setOnTouchListener(this);
|
setOnTouchListener(this);
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "called surfaceCreated");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mat before usage
|
// initialize Mat before usage
|
||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
@ -53,6 +51,14 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
|||||||
super.surfaceCreated(holder);
|
super.surfaceCreated(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
|
||||||
|
Mat pointMatRgba = new Mat();
|
||||||
|
Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
|
||||||
|
Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||||
|
|
||||||
|
return new Scalar(pointMatRgba.get(0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
public boolean onTouch(View v, MotionEvent event) {
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
int cols = mRgba.cols();
|
int cols = mRgba.cols();
|
||||||
int rows = mRgba.rows();
|
int rows = mRgba.rows();
|
||||||
@ -130,14 +136,6 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
|||||||
return bmp;
|
return bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
|
|
||||||
Mat pointMatRgba = new Mat();
|
|
||||||
Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
|
|
||||||
Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
|
||||||
|
|
||||||
return new Scalar(pointMatRgba.get(0, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
super.run();
|
super.run();
|
||||||
|
@ -12,6 +12,16 @@ import org.opencv.core.Scalar;
|
|||||||
import org.opencv.imgproc.Imgproc;
|
import org.opencv.imgproc.Imgproc;
|
||||||
|
|
||||||
public class ColorBlobDetector {
|
public class ColorBlobDetector {
|
||||||
|
// Lower and Upper bounds for range checking in HSV color space
|
||||||
|
private Scalar mLowerBound = new Scalar(0);
|
||||||
|
private Scalar mUpperBound = new Scalar(0);
|
||||||
|
// Minimum contour area in percent for contours filtering
|
||||||
|
private static double mMinContourArea = 0.1;
|
||||||
|
// Color radius for range checking in HSV color space
|
||||||
|
private Scalar mColorRadius = new Scalar(25,50,50,0);
|
||||||
|
private Mat mSpectrum = new Mat();
|
||||||
|
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
|
||||||
|
|
||||||
public void setColorRadius(Scalar radius) {
|
public void setColorRadius(Scalar radius) {
|
||||||
mColorRadius = radius;
|
mColorRadius = radius;
|
||||||
}
|
}
|
||||||
@ -94,14 +104,4 @@ public class ColorBlobDetector {
|
|||||||
public List<MatOfPoint> getContours() {
|
public List<MatOfPoint> getContours() {
|
||||||
return mContours;
|
return mContours;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lower and Upper bounds for range checking in HSV color space
|
|
||||||
private Scalar mLowerBound = new Scalar(0);
|
|
||||||
private Scalar mUpperBound = new Scalar(0);
|
|
||||||
// Minimum contour area in percent for contours filtering
|
|
||||||
private static double mMinContourArea = 0.1;
|
|
||||||
// Color radius for range checking in HSV color space
|
|
||||||
private Scalar mColorRadius = new Scalar(25,50,50,0);
|
|
||||||
private Mat mSpectrum = new Mat();
|
|
||||||
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -26,34 +26,28 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public synchronized boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
synchronized (this) {
|
|
||||||
releaseCamera();
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||||
if (!mCamera.isOpened()) {
|
if (!mCamera.isOpened()) {
|
||||||
releaseCamera();
|
releaseCamera();
|
||||||
Log.e(TAG, "Failed to open native camera");
|
Log.e(TAG, "Can't open native camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public synchronized void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
@ -75,27 +69,24 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (true) {
|
while (true) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
@ -121,7 +112,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
Log.i(TAG, "Finishing processing thread");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -38,8 +38,4 @@ public class DetectionBasedTracker
|
|||||||
private static native void nativeStop(long thiz);
|
private static native void nativeStop(long thiz);
|
||||||
private static native void nativeSetFaceSize(long thiz, int size);
|
private static native void nativeSetFaceSize(long thiz, int size);
|
||||||
private static native void nativeDetect(long thiz, long inputImage, long faces);
|
private static native void nativeDetect(long thiz, long inputImage, long faces);
|
||||||
|
|
||||||
static {
|
|
||||||
System.loadLibrary("detection_based_tracker");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class FdActivity extends Activity {
|
public class FdActivity extends Activity {
|
||||||
private static final String TAG = "Sample-FD::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private MenuItem mItemFace50;
|
private MenuItem mItemFace50;
|
||||||
private MenuItem mItemFace40;
|
private MenuItem mItemFace40;
|
||||||
@ -42,6 +42,7 @@ public class FdActivity extends Activity {
|
|||||||
mView.setDetectorType(mDetectorType);
|
mView.setDetectorType(mDetectorType);
|
||||||
mView.setMinFaceSize(0.2f);
|
mView.setMinFaceSize(0.2f);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -56,6 +57,7 @@ public class FdActivity extends Activity {
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -80,28 +82,27 @@ public class FdActivity extends Activity {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public FdActivity() {
|
public FdActivity() {
|
||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
|
||||||
mDetectorName = new String[2];
|
mDetectorName = new String[2];
|
||||||
mDetectorName[FdView.JAVA_DETECTOR] = "Java";
|
mDetectorName[FdView.JAVA_DETECTOR] = "Java";
|
||||||
mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
|
mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (mView != null)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +110,7 @@ public class FdActivity extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
@ -117,7 +118,7 @@ public class FdActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemFace50 = menu.add("Face size 50%");
|
mItemFace50 = menu.add("Face size 50%");
|
||||||
mItemFace40 = menu.add("Face size 40%");
|
mItemFace40 = menu.add("Face size 40%");
|
||||||
mItemFace30 = menu.add("Face size 30%");
|
mItemFace30 = menu.add("Face size 30%");
|
||||||
@ -128,7 +129,7 @@ public class FdActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemFace50)
|
if (item == mItemFace50)
|
||||||
mView.setMinFaceSize(0.5f);
|
mView.setMinFaceSize(0.5f);
|
||||||
else if (item == mItemFace40)
|
else if (item == mItemFace40)
|
||||||
@ -137,8 +138,7 @@ public class FdActivity extends Activity {
|
|||||||
mView.setMinFaceSize(0.3f);
|
mView.setMinFaceSize(0.3f);
|
||||||
else if (item == mItemFace20)
|
else if (item == mItemFace20)
|
||||||
mView.setMinFaceSize(0.2f);
|
mView.setMinFaceSize(0.2f);
|
||||||
else if (item == mItemType)
|
else if (item == mItemType) {
|
||||||
{
|
|
||||||
mDetectorType = (mDetectorType + 1) % mDetectorName.length;
|
mDetectorType = (mDetectorType + 1) % mDetectorName.length;
|
||||||
item.setTitle(mDetectorName[mDetectorType]);
|
item.setTitle(mDetectorName[mDetectorType]);
|
||||||
mView.setDetectorType(mDetectorType);
|
mView.setDetectorType(mDetectorType);
|
||||||
|
@ -22,7 +22,7 @@ import android.util.Log;
|
|||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
class FdView extends SampleCvViewBase {
|
class FdView extends SampleCvViewBase {
|
||||||
private static final String TAG = "Sample-FD::View";
|
private static final String TAG = "OCVSample::View";
|
||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mGray;
|
private Mat mGray;
|
||||||
private File mCascadeFile;
|
private File mCascadeFile;
|
||||||
@ -62,6 +62,7 @@ class FdView extends SampleCvViewBase {
|
|||||||
super(context);
|
super(context);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// load cascade file from application resources
|
||||||
InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
|
InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
|
||||||
File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
|
File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
|
||||||
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
|
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
|
||||||
@ -90,10 +91,13 @@ class FdView extends SampleCvViewBase {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
|
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "called surfaceCreated");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
|
@ -10,7 +10,7 @@ import android.graphics.Paint;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class FpsMeter {
|
public class FpsMeter {
|
||||||
private static final String TAG = "Sample::FpsMeter";
|
private static final String TAG = "OCVSample::FpsMeter";
|
||||||
int step;
|
int step;
|
||||||
int framesCouner;
|
int framesCouner;
|
||||||
double freq;
|
double freq;
|
||||||
|
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -28,34 +28,28 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public synchronized boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
synchronized (this) {
|
|
||||||
releaseCamera();
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||||
if (!mCamera.isOpened()) {
|
if (!mCamera.isOpened()) {
|
||||||
Log.e(TAG, "Failed to open native camera");
|
|
||||||
releaseCamera();
|
releaseCamera();
|
||||||
|
Log.e(TAG, "Can't open native camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public synchronized void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
@ -77,28 +71,23 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
Log.i(TAG, "surfaceDestroyed2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
mFps.init();
|
mFps.init();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -128,7 +117,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
Log.i(TAG, "Finishing processing thread");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ import android.graphics.Paint;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class FpsMeter {
|
public class FpsMeter {
|
||||||
private static final String TAG = "Sample::FpsMeter";
|
private static final String TAG = "OCVSample::FpsMeter";
|
||||||
int step;
|
int step;
|
||||||
int framesCouner;
|
int framesCouner;
|
||||||
double freq;
|
double freq;
|
||||||
|
@ -15,8 +15,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class ImageManipulationsActivity extends Activity {
|
public class ImageManipulationsActivity extends Activity {
|
||||||
|
private static final String TAG = "OCVSample::Activity";
|
||||||
private static final String TAG = "Sample-ImageManipulations::Activity";
|
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
public static final int VIEW_MODE_HIST = 1;
|
public static final int VIEW_MODE_HIST = 1;
|
||||||
@ -35,11 +34,10 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
private MenuItem mItemPreviewZoom;
|
private MenuItem mItemPreviewZoom;
|
||||||
private MenuItem mItemPreviewPixelize;
|
private MenuItem mItemPreviewPixelize;
|
||||||
private MenuItem mItemPreviewPosterize;
|
private MenuItem mItemPreviewPosterize;
|
||||||
|
private ImageManipulationsView mView;
|
||||||
|
|
||||||
public static int viewMode = VIEW_MODE_RGBA;
|
public static int viewMode = VIEW_MODE_RGBA;
|
||||||
|
|
||||||
private ImageManipulationsView mView;
|
|
||||||
|
|
||||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||||
@Override
|
@Override
|
||||||
public void onManagerConnected(int status) {
|
public void onManagerConnected(int status) {
|
||||||
@ -50,6 +48,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
// Create and set View
|
// Create and set View
|
||||||
mView = new ImageManipulationsView(mAppContext);
|
mView = new ImageManipulationsView(mAppContext);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -64,6 +63,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -93,7 +93,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -101,12 +101,11 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,7 +113,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
@ -122,7 +121,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewHist = menu.add("Histograms");
|
mItemPreviewHist = menu.add("Histograms");
|
||||||
mItemPreviewCanny = menu.add("Canny");
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
@ -136,7 +135,7 @@ public class ImageManipulationsActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemPreviewRGBA)
|
if (item == mItemPreviewRGBA)
|
||||||
viewMode = VIEW_MODE_RGBA;
|
viewMode = VIEW_MODE_RGBA;
|
||||||
if (item == mItemPreviewHist)
|
if (item == mItemPreviewHist)
|
||||||
|
@ -21,6 +21,7 @@ import android.util.Log;
|
|||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
class ImageManipulationsView extends SampleCvViewBase {
|
class ImageManipulationsView extends SampleCvViewBase {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
private Size mSize0;
|
private Size mSize0;
|
||||||
private Size mSizeRgba;
|
private Size mSizeRgba;
|
||||||
private Size mSizeRgbaInner;
|
private Size mSizeRgbaInner;
|
||||||
@ -28,20 +29,23 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mGray;
|
private Mat mGray;
|
||||||
private Mat mIntermediateMat;
|
private Mat mIntermediateMat;
|
||||||
private Mat mHist, mMat0;
|
private Mat mHist;
|
||||||
private MatOfInt mChannels[], mHistSize;
|
private Mat mMat0;
|
||||||
|
private MatOfInt mChannels[];
|
||||||
|
private MatOfInt mHistSize;
|
||||||
private int mHistSizeNum;
|
private int mHistSizeNum;
|
||||||
private MatOfFloat mRanges;
|
private MatOfFloat mRanges;
|
||||||
private Scalar mColorsRGB[], mColorsHue[], mWhilte;
|
private Scalar mColorsRGB[];
|
||||||
private Point mP1, mP2;
|
private Scalar mColorsHue[];
|
||||||
float mBuff[];
|
private Scalar mWhilte;
|
||||||
|
private Point mP1;
|
||||||
|
private Point mP2;
|
||||||
|
private float mBuff[];
|
||||||
private Mat mRgbaInnerWindow;
|
private Mat mRgbaInnerWindow;
|
||||||
private Mat mGrayInnerWindow;
|
private Mat mGrayInnerWindow;
|
||||||
private Mat mBlurWindow;
|
private Mat mBlurWindow;
|
||||||
private Mat mZoomWindow;
|
private Mat mZoomWindow;
|
||||||
private Mat mZoomCorner;
|
private Mat mZoomCorner;
|
||||||
|
|
||||||
private Mat mSepiaKernel;
|
private Mat mSepiaKernel;
|
||||||
|
|
||||||
public ImageManipulationsView(Context context) {
|
public ImageManipulationsView(Context context) {
|
||||||
@ -52,10 +56,13 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
mSepiaKernel.put(1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
|
mSepiaKernel.put(1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
|
||||||
mSepiaKernel.put(2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
|
mSepiaKernel.put(2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
|
||||||
mSepiaKernel.put(3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
|
mSepiaKernel.put(3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
|
||||||
|
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "called surfaceCreated");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
@ -234,7 +241,7 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
Utils.matToBitmap(mRgba, bmp);
|
Utils.matToBitmap(mRgba, bmp);
|
||||||
return bmp;
|
return bmp;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample-ImageManipulations::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -28,34 +28,28 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public synchronized boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
synchronized (this) {
|
|
||||||
releaseCamera();
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||||
if (!mCamera.isOpened()) {
|
if (!mCamera.isOpened()) {
|
||||||
releaseCamera();
|
releaseCamera();
|
||||||
Log.e(TAG, "Failed to open native camera");
|
Log.e(TAG, "Can't open native camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public synchronized void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
@ -77,37 +71,32 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
mFps.init();
|
mFps.init();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera == null) {
|
if (mCamera == null)
|
||||||
Log.i(TAG, "mCamera == null");
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (!mCamera.grab()) {
|
if (!mCamera.grab()) {
|
||||||
Log.e(TAG, "mCamera.grab() failed");
|
Log.e(TAG, "mCamera.grab() failed");
|
||||||
@ -129,7 +118,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
Log.i(TAG, "Finishing processing thread");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ import android.view.Window;
|
|||||||
|
|
||||||
public class Sample0Base extends Activity {
|
public class Sample0Base extends Activity {
|
||||||
|
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private MenuItem mItemPreviewRGBA;
|
private MenuItem mItemPreviewRGBA;
|
||||||
private MenuItem mItemPreviewGray;
|
private MenuItem mItemPreviewGray;
|
||||||
@ -23,14 +23,14 @@ public class Sample0Base extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
super.onPause();
|
super.onPause();
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(this).create();
|
AlertDialog ad = new AlertDialog.Builder(this).create();
|
||||||
@ -49,7 +49,7 @@ public class Sample0Base extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
mView = new Sample0View(this);
|
mView = new Sample0View(this);
|
||||||
@ -58,7 +58,7 @@ public class Sample0Base extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
return true;
|
return true;
|
||||||
@ -66,7 +66,7 @@ public class Sample0Base extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemPreviewRGBA)
|
if (item == mItemPreviewRGBA)
|
||||||
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
|
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
|
||||||
else if (item == mItemPreviewGray)
|
else if (item == mItemPreviewGray)
|
||||||
|
@ -6,20 +6,21 @@ import android.util.Log;
|
|||||||
|
|
||||||
class Sample0View extends SampleViewBase {
|
class Sample0View extends SampleViewBase {
|
||||||
|
|
||||||
private static final String TAG = "Sample::View";
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
|
|
||||||
int mSize;
|
int mSize;
|
||||||
int[] mRGBA;
|
int[] mRGBA;
|
||||||
private Bitmap mBitmap;
|
private Bitmap mBitmap;
|
||||||
private int mViewMode;
|
private int mViewMode;
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
|
||||||
public static final int VIEW_MODE_GRAY = 1;
|
|
||||||
|
|
||||||
|
|
||||||
public Sample0View(Context context) {
|
public Sample0View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
mSize = 0;
|
mSize = 0;
|
||||||
mViewMode = VIEW_MODE_RGBA;
|
mViewMode = VIEW_MODE_RGBA;
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -64,7 +65,7 @@ class Sample0View extends SampleViewBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||||
Log.i(TAG, "onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
Log.i(TAG, "called onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||||
/* Create a bitmap that will be used through to calculate the image to */
|
/* Create a bitmap that will be used through to calculate the image to */
|
||||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||||
mRGBA = new int[previewWidth * previewHeight];
|
mRGBA = new int[previewWidth * previewHeight];
|
||||||
@ -72,7 +73,7 @@ class Sample0View extends SampleViewBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStopped() {
|
protected void onPreviewStopped() {
|
||||||
Log.i(TAG, "onPreviewStopped");
|
Log.i(TAG, "called onPreviewStopped");
|
||||||
if(mBitmap != null) {
|
if(mBitmap != null) {
|
||||||
mBitmap.recycle();
|
mBitmap.recycle();
|
||||||
mBitmap = null;
|
mBitmap = null;
|
||||||
@ -84,7 +85,7 @@ class Sample0View extends SampleViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setViewMode(int viewMode) {
|
public void setViewMode(int viewMode) {
|
||||||
Log.i(TAG, "setViewMode("+viewMode+")");
|
Log.i(TAG, "called setViewMode("+viewMode+")");
|
||||||
mViewMode = viewMode;
|
mViewMode = viewMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
@ -52,19 +52,30 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.setPreviewDisplay(null);
|
mCamera.setPreviewDisplay(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean openCamera() {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mCamera = Camera.open();
|
mCamera = Camera.open();
|
||||||
}
|
}
|
||||||
catch (Exception e){
|
catch (Exception e){
|
||||||
Log.e(TAG, "Camera is not available (in use or does not exist)");
|
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||||
|
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
||||||
|
try {
|
||||||
|
mCamera = Camera.open(camIdx);
|
||||||
|
}
|
||||||
|
catch (RuntimeException e) {
|
||||||
|
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(mCamera == null) {
|
if(mCamera == null) {
|
||||||
Log.e(TAG, "Failed to open camera");
|
Log.e(TAG, "Can't open any camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,11 +88,12 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
camera.addCallbackBuffer(mBuffer);
|
camera.addCallbackBuffer(mBuffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
@ -93,11 +105,9 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
onPreviewStopped();
|
onPreviewStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
Log.i(TAG, "setupCamera - " + width + "x" + height);
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
mFrameWidth = width;
|
mFrameWidth = width;
|
||||||
@ -147,10 +157,9 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
// stop preview before making changes
|
// stop preview before making changes
|
||||||
try {
|
try {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
@ -163,12 +172,12 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
||||||
@ -191,7 +200,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
mThreadRun = true;
|
mThreadRun = true;
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (mThreadRun) {
|
while (mThreadRun) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
@ -214,6 +223,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.i(TAG, "Finishing processing thread");
|
Log.i(TAG, "Finished processing thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class Sample1Java extends Activity {
|
public class Sample1Java extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private MenuItem mItemPreviewRGBA;
|
private MenuItem mItemPreviewRGBA;
|
||||||
private MenuItem mItemPreviewGray;
|
private MenuItem mItemPreviewGray;
|
||||||
@ -32,6 +32,7 @@ public class Sample1Java extends Activity {
|
|||||||
// Create and set View
|
// Create and set View
|
||||||
mView = new Sample1View(mAppContext);
|
mView = new Sample1View(mAppContext);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -76,7 +77,7 @@ public class Sample1Java extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -84,12 +85,11 @@ public class Sample1Java extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,7 +97,7 @@ public class Sample1Java extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
@ -105,7 +105,7 @@ public class Sample1Java extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
mItemPreviewCanny = menu.add("Canny");
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
@ -114,7 +114,7 @@ public class Sample1Java extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemPreviewRGBA) {
|
if (item == mItemPreviewRGBA) {
|
||||||
mView.setViewMode(Sample1View.VIEW_MODE_RGBA);
|
mView.setViewMode(Sample1View.VIEW_MODE_RGBA);
|
||||||
} else if (item == mItemPreviewGray) {
|
} else if (item == mItemPreviewGray) {
|
||||||
|
@ -13,6 +13,7 @@ import android.graphics.Bitmap;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
class Sample1View extends SampleViewBase {
|
class Sample1View extends SampleViewBase {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
public static final int VIEW_MODE_GRAY = 1;
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
@ -28,11 +29,13 @@ class Sample1View extends SampleViewBase {
|
|||||||
public Sample1View(Context context) {
|
public Sample1View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
mViewMode = VIEW_MODE_RGBA;
|
mViewMode = VIEW_MODE_RGBA;
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||||
synchronized (this) {
|
Log.i(TAG, "called onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||||
|
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||||
@ -42,12 +45,14 @@ class Sample1View extends SampleViewBase {
|
|||||||
|
|
||||||
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStopped() {
|
protected void onPreviewStopped() {
|
||||||
|
Log.i(TAG, "called onPreviewStopped");
|
||||||
|
|
||||||
if(mBitmap != null) {
|
if(mBitmap != null) {
|
||||||
mBitmap.recycle();
|
mBitmap.recycle();
|
||||||
|
mBitmap = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
@ -101,6 +106,7 @@ class Sample1View extends SampleViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setViewMode(int viewMode) {
|
public void setViewMode(int viewMode) {
|
||||||
|
Log.i(TAG, "called setViewMode("+viewMode+")");
|
||||||
mViewMode = viewMode;
|
mViewMode = viewMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,14 +16,14 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private int mFrameWidth;
|
private int mFrameWidth;
|
||||||
private int mFrameHeight;
|
private int mFrameHeight;
|
||||||
private byte[] mFrame;
|
private byte[] mFrame;
|
||||||
private boolean mThreadRun;
|
private volatile boolean mThreadRun;
|
||||||
private byte[] mBuffer;
|
private byte[] mBuffer;
|
||||||
private SurfaceTexture mSf;
|
private SurfaceTexture mSf;
|
||||||
|
|
||||||
@ -53,11 +53,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
releaseCamera();
|
mCamera = null;
|
||||||
|
|
||||||
|
try {
|
||||||
mCamera = Camera.open();
|
mCamera = Camera.open();
|
||||||
|
}
|
||||||
|
catch (Exception e){
|
||||||
|
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||||
|
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
||||||
|
try {
|
||||||
|
mCamera = Camera.open(camIdx);
|
||||||
|
}
|
||||||
|
catch (RuntimeException e) {
|
||||||
|
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(mCamera == null) {
|
if(mCamera == null) {
|
||||||
Log.e(TAG, "Can't open camera!");
|
Log.e(TAG, "Can't open any camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,16 +88,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
camera.addCallbackBuffer(mBuffer);
|
camera.addCallbackBuffer(mBuffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
@ -87,10 +105,9 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
onPreviewStopped();
|
onPreviewStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
mFrameWidth = width;
|
mFrameWidth = width;
|
||||||
@ -140,21 +157,27 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
|
// stop preview before making changes
|
||||||
|
try {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
} catch (Exception e){
|
||||||
|
// ignore: tried to stop a non-existent preview
|
||||||
|
}
|
||||||
|
|
||||||
|
// start preview with new settings
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
||||||
@ -177,13 +200,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
mThreadRun = true;
|
mThreadRun = true;
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (mThreadRun) {
|
while (mThreadRun) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
|
if (!mThreadRun)
|
||||||
|
break;
|
||||||
bmp = processFrame(mFrame);
|
bmp = processFrame(mFrame);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -198,5 +223,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class Sample2NativeCamera extends Activity {
|
public class Sample2NativeCamera extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
public static final int VIEW_MODE_GRAY = 1;
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
@ -24,11 +24,10 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
private MenuItem mItemPreviewRGBA;
|
private MenuItem mItemPreviewRGBA;
|
||||||
private MenuItem mItemPreviewGray;
|
private MenuItem mItemPreviewGray;
|
||||||
private MenuItem mItemPreviewCanny;
|
private MenuItem mItemPreviewCanny;
|
||||||
|
private Sample2View mView;
|
||||||
|
|
||||||
public static int viewMode = VIEW_MODE_RGBA;
|
public static int viewMode = VIEW_MODE_RGBA;
|
||||||
|
|
||||||
private Sample2View mView;
|
|
||||||
|
|
||||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||||
@Override
|
@Override
|
||||||
public void onManagerConnected(int status) {
|
public void onManagerConnected(int status) {
|
||||||
@ -39,6 +38,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
// Create and set View
|
// Create and set View
|
||||||
mView = new Sample2View(mAppContext);
|
mView = new Sample2View(mAppContext);
|
||||||
setContentView(mView);
|
setContentView(mView);
|
||||||
|
|
||||||
// Check native OpenCV camera
|
// Check native OpenCV camera
|
||||||
if( !mView.openCamera() ) {
|
if( !mView.openCamera() ) {
|
||||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||||
@ -53,6 +53,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -82,7 +83,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -90,12 +91,11 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,7 +103,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
@ -111,7 +111,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
mItemPreviewCanny = menu.add("Canny");
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
@ -120,7 +120,7 @@ public class Sample2NativeCamera extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemPreviewRGBA)
|
if (item == mItemPreviewRGBA)
|
||||||
viewMode = VIEW_MODE_RGBA;
|
viewMode = VIEW_MODE_RGBA;
|
||||||
else if (item == mItemPreviewGray)
|
else if (item == mItemPreviewGray)
|
||||||
|
@ -15,16 +15,20 @@ import android.util.Log;
|
|||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
class Sample2View extends SampleCvViewBase {
|
class Sample2View extends SampleCvViewBase {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mGray;
|
private Mat mGray;
|
||||||
private Mat mIntermediateMat;
|
private Mat mIntermediateMat;
|
||||||
|
|
||||||
public Sample2View(Context context) {
|
public Sample2View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "called surfaceCreated");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mGray = new Mat();
|
mGray = new Mat();
|
||||||
@ -59,7 +63,7 @@ class Sample2View extends SampleCvViewBase {
|
|||||||
Utils.matToBitmap(mRgba, bmp);
|
Utils.matToBitmap(mRgba, bmp);
|
||||||
return bmp;
|
return bmp;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Log.e("org.opencv.samples.tutorial2", "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private VideoCapture mCamera;
|
private VideoCapture mCamera;
|
||||||
@ -26,34 +26,28 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public synchronized boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
synchronized (this) {
|
|
||||||
releaseCamera();
|
|
||||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||||
if (!mCamera.isOpened()) {
|
if (!mCamera.isOpened()) {
|
||||||
releaseCamera();
|
releaseCamera();
|
||||||
Log.e(TAG, "Failed to open native camera");
|
Log.e(TAG, "Can't open native camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public synchronized void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null && mCamera.isOpened()) {
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
int mFrameWidth = width;
|
int mFrameWidth = width;
|
||||||
int mFrameHeight = height;
|
int mFrameHeight = height;
|
||||||
@ -75,27 +69,24 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (true) {
|
while (true) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
@ -121,7 +112,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
|||||||
bmp.recycle();
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
Log.i(TAG, "Finishing processing thread");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,7 +13,8 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class Sample3Native extends Activity {
|
public class Sample3Native extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private Sample3View mView;
|
private Sample3View mView;
|
||||||
|
|
||||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||||
@ -73,7 +74,7 @@ public class Sample3Native extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -81,12 +82,11 @@ public class Sample3Native extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ public class Sample3Native extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
Log.i(TAG, "onCreate");
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
|
@ -2,8 +2,10 @@ package org.opencv.samples.tutorial3;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
class Sample3View extends SampleViewBase {
|
class Sample3View extends SampleViewBase {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
private int mFrameSize;
|
private int mFrameSize;
|
||||||
private Bitmap mBitmap;
|
private Bitmap mBitmap;
|
||||||
@ -11,13 +13,16 @@ class Sample3View extends SampleViewBase {
|
|||||||
|
|
||||||
public Sample3View(Context context) {
|
public Sample3View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||||
mFrameSize = previewWidtd * previewHeight;
|
Log.i(TAG, "called onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||||
|
|
||||||
|
mFrameSize = previewWidth * previewHeight;
|
||||||
mRGBA = new int[mFrameSize];
|
mRGBA = new int[mFrameSize];
|
||||||
mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
|
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -15,14 +15,14 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private int mFrameWidth;
|
private int mFrameWidth;
|
||||||
private int mFrameHeight;
|
private int mFrameHeight;
|
||||||
private byte[] mFrame;
|
private byte[] mFrame;
|
||||||
private boolean mThreadRun;
|
private volatile boolean mThreadRun;
|
||||||
private byte[] mBuffer;
|
private byte[] mBuffer;
|
||||||
private SurfaceTexture mSf;
|
private SurfaceTexture mSf;
|
||||||
|
|
||||||
@ -52,11 +52,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
releaseCamera();
|
mCamera = null;
|
||||||
|
|
||||||
|
try {
|
||||||
mCamera = Camera.open();
|
mCamera = Camera.open();
|
||||||
|
}
|
||||||
|
catch (Exception e){
|
||||||
|
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||||
|
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
||||||
|
try {
|
||||||
|
mCamera = Camera.open(camIdx);
|
||||||
|
}
|
||||||
|
catch (RuntimeException e) {
|
||||||
|
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(mCamera == null) {
|
if(mCamera == null) {
|
||||||
Log.e(TAG, "Can't open camera!");
|
Log.e(TAG, "Can't open any camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,11 +87,12 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
camera.addCallbackBuffer(mBuffer);
|
camera.addCallbackBuffer(mBuffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
@ -86,10 +105,9 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
onPreviewStopped();
|
onPreviewStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
mFrameWidth = width;
|
mFrameWidth = width;
|
||||||
@ -139,24 +157,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
|
// stop preview before making changes
|
||||||
|
try {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
} catch (Exception e){
|
||||||
|
// ignore: tried to stop a non-existent preview
|
||||||
|
}
|
||||||
|
|
||||||
|
// start preview with new settings
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
||||||
protected abstract Bitmap processFrame(byte[] data);
|
protected abstract Bitmap processFrame(byte[] data);
|
||||||
|
|
||||||
@ -177,13 +200,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
mThreadRun = true;
|
mThreadRun = true;
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (mThreadRun) {
|
while (mThreadRun) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
|
if (!mThreadRun)
|
||||||
|
break;
|
||||||
bmp = processFrame(mFrame);
|
bmp = processFrame(mFrame);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -198,5 +223,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
public class Sample4Mixed extends Activity {
|
public class Sample4Mixed extends Activity {
|
||||||
private static final String TAG = "Sample::Activity";
|
private static final String TAG = "OCVSample::Activity";
|
||||||
|
|
||||||
private MenuItem mItemPreviewRGBA;
|
private MenuItem mItemPreviewRGBA;
|
||||||
private MenuItem mItemPreviewGray;
|
private MenuItem mItemPreviewGray;
|
||||||
@ -52,6 +52,7 @@ public class Sample4Mixed extends Activity {
|
|||||||
ad.show();
|
ad.show();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/** OpenCV loader cannot start Google Play **/
|
/** OpenCV loader cannot start Google Play **/
|
||||||
case LoaderCallbackInterface.MARKET_ERROR:
|
case LoaderCallbackInterface.MARKET_ERROR:
|
||||||
{
|
{
|
||||||
@ -81,7 +82,7 @@ public class Sample4Mixed extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
Log.i(TAG, "onPause");
|
Log.i(TAG, "called onPause");
|
||||||
if (null != mView)
|
if (null != mView)
|
||||||
mView.releaseCamera();
|
mView.releaseCamera();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@ -89,12 +90,11 @@ public class Sample4Mixed extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
Log.i(TAG, "called onResume");
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Log.i(TAG, "Trying to load OpenCV library");
|
Log.i(TAG, "Trying to load OpenCV library");
|
||||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,14 +102,15 @@ public class Sample4Mixed extends Activity {
|
|||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
Log.i(TAG, "called onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
Log.i(TAG, "onCreate");
|
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "called onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
mItemPreviewCanny = menu.add("Canny");
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
@ -117,8 +118,9 @@ public class Sample4Mixed extends Activity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||||
if (item == mItemPreviewRGBA) {
|
if (item == mItemPreviewRGBA) {
|
||||||
mView.setViewMode(Sample4View.VIEW_MODE_RGBA);
|
mView.setViewMode(Sample4View.VIEW_MODE_RGBA);
|
||||||
} else if (item == mItemPreviewGray) {
|
} else if (item == mItemPreviewGray) {
|
||||||
|
@ -10,6 +10,7 @@ import android.graphics.Bitmap;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
class Sample4View extends SampleViewBase {
|
class Sample4View extends SampleViewBase {
|
||||||
|
private static final String TAG = "OCVSample::View";
|
||||||
|
|
||||||
public static final int VIEW_MODE_RGBA = 0;
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
public static final int VIEW_MODE_GRAY = 1;
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
@ -20,16 +21,17 @@ class Sample4View extends SampleViewBase {
|
|||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private Mat mGraySubmat;
|
private Mat mGraySubmat;
|
||||||
private Mat mIntermediateMat;
|
private Mat mIntermediateMat;
|
||||||
|
|
||||||
private int mViewMode;
|
|
||||||
private Bitmap mBitmap;
|
private Bitmap mBitmap;
|
||||||
|
private int mViewMode;
|
||||||
|
|
||||||
public Sample4View(Context context) {
|
public Sample4View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStarted(int previewWidtd, int previewHeight) {
|
protected void onPreviewStarted(int previewWidth, int previewHeight) {
|
||||||
|
Log.i(TAG, "called onPreviewStarted("+previewWidth+", "+previewHeight+")");
|
||||||
|
|
||||||
// initialize Mats before usage
|
// initialize Mats before usage
|
||||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||||
@ -37,17 +39,19 @@ class Sample4View extends SampleViewBase {
|
|||||||
mRgba = new Mat();
|
mRgba = new Mat();
|
||||||
mIntermediateMat = new Mat();
|
mIntermediateMat = new Mat();
|
||||||
|
|
||||||
mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);
|
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreviewStopped() {
|
protected void onPreviewStopped() {
|
||||||
|
Log.i(TAG, "called onPreviewStopped");
|
||||||
|
|
||||||
if (mBitmap != null) {
|
if (mBitmap != null) {
|
||||||
mBitmap.recycle();
|
mBitmap.recycle();
|
||||||
mBitmap = null;
|
mBitmap = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
// Explicitly deallocate Mats
|
// Explicitly deallocate Mats
|
||||||
if (mYuv != null)
|
if (mYuv != null)
|
||||||
mYuv.release();
|
mYuv.release();
|
||||||
@ -62,6 +66,7 @@ class Sample4View extends SampleViewBase {
|
|||||||
mRgba = null;
|
mRgba = null;
|
||||||
mGraySubmat = null;
|
mGraySubmat = null;
|
||||||
mIntermediateMat = null;
|
mIntermediateMat = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,6 +110,7 @@ class Sample4View extends SampleViewBase {
|
|||||||
public native void FindFeatures(long matAddrGr, long matAddrRgba);
|
public native void FindFeatures(long matAddrGr, long matAddrRgba);
|
||||||
|
|
||||||
public void setViewMode(int viewMode) {
|
public void setViewMode(int viewMode) {
|
||||||
|
Log.i(TAG, "called setViewMode("+viewMode+")");
|
||||||
mViewMode = viewMode;
|
mViewMode = viewMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package org.opencv.samples.tutorial4;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
@ -15,14 +16,14 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample::SurfaceView";
|
private static final String TAG = "OCVSample::BaseView";
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
private int mFrameWidth;
|
private int mFrameWidth;
|
||||||
private int mFrameHeight;
|
private int mFrameHeight;
|
||||||
private byte[] mFrame;
|
private byte[] mFrame;
|
||||||
private boolean mThreadRun;
|
private volatile boolean mThreadRun;
|
||||||
private byte[] mBuffer;
|
private byte[] mBuffer;
|
||||||
private SurfaceTexture mSf;
|
private SurfaceTexture mSf;
|
||||||
|
|
||||||
@ -52,11 +53,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean openCamera() {
|
public boolean openCamera() {
|
||||||
Log.i(TAG, "openCamera");
|
Log.i(TAG, "Opening Camera");
|
||||||
releaseCamera();
|
mCamera = null;
|
||||||
|
|
||||||
|
try {
|
||||||
mCamera = Camera.open();
|
mCamera = Camera.open();
|
||||||
|
}
|
||||||
|
catch (Exception e){
|
||||||
|
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||||
|
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
||||||
|
try {
|
||||||
|
mCamera = Camera.open(camIdx);
|
||||||
|
}
|
||||||
|
catch (RuntimeException e) {
|
||||||
|
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(mCamera == null) {
|
if(mCamera == null) {
|
||||||
Log.e(TAG, "Can't open camera!");
|
Log.e(TAG, "Can't open any camera");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,16 +88,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
camera.addCallbackBuffer(mBuffer);
|
camera.addCallbackBuffer(mBuffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
Log.i(TAG, "releaseCamera");
|
Log.i(TAG, "Releasing Camera");
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
mCamera.setPreviewCallback(null);
|
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
mCamera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
@ -86,10 +105,9 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
onPreviewStopped();
|
onPreviewStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupCamera(int width, int height) {
|
public synchronized void setupCamera(int width, int height) {
|
||||||
Log.i(TAG, "setupCamera");
|
|
||||||
synchronized (this) {
|
|
||||||
if (mCamera != null) {
|
if (mCamera != null) {
|
||||||
|
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
mFrameWidth = width;
|
mFrameWidth = width;
|
||||||
@ -139,25 +157,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
Log.i(TAG, "surfaceChanged");
|
Log.i(TAG, "called surfaceChanged");
|
||||||
|
// stop preview before making changes
|
||||||
|
try {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
} catch (Exception e){
|
||||||
|
// ignore: tried to stop a non-existent preview
|
||||||
|
}
|
||||||
|
|
||||||
|
// start preview with new settings
|
||||||
setupCamera(width, height);
|
setupCamera(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceCreated");
|
Log.i(TAG, "called surfaceCreated");
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "surfaceDestroyed");
|
Log.i(TAG, "called surfaceDestroyed");
|
||||||
releaseCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
|
||||||
protected abstract Bitmap processFrame(byte[] data);
|
protected abstract Bitmap processFrame(byte[] data);
|
||||||
|
|
||||||
@ -178,13 +200,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
mThreadRun = true;
|
mThreadRun = true;
|
||||||
Log.i(TAG, "Starting processing thread");
|
Log.i(TAG, "Started processing thread");
|
||||||
while (mThreadRun) {
|
while (mThreadRun) {
|
||||||
Bitmap bmp = null;
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
|
if (!mThreadRun)
|
||||||
|
break;
|
||||||
bmp = processFrame(mFrame);
|
bmp = processFrame(mFrame);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -199,5 +223,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "Finished processing thread");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user