Skip to content

Commit e19a34c

Browse files
author
lycha
committed
Small code improvements
1 parent f09748a commit e19a34c

9 files changed

+124
-135
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Android Implementation of Augmented Reality
22

33
This is a simple example how to implement Augmented Reality on Android device. You can see this project running on a video: https://www.youtube.com/watch?v=zFb1NTGYhoU
4+
Example was tested on Nexus 4 and Samsung Galaxy S4. There is possibility that on different devices sensors needs to be implemented in different way.

app/app.iml

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
1313
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
1414
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
15-
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
1615
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
1716
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
18-
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
17+
<afterSyncTasks>
18+
<task>generateDebugAndroidTestSources</task>
19+
<task>generateDebugSources</task>
20+
</afterSyncTasks>
1921
<option name="ALLOW_USER_CONFIGURATION" value="false" />
2022
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
2123
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
@@ -24,7 +26,7 @@
2426
</configuration>
2527
</facet>
2628
</component>
27-
<component name="NewModuleRootManager" inherit-compiler-output="false">
29+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
2830
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
2931
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" />
3032
<exclude-output />

app/src/main/AndroidManifest.xml

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
android:label="@string/app_name"
2020
android:theme="@style/Theme.AppCompat.Light" >
2121

22-
<uses-library android:name="com.google.android.maps" />
23-
2422
<activity
2523
android:name="com.lycha.example.augmentedreality.CameraViewActivity"
2624
android:label="@string/title_activity_camera_view" >
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
package com.lycha.example.augmentedreality;
2-
2+
/**
3+
* Created by krzysztofjackowski on 24/09/15.
4+
*/
35
public class AugmentedPOI {
4-
private int poiId;
5-
private String poiName;
6-
private String poiDescription;
7-
private double poiLatitude;
8-
private double poiLongitude;
6+
private int mId;
7+
private String mName;
8+
private String mDescription;
9+
private double mLatitude;
10+
private double mLongitude;
911

1012
public AugmentedPOI(String newName, String newDescription,
1113
double newLatitude, double newLongitude) {
12-
this.poiName = newName;
13-
this.poiDescription = newDescription;
14-
this.poiLatitude = newLatitude;
15-
this.poiLongitude = newLongitude;
14+
this.mName = newName;
15+
this.mDescription = newDescription;
16+
this.mLatitude = newLatitude;
17+
this.mLongitude = newLongitude;
1618
}
1719

1820
public int getPoiId() {
19-
return poiId;
21+
return mId;
2022
}
2123
public void setPoiId(int poiId) {
22-
this.poiId = poiId;
24+
this.mId = poiId;
2325
}
2426
public String getPoiName() {
25-
return poiName;
27+
return mName;
2628
}
2729
public void setPoiName(String poiName) {
28-
this.poiName = poiName;
30+
this.mName = poiName;
2931
}
3032
public String getPoiDescription() {
31-
return poiDescription;
33+
return mDescription;
3234
}
3335
public void setPoiDescription(String poiDescription) {
34-
this.poiDescription = poiDescription;
36+
this.mDescription = poiDescription;
3537
}
3638
public double getPoiLatitude() {
37-
return poiLatitude;
39+
return mLatitude;
3840
}
3941
public void setPoiLatitude(double poiLatitude) {
40-
this.poiLatitude = poiLatitude;
42+
this.mLatitude = poiLatitude;
4143
}
4244
public double getPoiLongitude() {
43-
return poiLongitude;
45+
return mLongitude;
4446
}
4547
public void setPoiLongitude(double poiLongitude) {
46-
this.poiLongitude = poiLongitude;
48+
this.mLongitude = poiLongitude;
4749
}
4850
}

app/src/main/java/com/lycha/example/augmentedreality/CameraViewActivity.java

+85-99
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.hardware.Camera;
77
import android.location.Location;
88
import android.os.Bundle;
9-
import android.util.Log;
109
import android.view.SurfaceHolder;
1110
import android.view.SurfaceView;
1211
import android.view.View;
@@ -17,28 +16,29 @@
1716
import java.io.IOException;
1817
import java.util.ArrayList;
1918
import java.util.List;
20-
19+
/**
20+
* Created by krzysztofjackowski on 24/09/15.
21+
*/
2122
public class CameraViewActivity extends Activity implements
22-
SurfaceHolder.Callback, MyCurrentLocationListener, OnAzimuthChangedListener{
23+
SurfaceHolder.Callback, OnLocationChangedListener, OnAzimuthChangedListener{
2324

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;
2829

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;
3435

3536
private MyCurrentAzimuth myCurrentAzimuth;
3637
private MyCurrentLocation myCurrentLocation;
3738

38-
TextView cameraText;
39-
ImageView icon;
39+
TextView descriptionTextView;
40+
ImageView pointerIcon;
4041

41-
/** Called when the activity is first created. */
4242
@Override
4343
public void onCreate(Bundle savedInstanceState) {
4444
super.onCreate(savedInstanceState);
@@ -51,106 +51,98 @@ public void onCreate(Bundle savedInstanceState) {
5151
}
5252

5353
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
5959
);
6060
}
6161

6262
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;
7365

74-
double fi;
75-
double tanFi;
76-
double A = 0;
66+
double phiAngle;
67+
double tanPhi;
68+
double azimuth = 0;
7769

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);
8173

