Advertisement
Guest User

Untitled

a guest
May 25th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.23 KB | None | 0 0
  1. package com.frontend.frontend;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.pm.PackageManager;
  10. import android.hardware.Sensor;
  11. import android.hardware.SensorEvent;
  12. import android.hardware.SensorEventListener;
  13. import android.hardware.SensorManager;
  14. import android.media.Image;
  15. import android.os.Handler;
  16. import android.os.Looper;
  17. import android.support.annotation.NonNull;
  18. import android.support.v4.app.ActivityCompat;
  19. import android.support.v7.app.AppCompatActivity;
  20. import android.os.Bundle;
  21. import android.text.InputType;
  22. import android.util.SparseArray;
  23. import android.view.Gravity;
  24. import android.view.SurfaceHolder;
  25. import android.view.SurfaceView;
  26. import android.view.View;
  27. import android.view.animation.Animation;
  28. import android.view.animation.RotateAnimation;
  29. import android.widget.Button;
  30. import android.widget.EditText;
  31. import android.widget.ImageView;
  32. import android.widget.TextView;
  33. import android.widget.Toast;
  34.  
  35. import java.sql.SQLException;
  36. import java.time.Clock;
  37. import java.util.ArrayList;
  38.  
  39. import com.frontend.backend.ARGuide.main.JSONResourceException;
  40. import com.frontend.backend.ARGuide.main.MyApplication;
  41. import com.frontend.frontend.Main.MainActivity;
  42. import com.frontend.frontend.Timetable.TimetableScreen;
  43. import com.google.android.gms.vision.CameraSource;
  44. import com.google.android.gms.vision.Detector;
  45. import com.google.android.gms.vision.text.TextBlock;
  46. import com.google.android.gms.vision.text.TextRecognizer;
  47.  
  48. import java.io.IOException;
  49. import java.util.List;
  50.  
  51. import com.frontend.backend.ARGuide.main.ARGuide;
  52.  
  53. import static java.lang.System.exit;
  54.  
  55. /**
  56. * Tools for the pattern recognition and the camera functionality.
  57. */
  58. public class SelectDestination extends AppCompatActivity implements SensorEventListener {
  59.  
  60. private Button getRoomBtn;
  61. private Button returnButton;
  62. private CameraSource cameraSource;
  63. private TextRecognizer textRecognizer;
  64. private SurfaceView cameraView;
  65. private TextView ocrTextView;
  66. private ImageView arrowImg;
  67. private Arrow arrow;
  68.  
  69. private String room = null;
  70. private String finalText;
  71. private String destRoom = null;
  72.  
  73. private float[] mGravity = new float[3];
  74. private float[] mGeomagnetic = new float[3];
  75. private float azimuth = 0f;
  76. private SensorManager mSensorManager;
  77. private SensorManager stepSensorManager;
  78.  
  79. private List<String> roomsList = new ArrayList<>();
  80.  
  81. private boolean firstRun = true;
  82. private float firstAzimuth = 0f;
  83. private int counterSteps = 0;
  84.  
  85. private String target = "N";
  86. private boolean walking;
  87.  
  88.  
  89. @Override
  90. protected void onCreate(Bundle savedInstanceState) {
  91. super.onCreate(savedInstanceState);
  92. MyApplication.path = this.getFilesDir().getAbsolutePath();
  93. setContentView(R.layout.destination_layout);
  94.  
  95. getRoomBtn = findViewById(R.id.getRoomBtn);
  96. getRoomBtn.setVisibility(View.INVISIBLE);
  97. returnButton = findViewById(R.id.returnButton);
  98. cameraView = findViewById(R.id.cameraViewSurface);
  99. ocrTextView = findViewById(R.id.ocrTextView);
  100.  
  101. arrowImg = findViewById(R.id.imageViewCompass);
  102. arrowImg.setVisibility(View.VISIBLE);
  103. arrow = new Arrow();
  104. mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
  105. stepSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  106.  
  107.  
  108. try {
  109.  
  110. Bundle extras = getIntent().getExtras();
  111. if (extras != null) {
  112. room = extras.getString("current_room");
  113. }
  114. ARGuide databaseConn = new ARGuide("faculty_uaic_cs",
  115. MyApplication.path+"/faculty.db",
  116. MyApplication.path+"/facultySchedule.json",
  117. MyApplication.path+"/buildingPlan.json");
  118.  
  119. roomsList = databaseConn.selectAllClassroomNames();
  120.  
  121. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  122. builder.setTitle("What room");
  123. final EditText input = new EditText(this);
  124. input.setInputType(InputType.TYPE_CLASS_TEXT);
  125. builder.setView(input);
  126. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  127. @Override
  128. public void onClick(DialogInterface dialog, int which) {
  129. destRoom = input.getText().toString();
  130. arrowImg.setVisibility(View.VISIBLE);
  131. }
  132. });
  133. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  134. @Override
  135. public void onClick(DialogInterface dialog, int which) {
  136. dialog.cancel();
  137. finish();
  138. }
  139. });
  140. builder.show();
  141.  
  142. final Toast warning = Toast.makeText(getApplicationContext(), "Room not found.", Toast.LENGTH_SHORT);
  143. warning.setGravity(Gravity.CENTER, 0, 0);
  144.  
  145. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  146. startTextRecognizer();
  147. }
  148.  
  149. returnButton.setOnClickListener(new View.OnClickListener() {
  150. @Override
  151. public void onClick(View v) {
  152. openMenu();
  153. }
  154. });
  155. getRoomBtn.setOnClickListener(new View.OnClickListener() {
  156. @Override
  157. public void onClick(View v) {
  158. if (roomsList.contains(finalText.trim()) ){
  159. getRoomBtn.setVisibility(View.INVISIBLE);
  160. room = finalText;
  161. } else {
  162. warning.show();
  163. }
  164. }
  165. });
  166.  
  167. }catch (SQLException e) {
  168. e.printStackTrace();
  169. } catch (JSONResourceException e) {
  170. e.printStackTrace();
  171. }
  172. }
  173.  
  174. public void openMenu() {
  175. finish();
  176. }
  177.  
  178.  
  179. @Override
  180. protected void onDestroy() {
  181. super.onDestroy();
  182. cameraSource.release();
  183. }
  184.  
  185. /**
  186. * Initializes text recognition
  187. */
  188. private void startTextRecognizer() {
  189. textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
  190. if (!textRecognizer.isOperational()) {
  191. Toast.makeText(getApplicationContext(), "Oops ! Not able to start the text recognizer ...", Toast.LENGTH_LONG).show(); exit(0);
  192. }
  193. cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
  194. .setFacing(CameraSource.CAMERA_FACING_BACK)
  195. .setRequestedPreviewSize(1280, 1024)
  196. .setRequestedFps(15.0f)
  197. .setAutoFocusEnabled(true)
  198. .build();
  199.  
  200. cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
  201. @Override
  202. public void surfaceCreated(SurfaceHolder holder) {
  203. if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  204. try {
  205. cameraSource.start(cameraView.getHolder());
  206. } catch (IOException e) {
  207. e.printStackTrace();
  208. }
  209. }
  210. }
  211.  
  212. @Override
  213. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  214.  
  215. }
  216.  
  217. @Override
  218. public void surfaceDestroyed(SurfaceHolder holder) {
  219. cameraSource.stop();
  220. }
  221. });
  222.  
  223. textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
  224. @Override
  225. public void release() {
  226.  
  227. }
  228.  
  229. @Override
  230. public void receiveDetections(Detector.Detections<TextBlock> detections) {
  231. SparseArray<TextBlock> items = detections.getDetectedItems();
  232. StringBuilder stringBuilder = new StringBuilder();
  233. for (int i = 0; i < items.size(); ++i) {
  234. TextBlock item = items.valueAt(i);
  235. if (item != null && item.getValue() != null) {
  236. stringBuilder.append(item.getValue() + " ");
  237. }
  238. }
  239.  
  240. final String fullText = stringBuilder.toString();
  241. Handler handler = new Handler(Looper.getMainLooper());
  242. handler.post(new Runnable() {
  243. public void run() {
  244. ocrTextView.setText(fullText);
  245. if (roomsList.contains(fullText.trim())) {
  246. finalText = fullText;
  247. getRoomBtn.setVisibility(View.VISIBLE);
  248. //startTextRecognizer();
  249. }
  250. }
  251. });
  252. }
  253. });
  254. }
  255.  
  256. public int getAngle(String target){
  257. switch(target) {
  258. case "N":
  259. return 0;
  260. case "S":
  261. return 180;
  262. case "W":
  263. return 90;
  264. case "E":
  265. return -90;
  266. }
  267.  
  268. return 0;
  269. }
  270.  
  271. @Override
  272. protected void onResume() {
  273. super.onResume();
  274. mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
  275. SensorManager.SENSOR_DELAY_FASTEST);
  276. mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  277. SensorManager.SENSOR_DELAY_FASTEST);
  278. mSensorManager.registerListener(this, mSensorManager.getDefaultSensor((Sensor.TYPE_STEP_DETECTOR)),
  279. SensorManager.SENSOR_DELAY_FASTEST);
  280.  
  281. walking = true;
  282. Sensor countSensor = stepSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
  283. if(countSensor != null){
  284. stepSensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
  285. } else {
  286. Toast.makeText(this, "Sensor not found", Toast.LENGTH_SHORT);
  287. }
  288. }
  289.  
  290. @Override
  291. protected void onPause() {
  292. super.onPause();
  293. mSensorManager.unregisterListener(this);
  294. walking = false;
  295. stepSensorManager.unregisterListener(this);
  296. }
  297. @Override
  298. public void onSensorChanged(SensorEvent sensorEvent) {
  299. final float alpha= 0.97f;
  300. synchronized (this){
  301. if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
  302. {
  303. mGravity[0] = alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
  304. mGravity[1] = alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
  305. mGravity[2] = alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
  306. }
  307. if(sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
  308. {
  309. mGeomagnetic[0] = alpha*mGeomagnetic[0]+(1-alpha)*sensorEvent.values[0];
  310. mGeomagnetic[1] = alpha*mGeomagnetic[1]+(1-alpha)*sensorEvent.values[1];
  311. mGeomagnetic[2] = alpha*mGeomagnetic[2]+(1-alpha)*sensorEvent.values[2];
  312. }
  313. float R[] = new float[9];
  314. float I[] = new float[9];
  315. boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
  316.  
  317. if(success)
  318. {
  319. float orientation[] = new float[3];
  320. SensorManager.getOrientation(R,orientation);
  321. azimuth = (float)Math.toDegrees(orientation[0]);
  322.  
  323. if(counterSteps > 5){
  324. counterSteps = 0;
  325. firstRun = true;
  326. target="S";
  327. }
  328.  
  329. if(firstRun == true){
  330. firstAzimuth = azimuth;
  331. firstRun = false;
  332. }
  333. azimuth = (azimuth+360 - firstAzimuth + getAngle(target))%360;
  334.  
  335. //System.out.println(azimuth);
  336.  
  337.  
  338. if ((azimuth >315 && azimuth <= 360) || (azimuth >= 0 && azimuth <= 45) ) {
  339. arrow.setOrientation("N");
  340. }
  341. if (azimuth >45 && azimuth <= 135 ) {
  342. arrow.setOrientation("W");
  343. }
  344. if (azimuth >135 && azimuth <=225) {
  345. arrow.setOrientation("S");
  346. }
  347. if (azimuth >225 && azimuth <=315) {
  348. arrow.setOrientation("E");
  349. }
  350.  
  351. System.out.println(counterSteps);
  352. arrowImg.setRotation(arrow.update());
  353. }
  354.  
  355. if(sensorEvent.sensor.getType() == Sensor.TYPE_STEP_COUNTER && arrow.getOrientation() == "N"){
  356. if(walking){
  357. counterSteps = (int) sensorEvent.values[0];
  358. System.out.println("Pasi: " + counterSteps);
  359. }
  360. }
  361. }
  362. }
  363. @Override
  364. public void onAccuracyChanged(Sensor sensor, int i){ }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement