Skip to content

Commit

Permalink
[ADD/#46] text recognition 메서드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Jan 12, 2023
1 parent 07ad852 commit 88d1c4b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ dependencies {
// shared preference
implementation 'androidx.security:security-crypto-ktx:1.1.0-alpha03'

// google ml kit text recognition
implementation 'com.google.mlkit:text-recognition-korean:16.0.0-beta6'

// text features
implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.0.0'

// firebase storage
implementation 'com.google.firebase:firebase-storage'

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.keyneez.presentation.ocr

import android.graphics.Bitmap
import android.graphics.Paint
import android.os.Bundle
import android.view.View
Expand All @@ -8,9 +9,14 @@ import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.Text
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.korean.KoreanTextRecognizerOptions
import com.keyneez.presentation.ocr.dialog.OcrResultFragment
import com.keyneez.util.binding.BindingActivity
import com.keyneez.util.extension.setOnSingleClickListener
import com.keyneez.util.extension.showSnackbar
import com.lab.keyneez.R
import com.lab.keyneez.databinding.ActivityOcrBinding
import timber.log.Timber
Expand Down Expand Up @@ -82,6 +88,42 @@ class OcrActivity : BindingActivity<ActivityOcrBinding>(R.layout.activity_ocr) {
}
}

// Text Recognition

// CameraX -> OnImageCapturedListener & ImageAnalysis.Analyzer 활용해서 rotation 계산

private fun runTextRecognition(img: Bitmap) {
// 이미지 유형 : Bitmap, media.Image, ByteBuffer, byte array, device file
val image = InputImage.fromBitmap(img, 0)
val recognizer = TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
recognizer.process(image)
.addOnSuccessListener { visionText ->
processTextRecognitionResult(visionText)
}
.addOnFailureListener { e ->
showSnackbar(binding.root, getString(R.string.msg_error))
}
}

private fun processTextRecognitionResult(text: Text) {
if (text.textBlocks.size == 0) {
Timber.e("인식된 글자 없음")
showSnackbar(binding.root, "인식된 글자가 없습니다.")
return
}

for (block in text.textBlocks) {
Timber.d("block : $block")

for (line in block.lines) {
Timber.d("line : $line")

for (element in line.elements)
Timber.d("element : $element")
}
}
}

override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
Expand Down

0 comments on commit 88d1c4b

Please sign in to comment.