Newer
Older
package uy.edu.fing.chesstrack;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import uy.edu.chesstrack.communication.Client;
import uy.edu.fing.chesstrack.modulovision.Adquisicion;
import uy.edu.fing.chesstrack.modulovision.Calibracion;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ChessTrackActivity extends Activity implements CvCameraViewListener {
private static final String TAG = "CHESSTRACK::Activity";
private static final String IPV4_REGEX ="^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
private static final String PORT_REGEX ="^(\\d{4,5})$";
private static final String welcomeMSG = ""
+ "--------------------------------------------------------------------------\n"
+ " .:: CHESSTRACK ::. seguimiento de una partida de Ajedrez\n"
+ "\n"
+ " Aylen Ricca - Nicolas Furquez\n"
+ "--------------------------------------------------------------------------\n";
private CameraBridgeViewBase mOpenCvCameraView;
private Adquisicion adq;
private Calibracion calibrar;
private Button btnCalibrar;
private Client ClientCommunication;
//TODO es chancho pero ver luego
private Mat frame;
private boolean isUsed;
private boolean isCalibrada ;
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
private final 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 onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
}
@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.chess_track_layout);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.chess_track_layout);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
try {
isUsed = false;
isCalibrada =false;
addListenerOnButton();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getServerIp();
public void getServerIp(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Communication Setup");
alert.setMessage("Set server ip");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
if (value == null || value.toString().equals("") || !value.toString().matches(IPV4_REGEX)) {
Log.i(TAG,"INPUT= not valid IP" + value.toString());
getServerIp();
} else {
Log.i(TAG,"INPUT=" + value.toString());
Log.i(TAG,"IP=" + value.toString());
getServerPort(value.toString());
}
/*
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// no server communication -- FIXME do not send info to client
*/
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public void getServerPort(final String ip){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Communication Setup");
alert.setMessage("Set server port");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
Log.i(TAG,"INPUT=" + value.toString());
if (value == null || value.toString().equals("") || !value.toString().matches(PORT_REGEX)) {
Log.i(TAG,"INPUT= not valid PORT" + value.toString());
getServerPort(ip);
} else {
Log.i(TAG,"INPUT=" + value.toString());
int port = Integer.parseInt(value.toString());
Log.i(TAG,"PORT=" + port);
ClientCommunication = Client.getInstance();
ClientCommunication.EstablishConnection(ip, port);
ClientCommunication.SendData(welcomeMSG);
}
}
});
alert.show();
}
public void addListenerOnButton() {
btnCalibrar = (Button) findViewById(R.id.btn_calibrar);
btnCalibrar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
isUsed = true;
if(frame!=null){
if (Calibracion.getInstance().calibrar(frame)){
isCalibrada = true;
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms
try {
adq = new Adquisicion();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 700); // 200 is duration in ms
}
}
isUsed = false;
}
});
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
if (this.ClientCommunication != null) {
this.ClientCommunication.Stop();
}
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(Mat inputFrame) {
// TODO MANEJAR ESTADOS
if (!isUsed){
frame = inputFrame;
}
if(isCalibrada){
Log.i(TAG, "calibrada");
return adq.processFrame(inputFrame);
}
return inputFrame;