From 88d1c4bdf699d95d5bea93c5a2c7069bd62a99ee Mon Sep 17 00:00:00 2001 From: b1urrrr Date: Fri, 13 Jan 2023 02:59:12 +0900 Subject: [PATCH] =?UTF-8?q?[ADD/#46]=20text=20recognition=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 9 ++++ .../keyneez/presentation/ocr/OcrActivity.kt | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 149a61c..afb1642 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt b/app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt index 9f012df..77817d3 100644 --- a/app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt +++ b/app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt @@ -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 @@ -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 @@ -82,6 +88,42 @@ class OcrActivity : BindingActivity(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()