Android

ML Kit 免費圖片文字辨識 Text Recognition

ML Kit 免費圖片文字辨識 Text Recognition

ML Kit 免費圖片文字辨識 Text Recognition 可以辨識出中文,這個SDK是免費的,讓你感受到OCR的力量,雖然辨識度沒有到很好但我相信Google團隊會慢慢開發越來越厲害。

文章目錄

  1. 導入Recognition Library
  2. 設定辨識的文字語言
  3. 取得要辨識的圖片
  4. 辨識圖片後回傳的資料
  5. 程式範例
  6. 效果展示
  7. Github
  8. 相關參考

1.導入Recognition Library

//中文
implementation 'com.google.mlkit:text-recognition-chinese:16.0.0-beta1'
//日文
implementation 'com.google.mlkit:text-recognition-japanese:16.0.0-beta1'
//韓文
implementation 'com.google.mlkit:text-recognition-korean:16.0.0-beta1'

2.設定辨識的文字語言

val korean = TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
val chinese = TextRecognition.getClient(ChineseTextRecognizerOptions.Builder().build())
val japanese = TextRecognition.getClient(JapaneseTextRecognizerOptions.Builder().build())

3.取得要辨識的圖片

圖片在App cache內

a.取得Uri
//取得檔案
val file = File(cacheDir, "chinese.png")
val uri = file.toUri()
//轉成InputImage
val image = InputImage.fromFilePath(this, uri)
b.取得Bitmap
//取得檔案
val file = File(cacheDir, "chinese.png")
val bitmap = BitmapFactory.decodeFile(file.path)
//轉成InputImage
val image = InputImage.fromBitmap(bitmap, 0)

4.辨識圖片後回傳的資料

chinese.process(image)
    .addOnSuccessListener { visionText ->
        Log.e("success", visionText.text)
    }
    .addOnFailureListener { e ->
        Log.e("failure", e.message.toString())
    }

5.程式範例

package com.example.recognizetext

import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.TextRecognizer
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions
import com.google.mlkit.vision.text.japanese.JapaneseTextRecognizerOptions
import com.google.mlkit.vision.text.korean.KoreanTextRecognizerOptions
import java.io.File

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val korean = TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
        val chinese = TextRecognition.getClient(ChineseTextRecognizerOptions.Builder().build())
        val japanese = TextRecognition.getClient(JapaneseTextRecognizerOptions.Builder().build())

        //中文
        getText(chinese, File(cacheDir, "chinese.png"))
        //日文
        getText(japanese, File(cacheDir, "japanese.png"))
        //韓文
        getText(korean, File(cacheDir, "korean.png"))
        //英文
        getText(chinese, File(cacheDir, "english.png"))
    }

    private fun getText(textRecognizer: TextRecognizer, file: File) {
        val bitmap = BitmapFactory.decodeFile(file.path)
        val image = InputImage.fromBitmap(bitmap, 0)
        textRecognizer.process(image)
            .addOnSuccessListener { visionText ->
                Log.e("success", visionText.text)
            }
            .addOnFailureListener { e ->
                Log.e("failure", e.message.toString())
            }
    }
}

6.效果展示

7.Github

ML Kit 免費圖片文字辨識 Text Recognition Github

8.相關參考

https://developers.google.com/ml-kit/vision/text-recognition/v2
https://developers.google.com/ml-kit/vision/text-recognition/v2/languages
https://developers.google.com/ml-kit/vision/text-recognition/v2/android

發表迴響