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.fing.chesstrack.modulomodelador.Modelador;
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.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
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;
private MenuItem mItemCalibrar;
//TODO es chancho pero ver luego
private Mat frame;
private boolean isUsed;
private boolean isCalibrada ;
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
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);
Modelador.getInstance();
try {
isUsed = false;
isCalibrada =false;
} 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
*/
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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "called onCreateOptionsMenu");
mItemCalibrar = menu.add("Show/hide tile numbers");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "Menu Item selected " + item);
if (item == mItemCalibrar) {
isUsed = true;
if(frame!=null){
if (Calibracion.getInstance().calibrar(frame)){
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();
isCalibrada = true;
}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;
}
return true;
@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");
inputFrame = adq.processFrame(inputFrame);
//Modelador.getInstance().dividirTablero(inputFrame);
//return Modelador.getInstance().dibujarEscaque(2, 3);
return inputFrame;
}
return inputFrame;