Untitled
unknown
plain_text
24 days ago
9.9 kB
3
Indexable
Never
public class MainActivity extends AppCompatActivity { private ArFragment arSceneView; private ModelRenderable routeRenderable; private TransformationSystem transformationSystem; private static final int CAMERA_PERMISSION_REQUEST_CODE = 100; private static final String CAMERA_PERMISSION = android.Manifest.permission.CAMERA; private Session arSession; double userLat = 0.0; double userLng = 0.0; double destinationLng = 0.0; double destinationLat = 0.0; double sourceLng = 0.0; double sourceLat = 0.0; private final DepthSettings depthSettings = new DepthSettings(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); arSceneView = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment); depthSettings.onCreate(this); FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); if (!ArCoreApk.getInstance().checkAvailability(this).isSupported()) { Toast.makeText(this, "ARCore is not supported on this device", Toast.LENGTH_SHORT).show(); } try { arSession = new Session(this); } catch (UnavailableArcoreNotInstalledException e) { // ARCore not installed } catch (UnavailableDeviceNotCompatibleException e) { // Device not compatible with ARCore } catch (Exception e) { e.printStackTrace(); } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } fusedLocationClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { // Got last known location if (location != null) { double userLat = location.getLatitude(); double userLng = location.getLongitude(); Log.d(TAG, "onSuccessuserL: "+userLng); Log.d(TAG, "onSuccessuserLat=: "+userLat); } } }); // Button to exit the AR activity Button exitButton = findViewById(R.id.exit_button); exitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); // Exit the AR view } }); // Check if Camera permission is granted if (checkCameraPermission()) { createRouteRenderable(); } else { requestCameraPermission(); } // Convert source and destination GPS to AR coordinates Vector3 sourceARPos = convertGPSPositionToAR(userLat, userLng, sourceLat, sourceLng); Vector3 destinationARPos = convertGPSPositionToAR(userLat, userLng, destinationLat, destinationLng); // Create an anchor at the source location Anchor sourceAnchor = arSession.createAnchor(new Pose(new float[]{sourceARPos.x, sourceARPos.y, sourceARPos.z}, new float[]{0, 0, 0, 1})); AnchorNode sourceNode = new AnchorNode(sourceAnchor); sourceNode.setRenderable(routeRenderable); arSceneView.getArSceneView().getScene().addChild(sourceNode); // Create an anchor at the destination Anchor destinationAnchor = arSession.createAnchor(new Pose(new float[]{destinationARPos.x, destinationARPos.y, destinationARPos.z}, new float[]{0, 0, 0, 1})); AnchorNode destinationNode = new AnchorNode(destinationAnchor); destinationNode.setRenderable(routeRenderable); arSceneView.getArSceneView().getScene().addChild(destinationNode); } public static final double EARTH_RADIUS = 6371000; // meters public Vector3 convertGPSPositionToAR(double lat1, double lng1, double lat2, double lng2) { double dLat = Math.toRadians(lat2 - lat1); double dLng = Math.toRadians(lng2 - lng1); double x = dLng * Math.cos(Math.toRadians(lat1)) * EARTH_RADIUS; double y = dLat * EARTH_RADIUS; return new Vector3((float) x, 0, (float) y); } // Method to check camera permission private boolean checkCameraPermission() { return ContextCompat.checkSelfPermission(this,CAMERA_PERMISSION ) == PackageManager.PERMISSION_GRANTED; } // Request camera permission private void requestCameraPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this,CAMERA_PERMISSION)) { // Explain to the user why the permission is necessary Toast.makeText(this, "Camera permission is required for AR functionality", Toast.LENGTH_SHORT).show(); } ActivityCompat.requestPermissions(this, new String[]{CAMERA_PERMISSION}, CAMERA_PERMISSION_REQUEST_CODE); } // Handle the result of the permission request @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "createRouteRenderable: 122212"); // Permission granted arSceneView.setOnTapArPlaneListener((HitResult hitResult, Plane plane, MotionEvent motionEvent) -> { if (routeRenderable == null) { return; } // Create an anchor at the tapped location Anchor anchor = hitResult.createAnchor(); // Place the arrow at the anchor placeArrow(anchor); }); createRouteRenderable(); } else { // Permission denied Toast.makeText(this, "Camera permission is needed to run AR", Toast.LENGTH_SHORT).show(); finish(); // Close the app or disable AR functionality } } } private void placeArrow(Anchor anchor) { // Create an AnchorNode at the anchor position // AnchorNode anchorNode = new AnchorNode(anchor); // anchorNode.setParent(arSceneView.getArSceneView().getScene()); AnchorNode startAnchorNode = new AnchorNode(anchor); startAnchorNode.setRenderable(routeRenderable); arSceneView.getArSceneView().getScene().addChild(startAnchorNode); // Create a TransformableNode and set the arrow model as its renderable TransformableNode arrow = new TransformableNode(arSceneView.getTransformationSystem()); arrow.setParent(startAnchorNode); arrow.setRenderable(routeRenderable); // Optional: Adjust the rotation or scale if needed arrow.getScaleController().setMaxScale(0.9f); arrow.getScaleController().setMinScale(0.5f); arrow.select(); // Highlight the arrow for transformation if needed } private void createRouteRenderable() { // Here you can load your 3D model for the route // // Load the 3D arrow model ModelRenderable.builder() .setSource(this, R.raw.direction_arrow) // Ensure arrow_model is in the raw directory .build() .thenAccept(renderable -> routeRenderable = renderable) .exceptionally( throwable -> { Toast.makeText(this, "Unable to load arrow model", Toast.LENGTH_LONG).show(); return null; }); // arFragment.getArSceneView().getScene().addOnUpdateListener(this::onUpdateFrame); } @Override protected void onResume() { super.onResume(); if (arSceneView != null) { arSceneView.onResume(); } try { if (arSession == null) { arSession = new Session(this); } arSceneView.getArSceneView().resume(); } catch (UnavailableArcoreNotInstalledException | UnavailableApkTooOldException | UnavailableSdkTooOldException e) { e.printStackTrace(); } catch (CameraNotAvailableException | UnavailableDeviceNotCompatibleException e) { throw new RuntimeException(e); } } @Override protected void onPause() { super.onPause(); if (arSceneView != null) { arSceneView.onPause(); } if (arSession != null) { arSceneView.getArSceneView().pause(); } } @Override protected void onDestroy() { super.onDestroy(); if (arSceneView != null) { arSceneView.onDestroy(); } } }
Leave a Comment