6
6
import android .hardware .Camera ;
7
7
import android .location .Location ;
8
8
import android .os .Bundle ;
9
- import android .util .Log ;
10
9
import android .view .SurfaceHolder ;
11
10
import android .view .SurfaceView ;
12
11
import android .view .View ;
17
16
import java .io .IOException ;
18
17
import java .util .ArrayList ;
19
18
import java .util .List ;
20
-
19
+ /**
20
+ * Created by krzysztofjackowski on 24/09/15.
21
+ */
21
22
public class CameraViewActivity extends Activity implements
22
- SurfaceHolder .Callback , MyCurrentLocationListener , OnAzimuthChangedListener {
23
+ SurfaceHolder .Callback , OnLocationChangedListener , OnAzimuthChangedListener {
23
24
24
- private Camera camera ;
25
- private SurfaceHolder surfaceHolder ;
26
- private boolean cameraview = false ;
27
- private AugmentedPOI poi ;
25
+ private Camera mCamera ;
26
+ private SurfaceHolder mSurfaceHolder ;
27
+ private boolean isCameraviewOn = false ;
28
+ private AugmentedPOI mPoi ;
28
29
29
- private double azimuthReal = 0 ;
30
- private double azimuthTeoretical = 0 ;
31
- private double AZIMUTH_ACCURACY = 5 ;
32
- private double lat = 0 ;
33
- private double longi = 0 ;
30
+ private double mAzimuthReal = 0 ;
31
+ private double mAzimuthTeoretical = 0 ;
32
+ private static double AZIMUTH_ACCURACY = 5 ;
33
+ private double mMyLatitude = 0 ;
34
+ private double mMyLongitude = 0 ;
34
35
35
36
private MyCurrentAzimuth myCurrentAzimuth ;
36
37
private MyCurrentLocation myCurrentLocation ;
37
38
38
- TextView cameraText ;
39
- ImageView icon ;
39
+ TextView descriptionTextView ;
40
+ ImageView pointerIcon ;
40
41
41
- /** Called when the activity is first created. */
42
42
@ Override
43
43
public void onCreate (Bundle savedInstanceState ) {
44
44
super .onCreate (savedInstanceState );
@@ -51,106 +51,98 @@ public void onCreate(Bundle savedInstanceState) {
51
51
}
52
52
53
53
private void setAugmentedRealityPoint () {
54
- poi = new AugmentedPOI (
55
- "Zamek Ojców" , //poiName
56
- "Opis Zamku Ojców" , //poiDesc
57
- 50.21184861660004 , //poiLat
58
- 19.83051002025604 //poiLongi
54
+ mPoi = new AugmentedPOI (
55
+ "Kościół Marciacki" ,
56
+ "Kościół Marciacki w Krakowie" ,
57
+ 50.06169631 ,
58
+ 19.93919566
59
59
);
60
60
}
61
61
62
62
public double calculateTeoreticalAzimuth () {
63
- Location myLocation = new Location ("MyLocation" );
64
- myLocation .setLatitude (lat );
65
- myLocation .setLongitude (longi );
66
-
67
- Location locationB = new Location ("POI" );
68
- locationB .setLatitude (poi .getPoiLatitude ());
69
- locationB .setLongitude (poi .getPoiLongitude ());
70
-
71
- double dX = locationB .getLatitude () - myLocation .getLatitude ();
72
- double dY = locationB .getLongitude () - myLocation .getLongitude ();
63
+ double dX = mPoi .getPoiLatitude () - mMyLatitude ;
64
+ double dY = mPoi .getPoiLongitude () - mMyLongitude ;
73
65
74
- double fi ;
75
- double tanFi ;
76
- double A = 0 ;
66
+ double phiAngle ;
67
+ double tanPhi ;
68
+ double azimuth = 0 ;
77
69
78
- tanFi = Math .abs (dY / dX );
79
- fi = Math .atan (tanFi );
80
- fi = Math .toDegrees (fi );
70
+ tanPhi = Math .abs (dY / dX );
71
+ phiAngle = Math .atan (tanPhi );
72
+ phiAngle = Math .toDegrees (phiAngle );
81
73
82
74
if (dX > 0 && dY > 0 ) { // I quater
83
- return A = fi ;
75
+ return azimuth = phiAngle ;
84
76
} else if (dX < 0 && dY > 0 ) { // II
85
- return A = 180 - fi ;
77
+ return azimuth = 180 - phiAngle ;
86
78
} else if (dX < 0 && dY < 0 ) { // III
87
- return A = 180 + fi ;
79
+ return azimuth = 180 + phiAngle ;
88
80
} else if (dX > 0 && dY < 0 ) { // IV
89
- return A = 360 - fi ;
81
+ return azimuth = 360 - phiAngle ;
90
82
}
91
83
92
- return fi ;
84
+ return phiAngle ;
93
85
}
94
86
95
- private List <Double > calculateAzimuthAccuracy (double A ) {
96
- double min = A - AZIMUTH_ACCURACY ;
97
- double max = A + AZIMUTH_ACCURACY ;
87
+ private List <Double > calculateAzimuthAccuracy (double azimuth ) {
88
+ double minAngle = azimuth - AZIMUTH_ACCURACY ;
89
+ double maxAngle = azimuth + AZIMUTH_ACCURACY ;
98
90
List <Double > minMax = new ArrayList <Double >();
99
91
100
- if (min < 0 )
101
- min += 360 ;
92
+ if (minAngle < 0 )
93
+ minAngle += 360 ;
94
+
95
+ if (maxAngle >= 360 )
96
+ maxAngle -= 360 ;
102
97
103
- if (max >= 360 )
104
- max -= 360 ;
105
98
minMax .clear ();
106
- minMax .add (min );
107
- minMax .add (max );
99
+ minMax .add (minAngle );
100
+ minMax .add (maxAngle );
101
+
108
102
return minMax ;
109
103
}
110
104
111
- private boolean isBetween (double min , double max , double A ) {
112
- if (min > max ) {
113
- if (isBetween (0 , max , A ) && isBetween (min , 360 , A ))
105
+ private boolean isBetween (double minAngle , double maxAngle , double azimuth ) {
106
+ if (minAngle > maxAngle ) {
107
+ if (isBetween (0 , maxAngle , azimuth ) && isBetween (minAngle , 360 , azimuth ))
114
108
return true ;
115
109
} else {
116
- if (A > min && A < max )
110
+ if (azimuth > minAngle && azimuth < maxAngle )
117
111
return true ;
118
112
}
119
113
return false ;
120
114
}
121
115
122
116
@ Override
123
- public void getCurrentLocation (Location location ) {
124
- lat = location .getLatitude ();
125
- longi = location .getLongitude ();
126
- cameraText .setText (poi .getPoiName () + " azimuthTeoretical "
127
- + azimuthTeoretical + " azimuthReal " + azimuthReal + " lat "
128
- + lat + " lon " + longi );
129
- azimuthTeoretical = calculateTeoreticalAzimuth ();
117
+ public void onLocationChanged (Location location ) {
118
+ mMyLatitude = location .getLatitude ();
119
+ mMyLongitude = location .getLongitude ();
120
+ descriptionTextView .setText (mPoi .getPoiName () + " azimuthTeoretical "
121
+ + mAzimuthTeoretical + " azimuthReal " + mAzimuthReal + " latitude "
122
+ + mMyLatitude + " longitude " + mMyLongitude );
123
+ mAzimuthTeoretical = calculateTeoreticalAzimuth ();
130
124
Toast .makeText (this ,"latitude: " +location .getLatitude ()+" longitude: " +location .getLongitude (), Toast .LENGTH_SHORT ).show ();
131
- Log .d ("CurrentLocation" , "latitude: " + location .getLatitude () + " longitude: " + location .getLongitude ());
132
125
}
133
126
134
127
@ Override
135
- public void onAzimuthChanged (float azimuthFrom , float azimuthTo ) {
136
- azimuthReal = azimuthTo ;
137
- azimuthTeoretical = calculateTeoreticalAzimuth ();
128
+ public void onAzimuthChanged (float azimuthChangedFrom , float azimuthChangedTo ) {
129
+ mAzimuthReal = azimuthChangedTo ;
130
+ mAzimuthTeoretical = calculateTeoreticalAzimuth ();
138
131
139
- icon = (ImageView ) findViewById (R .id .icon );
132
+ pointerIcon = (ImageView ) findViewById (R .id .icon );
140
133
141
- double min = calculateAzimuthAccuracy (azimuthTeoretical ).get (0 );
142
- double max = calculateAzimuthAccuracy (azimuthTeoretical ).get (1 );
143
- boolean t ;
144
- if (isBetween (min , max , azimuthReal )) {
145
- icon .setVisibility (View .VISIBLE );
134
+ double minAngle = calculateAzimuthAccuracy (mAzimuthTeoretical ).get (0 );
135
+ double maxAngle = calculateAzimuthAccuracy (mAzimuthTeoretical ).get (1 );
136
+
137
+ if (isBetween (minAngle , maxAngle , mAzimuthReal )) {
138
+ pointerIcon .setVisibility (View .VISIBLE );
146
139
} else {
147
- icon .setVisibility (View .INVISIBLE );
140
+ pointerIcon .setVisibility (View .INVISIBLE );
148
141
}
149
142
150
- cameraText .setText (poi .getPoiName () + " azimuthTeoretical "
151
- + azimuthTeoretical + " azimuthReal " + azimuthReal + " lat "
152
- + lat + " lon " + longi );
153
- Log .d ("CurrentAzimuth" , "azimuth: " + azimuthTo );
143
+ descriptionTextView .setText (mPoi .getPoiName () + " mAzimuthTeoretical "
144
+ + mAzimuthTeoretical + " mAzimuthReal " + mAzimuthReal + " mMyLatitude "
145
+ + mMyLatitude + " lon " + mMyLongitude );
154
146
}
155
147
156
148
@ Override
@@ -178,51 +170,45 @@ private void setupListeners() {
178
170
179
171
180
172
private void setupLayout () {
181
- cameraText = (TextView ) findViewById (R .id .cameraTextView );
173
+ descriptionTextView = (TextView ) findViewById (R .id .cameraTextView );
182
174
183
- /** Set surface for camera view*/
184
175
getWindow ().setFormat (PixelFormat .UNKNOWN );
185
176
SurfaceView surfaceView = (SurfaceView ) findViewById (R .id .cameraview );
186
- surfaceHolder = surfaceView .getHolder ();
187
- surfaceHolder .addCallback (this );
188
- surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_PUSH_BUFFERS );
177
+ mSurfaceHolder = surfaceView .getHolder ();
178
+ mSurfaceHolder .addCallback (this );
179
+ mSurfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_PUSH_BUFFERS );
189
180
}
190
181
191
- //////////////////**Methods responsible for the camera view */////////////////////
192
182
@ Override
193
183
public void surfaceChanged (SurfaceHolder holder , int format , int width ,
194
184
int height ) {
195
-
196
- if (cameraview ) {
197
- camera .stopPreview ();
198
- cameraview = false ;
185
+ if (isCameraviewOn ) {
186
+ mCamera .stopPreview ();
187
+ isCameraviewOn = false ;
199
188
}
200
189
201
- if (camera != null ) {
190
+ if (mCamera != null ) {
202
191
try {
203
- camera .setPreviewDisplay (surfaceHolder );
204
- camera .startPreview ();
205
- cameraview = true ;
192
+ mCamera .setPreviewDisplay (mSurfaceHolder );
193
+ mCamera .startPreview ();
194
+ isCameraviewOn = true ;
206
195
} catch (IOException e ) {
207
-
208
196
e .printStackTrace ();
209
197
}
210
198
}
211
199
}
212
200
213
201
@ Override
214
202
public void surfaceCreated (SurfaceHolder holder ) {
215
-
216
- camera = Camera .open ();
217
- camera .setDisplayOrientation (90 );
203
+ mCamera = Camera .open ();
204
+ mCamera .setDisplayOrientation (90 );
218
205
}
219
206
220
207
@ Override
221
208
public void surfaceDestroyed (SurfaceHolder holder ) {
222
-
223
- camera .stopPreview ();
224
- camera .release ();
225
- camera = null ;
226
- cameraview = false ;
209
+ mCamera .stopPreview ();
210
+ mCamera .release ();
211
+ mCamera = null ;
212
+ isCameraviewOn = false ;
227
213
}
228
214
}
0 commit comments