Advertisement
Guest User

Untitled

a guest
May 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.55 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.  
  78.     private List<String> roomsList = new ArrayList<>();
  79.  
  80.     private boolean firstRun = true;
  81.     private float firstAzimuth = 0f;
  82.     private int counterSteps = 0;
  83.  
  84.     private String target = "N";
  85.  
  86.  
  87.     @Override
  88.     protected void onCreate(Bundle savedInstanceState) {
  89.         super.onCreate(savedInstanceState);
  90.         MyApplication.path = this.getFilesDir().getAbsolutePath();
  91.         setContentView(R.layout.destination_layout);
  92.  
  93.         getRoomBtn = findViewById(R.id.getRoomBtn);
  94.         getRoomBtn.setVisibility(View.INVISIBLE);
  95.         returnButton = findViewById(R.id.returnButton);
  96.         cameraView = findViewById(R.id.cameraViewSurface);
  97.         ocrTextView = findViewById(R.id.ocrTextView);
  98.  
  99.         arrowImg = findViewById(R.id.imageViewCompass);
  100.         arrowImg.setVisibility(View.VISIBLE);
  101.         arrow = new Arrow();
  102.         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
  103.  
  104.  
  105.         try {
  106.  
  107.             Bundle extras = getIntent().getExtras();
  108.             if (extras != null) {
  109.                 room = extras.getString("current_room");
  110.             }
  111.             ARGuide databaseConn = new ARGuide("faculty_uaic_cs",
  112.                     MyApplication.path+"/faculty.db",
  113.                     MyApplication.path+"/facultySchedule.json",
  114.                     MyApplication.path+"/buildingPlan.json");
  115.  
  116.             roomsList = databaseConn.selectAllClassroomNames();
  117.  
  118.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  119.             builder.setTitle("What room");
  120.             final EditText input = new EditText(this);
  121.             input.setInputType(InputType.TYPE_CLASS_TEXT);
  122.             builder.setView(input);
  123.             builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  124.                 @Override
  125.                 public void onClick(DialogInterface dialog, int which) {
  126.                     destRoom = input.getText().toString();
  127.                     arrowImg.setVisibility(View.VISIBLE);
  128.                 }
  129.             });
  130.             builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  131.                 @Override
  132.                 public void onClick(DialogInterface dialog, int which) {
  133.                     dialog.cancel();
  134.                     finish();
  135.                 }
  136.             });
  137.             builder.show();
  138.  
  139.             final Toast warning = Toast.makeText(getApplicationContext(), "Room not found.", Toast.LENGTH_SHORT);
  140.             warning.setGravity(Gravity.CENTER, 0, 0);
  141.  
  142.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  143.                 startTextRecognizer();
  144.             }
  145.  
  146.             returnButton.setOnClickListener(new View.OnClickListener() {
  147.                 @Override
  148.                 public void onClick(View v) {
  149.                     openMenu();
  150.                 }
  151.             });
  152.             getRoomBtn.setOnClickListener(new View.OnClickListener() {
  153.                 @Override
  154.                 public void onClick(View v) {
  155.                     if (roomsList.contains(finalText.trim()) ){
  156.                         getRoomBtn.setVisibility(View.INVISIBLE);
  157.                         room = finalText;
  158.                     } else {
  159.                         warning.show();
  160.                     }
  161.                 }
  162.             });
  163.  
  164.         }catch (SQLException e) {
  165.             e.printStackTrace();
  166.         } catch (JSONResourceException e) {
  167.             e.printStackTrace();
  168.         }
  169.     }
  170.  
  171.     public void openMenu() {
  172.         finish();
  173.     }
  174.  
  175.  
  176.     @Override
  177.     protected void onDestroy() {
  178.         super.onDestroy();
  179.         cameraSource.release();
  180.     }
  181.  
  182.     /**
  183.      * Initializes text recognition
  184.      */
  185.     private void startTextRecognizer() {
  186.         textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
  187.         if (!textRecognizer.isOperational()) {
  188.             Toast.makeText(getApplicationContext(), "Oops ! Not able to start the text recognizer ...", Toast.LENGTH_LONG).show(); exit(0);
  189.         }
  190.         cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
  191.                 .setFacing(CameraSource.CAMERA_FACING_BACK)
  192.                 .setRequestedPreviewSize(1280, 1024)
  193.                 .setRequestedFps(15.0f)
  194.                 .setAutoFocusEnabled(true)
  195.                 .build();
  196.  
  197.         cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
  198.             @Override
  199.             public void surfaceCreated(SurfaceHolder holder) {
  200.                 if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  201.                     try {
  202.                         cameraSource.start(cameraView.getHolder());
  203.                     } catch (IOException e) {
  204.                         e.printStackTrace();
  205.                     }
  206.                 }
  207.             }
  208.  
  209.             @Override
  210.             public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  211.  
  212.             }
  213.  
  214.             @Override
  215.             public void surfaceDestroyed(SurfaceHolder holder) {
  216.                 cameraSource.stop();
  217.             }
  218.         });
  219.  
  220.         textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
  221.             @Override
  222.             public void release() {
  223.  
  224.             }
  225.  
  226.             @Override
  227.             public void receiveDetections(Detector.Detections<TextBlock> detections) {
  228.                 SparseArray<TextBlock> items = detections.getDetectedItems();
  229.                 StringBuilder stringBuilder = new StringBuilder();
  230.                 for (int i = 0; i < items.size(); ++i) {
  231.                     TextBlock item = items.valueAt(i);
  232.                     if (item != null && item.getValue() != null) {
  233.                         stringBuilder.append(item.getValue() + " ");
  234.                     }
  235.                 }
  236.  
  237.                 final String fullText = stringBuilder.toString();
  238.                 Handler handler = new Handler(Looper.getMainLooper());
  239.                 handler.post(new Runnable() {
  240.                     public void run() {
  241.                         ocrTextView.setText(fullText);
  242.                         if (roomsList.contains(fullText.trim())) {
  243.                             finalText = fullText;
  244.                             getRoomBtn.setVisibility(View.VISIBLE);
  245.                             //startTextRecognizer();
  246.                         }
  247.                     }
  248.                 });
  249.             }
  250.         });
  251.     }
  252.  
  253.     public int getAngle(String target){
  254.         switch(target) {
  255.             case "N":
  256.                 return 0;
  257.             case "S":
  258.                 return 180;
  259.             case "W":
  260.                 return 90;
  261.             case "E":
  262.                 return -90;
  263.         }
  264.  
  265.         return 0;
  266.     }
  267.  
  268.     @Override
  269.     protected void onResume() {
  270.         super.onResume();
  271.         mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
  272.                 SensorManager.SENSOR_DELAY_FASTEST);
  273.         mSensorManager.registerListener( this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  274.                 SensorManager.SENSOR_DELAY_FASTEST);
  275.         mSensorManager.registerListener(this, mSensorManager.getDefaultSensor((Sensor.TYPE_STEP_DETECTOR)),
  276.                 SensorManager.SENSOR_DELAY_FASTEST);
  277.     }
  278.  
  279.     @Override
  280.     protected void onPause() {
  281.         super.onPause();
  282.         mSensorManager.unregisterListener(this);
  283.     }
  284.     @Override
  285.     public void onSensorChanged(SensorEvent sensorEvent) {
  286.         final float alpha=  0.97f;
  287.         synchronized (this){
  288.             if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
  289.             {
  290.                 mGravity[0] = alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
  291.                 mGravity[1] = alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
  292.                 mGravity[2] = alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
  293.             }
  294.             if(sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
  295.             {
  296.                 mGeomagnetic[0] = alpha*mGeomagnetic[0]+(1-alpha)*sensorEvent.values[0];
  297.                 mGeomagnetic[1] = alpha*mGeomagnetic[1]+(1-alpha)*sensorEvent.values[1];
  298.                 mGeomagnetic[2] = alpha*mGeomagnetic[2]+(1-alpha)*sensorEvent.values[2];
  299.             }
  300.             float R[] = new float[9];
  301.             float I[] = new float[9];
  302.             boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
  303.  
  304.             if(sensorEvent.sensor.getType() == Sensor.TYPE_STEP_DETECTOR && arrow.getOrientation() == "N")
  305.             {
  306.                 counterSteps = counterSteps + 1;
  307.             }
  308.  
  309.             if(success)
  310.             {
  311.                 float orientation[] = new float[3];
  312.                 SensorManager.getOrientation(R,orientation);
  313.                 azimuth = (float)Math.toDegrees(orientation[0]);
  314.  
  315.                 if(counterSteps > 5){
  316.                     counterSteps = 0;
  317.                     firstRun = true;
  318.                     target="S";
  319.                 }
  320.  
  321.                 if(firstRun == true){
  322.                     firstAzimuth = azimuth;
  323.                     firstRun = false;
  324.                 }
  325.                 azimuth = (azimuth+360 - firstAzimuth + getAngle(target))%360;
  326.  
  327.                 //System.out.println(azimuth);
  328.  
  329.  
  330.                 if  ((azimuth >315 && azimuth <= 360)  || (azimuth >= 0 && azimuth <= 45) ) {
  331.                     arrow.setOrientation("N");
  332.                 }
  333.                 if  (azimuth >45 && azimuth <= 135 ) {
  334.                     arrow.setOrientation("W");
  335.                 }
  336.                 if  (azimuth >135 && azimuth <=225) {
  337.                     arrow.setOrientation("S");
  338.                 }
  339.                 if  (azimuth >225 && azimuth <=315) {
  340.                     arrow.setOrientation("E");
  341.                 }
  342.  
  343.  
  344.  
  345.                 System.out.println(counterSteps);
  346.                 arrowImg.setRotation(arrow.update());
  347.             }
  348.         }
  349.     }
  350.     @Override
  351.     public void onAccuracyChanged(Sensor sensor, int i){ }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement