Skip to content

Commit

Permalink
modify the call for image to text in home activity
Browse files Browse the repository at this point in the history
  • Loading branch information
ryccoatika committed Nov 16, 2020
1 parent 5d3ea69 commit 82ce37a
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions app/src/main/java/com/ryccoatika/imagetotext/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.ml.vision.FirebaseVision
import com.google.firebase.ml.vision.common.FirebaseVisionImage
import com.jakewharton.rxbinding3.widget.textChangeEvents
import com.ryccoatika.imagetotext.R
import com.ryccoatika.imagetotext.core.domain.model.TextScanned
import com.ryccoatika.imagetotext.core.ui.HomeAdapter
import com.ryccoatika.imagetotext.core.utils.ImageToText
import com.ryccoatika.imagetotext.textscanneddetail.TextScannedDetailActivity
import com.theartofdev.edmodo.cropper.CropImage
import io.reactivex.Observer
Expand Down Expand Up @@ -106,29 +105,6 @@ class HomeActivity: AppCompatActivity(), HomeView {
homeViewModel.getAllTextScanned()
}

private fun imageToText(uri: Uri) {
val detector = FirebaseVision.getInstance().onDeviceTextRecognizer
val image = FirebaseVisionImage.fromFilePath(applicationContext, uri)

detector.processImage(image)
.addOnCompleteListener { firebaseVisionText ->
firebaseVisionText.result?.let { textResult ->
if (textResult.text.isEmpty()) {
showToast(getString(R.string.text_not_detected))
} else {
val textScanned = TextScanned(
dateTime = System.currentTimeMillis(),
textResult.text
)
homeViewModel.insertTextScanned(textScanned)
}
}
}
.addOnFailureListener { error ->
error.printStackTrace()
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
Expand Down Expand Up @@ -171,7 +147,25 @@ class HomeActivity: AppCompatActivity(), HomeView {
CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
val uri = CropImage.getActivityResult(data).uri
imageToText(uri)
val imageToText = ImageToText.Builder(this)
.addOnCompleteListener { result ->
if (result != null) {
if (result.isEmpty()) {
showToast(getString(R.string.text_not_detected))
} else {
val textScanned = TextScanned(
dateTime = System.currentTimeMillis(),
result
)
homeViewModel.insertTextScanned(textScanned)
}
}
}
.addOnFailureListener { error ->
error.printStackTrace()
}
.build()
imageToText.recognize(uri)
}
}
}
Expand Down

0 comments on commit 82ce37a

Please sign in to comment.