Android

Android vision API OCR 影像辨識 圖片腥羶色

1.啟動API 並申請憑證

Google API console

2.文字OCR

POST的JSON
{
   "requests":[
      {
         "features":[
            {
               "type":"TEXT_DETECTION"
            }
         ],
         "image":{
            "content":"Base64過的File"
         },
         "imageContext":{
            "languageHints":[
               "zh",
               "en"
            ]
         }
      }
   ]
}
Model
data class Requests(val requests: List<Features>)

data class Features(val features: List<Type>, val image: Image,val imageContext : ImageContext)

data class Typee(val type: String)

data class Image(val content: String)

data class ImageContext(val languageHints: List<String>)
Okhttp
//將File轉byteArray再用Base64
val encode = Base64.encodeToString(file2byte(path), Base64.DEFAULT)

val features = mutableListOf<Features>()
val type = mutableListOf<Typee>()
val languageHints = mutableListOf<String>()
val image = Image(encode)
val imageContext = ImageContext(languageHints)

languageHints.add("zh")
languageHints.add("en")
type.add(Typee("DOCUMENT_TEXT_DETECTION"))
features.add(Features(type, image,imageContext))
val json = Gson().toJson(Requests(features))

val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, json)

val client = OkHttpClient().newBuilder().build()
val request: Request = Request.Builder()
   .url("https://vision.googleapis.com/v1/images:annotate?key=憑證的key")
   .post(body)
   .build()

client.newCall(request).enqueue(object : Callback {
   override fun onFailure(call: Call, e: IOException) {

   }

   override fun onResponse(call: Call, response: Response) {
     response.body?.string()?.let { Log.d("GGG", it) }
   }
})

fun file2byte(filePath: String): ByteArray {
    val buffer: ByteArray
    val fis = FileInputStream(File(filePath))
    val bos = ByteArrayOutputStream()
    val b = ByteArray(1024)
    var n: Int
    while (fis.read(b).also { n = it } != -1) {
        bos.write(b, 0, n)
    }
    fis.close()
    bos.close()
    buffer = bos.toByteArray()
    return buffer
}

3.偵測腥羶色

POST的JSON
{
   "requests":[
      {
         "features":[
            {
               "type":"SAFE_SEARCH_DETECTION"
            }
         ],
         "image":{
            "content":"Base64過的File"
         }
      }
   ]
}
Okhttp
//將File轉byteArray再用Base64
val encode = Base64.encodeToString(file2byte(path), Base64.DEFAULT)
val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
val body: RequestBody = RequestBody.create(mediaType, "{\"requests\": [{\"features\": [{\"type\": \"SAFE_SEARCH_DETECTION\"},],\"image\": {\"content\": \"  $encode \"}}]}")

val client = OkHttpClient().newBuilder().build()
val request: Request = Request.Builder()
   .url("https://vision.googleapis.com/v1/images:annotate?key=憑證的key")
   .method("POST", body)
   .build()

client.newCall(request).enqueue(object : Callback {
   override fun onFailure(call: Call, e: IOException) {

   }

   override fun onResponse(call: Call, response: Response) {
     response.body?.string()?.let { Log.d("GGG", it) }
   }
})

fun file2byte(filePath: String): ByteArray {
    val buffer: ByteArray
    val fis = FileInputStream(File(filePath))
    val bos = ByteArrayOutputStream()
    val b = ByteArray(1024)
    var n: Int
    while (fis.read(b).also { n = it } != -1) {
        bos.write(b, 0, n)
    }
    fis.close()
    bos.close()
    buffer = bos.toByteArray()
    return buffer
}

發表迴響