8274
if (dX > 0 && dY > 0) { // I quater
83-
return A = fi;
75+
return azimuth = phiAngle;
8476
} else if (dX < 0 && dY > 0) { // II
85-
return A = 180 - fi;
77+
return azimuth = 180 - phiAngle;
8678
} else if (dX < 0 && dY < 0) { // III
87-
return A = 180 + fi;
79+
return azimuth = 180 + phiAngle;
8880
} else if (dX > 0 && dY < 0) { // IV
89-
return A = 360 - fi;
81+
return azimuth = 360 - phiAngle;
9082
}
9183

92-
return fi;
84+
return phiAngle;
9385
}
9486

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;
9890
List<Double> minMax = new ArrayList<Double>();
9991

100-
if (min < 0)
101-
min += 360;
92+
if (minAngle < 0)
93+
minAngle += 360;
94+
95+
if (maxAngle >= 360)
96+
maxAngle -= 360;
10297

103-
if (max >= 360)
104-
max -= 360;
10598
minMax.clear();
106-
minMax.add(min);
107-
minMax.add(max);
99+
minMax.add(minAngle);
100+
minMax.add(maxAngle);
101+
108102
return minMax;
109103
}
110104

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))
114108
return true;
115109
} else {
116-
if (A > min && A < max)
110+
if (azimuth > minAngle && azimuth < maxAngle)
117111
return true;
118112
}
119113
return false;
120114
}
121115

122116
@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();
130124
Toast.makeText(this,"latitude: "+location.getLatitude()+" longitude: "+location.getLongitude(), Toast.LENGTH_SHORT).show();
131-
Log.d("CurrentLocation", "latitude: " + location.getLatitude() + " longitude: " + location.getLongitude());
132125
}
133126

134127
@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();
138131

139-
icon = (ImageView) findViewById(R.id.icon);
132+
pointerIcon = (ImageView) findViewById(R.id.icon);
140133

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);
146139
} else {
147-
icon.setVisibility(View.INVISIBLE);
140+
pointerIcon.setVisibility(View.INVISIBLE);
148141
}
149142

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);
154146
}
155147

156148
@Override
@@ -178,51 +170,45 @@ private void setupListeners() {
178170

179171

180172
private void setupLayout() {
181-
cameraText = (TextView) findViewById(R.id.cameraTextView);
173+
descriptionTextView = (TextView) findViewById(R.id.cameraTextView);
182174

183-
/** Set surface for camera view*/
184175
getWindow().setFormat(PixelFormat.UNKNOWN);
185176
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);
189180
}
190181

191-
//////////////////**Methods responsible for the camera view */////////////////////
192182
@Override
193183
public void surfaceChanged(SurfaceHolder holder, int format, int width,
194184
int height) {
195-
196-
if (cameraview) {
197-
camera.stopPreview();
198-
cameraview = false;
185+
if (isCameraviewOn) {
186+
mCamera.stopPreview();
187+
isCameraviewOn = false;
199188
}
200189

201-
if (camera != null) {
190+
if (mCamera != null) {
202191
try {
203-
camera.setPreviewDisplay(surfaceHolder);
204-
camera.startPreview();
205-
cameraview = true;
192+
mCamera.setPreviewDisplay(mSurfaceHolder);
193+
mCamera.startPreview();
194+
isCameraviewOn = true;
206195
} catch (IOException e) {
207-
208196
e.printStackTrace();
209197
}
210198
}
211199
}
212200

213201
@Override
214202
public void surfaceCreated(SurfaceHolder holder) {
215-
216-
camera = Camera.open();
217-
camera.setDisplayOrientation(90);
203+
mCamera = Camera.open();
204+
mCamera.setDisplayOrientation(90);
218205
}
219206

220207
@Override
221208
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;
227213
}
228214
}

0 commit comments

Comments
 (0)