1+ package io.github.the_dagger.mlkit.activity
2+
3+ import android.Manifest
4+ import android.content.Intent
5+ import android.content.pm.PackageManager
6+ import android.graphics.Bitmap
7+ import android.graphics.BitmapFactory
8+ import android.os.Bundle
9+ import android.support.design.widget.BottomSheetBehavior
10+ import android.support.v4.app.ActivityCompat
11+ import android.util.Log
12+ import android.view.View
13+ import android.widget.Toast
14+ import com.google.firebase.ml.vision.FirebaseVision
15+ import com.google.firebase.ml.vision.common.FirebaseVisionImage
16+ import io.github.the_dagger.mlkit.R
17+ import kotlinx.android.synthetic.main.activity_main.*
18+ import kotlinx.android.synthetic.main.layout_landmark.*
19+ import pl.aprilapps.easyphotopicker.DefaultCallback
20+ import pl.aprilapps.easyphotopicker.EasyImage
21+ import java.io.File
22+
23+
24+ class LandmarkDetectorActivity : BaseCameraActivity () {
25+
26+ override fun onCreate (savedInstanceState : Bundle ? ) {
27+ super .onCreate(savedInstanceState)
28+ setupBottomSheet(R .layout.layout_landmark)
29+ cameraView.visibility = View .GONE
30+ if (ActivityCompat .checkSelfPermission(this , Manifest .permission.WRITE_EXTERNAL_STORAGE )
31+ == PackageManager .PERMISSION_DENIED ) {
32+ ActivityCompat .requestPermissions(this , Array <String >(1 ) { Manifest .permission.WRITE_EXTERNAL_STORAGE }, 12345 )
33+ fab_take_photo.setOnClickListener(null )
34+ } else {
35+ fab_take_photo.setOnClickListener(this )
36+ }
37+ }
38+
39+ override fun onClick (v : View ? ) {
40+ // onClick attribute for the FloatingActionButton
41+ startIntentForPicker()
42+ }
43+
44+ private fun startIntentForPicker () {
45+ EasyImage .openGallery(this , 0 )
46+ }
47+
48+ override fun onRequestPermissionsResult (requestCode : Int , permissions : Array <out String >, grantResults : IntArray ) {
49+ super .onRequestPermissionsResult(requestCode, permissions, grantResults)
50+ if (requestCode == 12345 ) {
51+ if (grantResults[0 ] == PackageManager .PERMISSION_GRANTED )
52+ // The library requires write access to the External Storage,
53+ // so ensure that the permission is granted
54+ fab_take_photo.setOnClickListener(this )
55+ }
56+ }
57+
58+
59+ override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
60+ super .onActivityResult(requestCode, resultCode, data)
61+ if (ActivityCompat .checkSelfPermission(this , Manifest .permission.WRITE_EXTERNAL_STORAGE )
62+ == PackageManager .PERMISSION_GRANTED ) {
63+ EasyImage .handleActivityResult(requestCode, resultCode, data, this , object : DefaultCallback () {
64+
65+ override fun onImagePicked (imageFile : File ? , source : EasyImage .ImageSource ? , type : Int ) {
66+ val bitmap = BitmapFactory .decodeFile(imageFile?.path)
67+ getLandmarkFromCloud(bitmap)
68+ imagePreview.setImageBitmap(bitmap)
69+ framePreview.visibility = View .VISIBLE
70+ btnRetry.visibility = View .GONE
71+ }
72+
73+ override fun onImagePickerError (e : Exception ? , source : EasyImage .ImageSource ? , type : Int ) {
74+ // Some error handling since no image was picked
75+ }
76+
77+ })
78+ }
79+ }
80+
81+ private fun getLandmarkFromCloud (bitmap : Bitmap ) {
82+ val image = FirebaseVisionImage .fromBitmap(bitmap)
83+ val detector = FirebaseVision .getInstance()
84+ .visionCloudLandmarkDetector
85+
86+ detector.detectInImage(image)
87+ .addOnCompleteListener {
88+ Log .e(" TAG" , " completed" )
89+ for (firebaseVisionLandmarks in it.result) {
90+ val landmark = firebaseVisionLandmarks.landmark
91+ tvLocationName.text = landmark
92+ for (location in firebaseVisionLandmarks.locations) {
93+ val lat = location.latitude
94+ val long = location.longitude
95+ tvLatitude.text = lat.toString()
96+ tvLongitude.text = long.toString()
97+ }
98+ tvAccuracy.text = (firebaseVisionLandmarks.confidence * 100 ).toInt().toString()
99+ }
100+ }
101+ .addOnFailureListener {
102+ Toast .makeText(this , " Something went wrong" , Toast .LENGTH_SHORT ).show()
103+ }
104+ .addOnCompleteListener {
105+ sheetBehavior.state = BottomSheetBehavior .STATE_EXPANDED
106+ }
107+ }
108+
109+ }
0 commit comments