package com sivsais oktyper import org opencv android BaseLoaderCallba

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.sivsais.oktyper;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class ProtoActivity2 extends Activity implements CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private final double SCALE = 1.5;
private Mat mRgba;
private Mat thumb;
private JavaViewWorker mWorker;
private CameraBridgeViewBase mOpenCvCameraView;
private MenuItem debugMode;
private MenuItem handToggle;
private MenuItem flashToggle;
private MenuItem actionToggle;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.java_surface_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.i(TAG, "SINGLETAP");
mWorker.onSingleTap(motionEvent);
return true;
}
});
mWorker = new JavaViewWorker(this, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
debugMode = menu.add("Toggle debug mode");
handToggle = menu.add("Left hand");
flashToggle = menu.add("Toggle flash");
actionToggle = menu.add("Toggle action");
return true;
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.getHolder().removeCallback(mOpenCvCameraView);
mOpenCvCameraView.disableView();
}
}
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
}
public void onCameraViewStopped() {
mRgba.release();
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
if (thumb == null) {
thumb = new Mat();
}
Imgproc.resize(mRgba, thumb, new Size(mRgba.cols() / SCALE, mRgba.rows() / SCALE));
mWorker.process(thumb);
Imgproc.resize(thumb, mRgba, mRgba.size());
return mRgba;
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item == debugMode) {
mWorker.toggleDebugMode();
}
if (item == handToggle) {
int hand = mWorker.toggleHand();
if (hand == Recognizer.HAND_LEFT) {
item.setTitle("Right hand");
} else if (hand == Recognizer.HAND_RIGHT) {
item.setTitle("Left hand");
}
}
if (item == flashToggle) {
((JavaCameraView) mOpenCvCameraView).flashToggle();
}
if (item == actionToggle) {
item.setTitle(mWorker.toggleAction());
}
return true;
}
}