//中文
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'
val korean = TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
val chinese = TextRecognition.getClient(ChineseTextRecognizerOptions.Builder().build())
val japanese = TextRecognition.getClient(JapaneseTextRecognizerOptions.Builder().build())
//取得檔案
val file = File(cacheDir, "chinese.png")
val uri = file.toUri()
//轉成InputImage
val image = InputImage.fromFilePath(this, uri)
//取得檔案
val file = File(cacheDir, "chinese.png")
val bitmap = BitmapFactory.decodeFile(file.path)
//轉成InputImage
val image = InputImage.fromBitmap(bitmap, 0)
chinese.process(image)
.addOnSuccessListener { visionText ->
Log.e("success", visionText.text)
}
.addOnFailureListener { e ->
Log.e("failure", e.message.toString())
}
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())
}
}
}
ML Kit 免費圖片文字辨識 Text Recognition Github
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