{"id":816,"date":"2020-10-24T13:35:37","date_gmt":"2020-10-24T05:35:37","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=816"},"modified":"2021-03-19T16:59:25","modified_gmt":"2021-03-19T08:59:25","slug":"vision-ai%e6%96%87%e5%ad%97ocr%e5%9c%96%e7%89%87%e8%85%a5%e7%be%b6%e8%89%b2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/vision-ai%e6%96%87%e5%ad%97ocr%e5%9c%96%e7%89%87%e8%85%a5%e7%be%b6%e8%89%b2\/","title":{"rendered":"Android vision API OCR \u5f71\u50cf\u8fa8\u8b58 \u5716\u7247\u8165\u7fb6\u8272"},"content":{"rendered":"<h4>1.\u555f\u52d5API \u4e26\u7533\u8acb\u6191\u8b49<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/console.cloud.google.com\/marketplace\/product\/google\/vision.googleapis.com?q=search&amp;referrer=search&amp;project=protean-bit-287508\" title=\"Google API console\" target=\"_blank\" rel=\"noopener\">Google API console<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/vision-API.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/vision-API-300x176.png\" alt=\"\" \/><\/a><\/p>\n<h4>2.\u6587\u5b57OCR<\/h4>\n<h5>POST\u7684JSON<\/h5>\n<pre><code class=\"language-JSON line-numbers\">{\n   \"requests\":[\n      {\n         \"features\":[\n            {\n               \"type\":\"TEXT_DETECTION\"\n            }\n         ],\n         \"image\":{\n            \"content\":\"Base64\u904e\u7684File\"\n         },\n         \"imageContext\":{\n            \"languageHints\":[\n               \"zh\",\n               \"en\"\n            ]\n         }\n      }\n   ]\n}\n<\/code><\/pre>\n<h5>Model<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">data class Requests(val requests: List&lt;Features&gt;)\n\ndata class Features(val features: List&lt;Type&gt;, val image: Image,val imageContext : ImageContext)\n\ndata class Typee(val type: String)\n\ndata class Image(val content: String)\n\ndata class ImageContext(val languageHints: List&lt;String&gt;)\n<\/code><\/pre>\n<h5>Okhttp<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u5c07File\u8f49byteArray\u518d\u7528Base64\nval encode = Base64.encodeToString(file2byte(path), Base64.DEFAULT)\n\nval features = mutableListOf&lt;Features&gt;()\nval type = mutableListOf&lt;Typee&gt;()\nval languageHints = mutableListOf&lt;String&gt;()\nval image = Image(encode)\nval imageContext = ImageContext(languageHints)\n\nlanguageHints.add(\"zh\")\nlanguageHints.add(\"en\")\ntype.add(Typee(\"DOCUMENT_TEXT_DETECTION\"))\nfeatures.add(Features(type, image,imageContext))\nval json = Gson().toJson(Requests(features))\n\nval mediaType = MediaType.parse(\"application\/json\")\nval body = RequestBody.create(mediaType, json)\n\nval client = OkHttpClient().newBuilder().build()\nval request: Request = Request.Builder()\n   .url(\"https:\/\/vision.googleapis.com\/v1\/images:annotate?key=\u6191\u8b49\u7684key\")\n   .post(body)\n   .build()\n\nclient.newCall(request).enqueue(object : Callback {\n   override fun onFailure(call: Call, e: IOException) {\n\n   }\n\n   override fun onResponse(call: Call, response: Response) {\n     response.body?.string()?.let { Log.d(\"GGG\", it) }\n   }\n})\n\nfun file2byte(filePath: String): ByteArray {\n    val buffer: ByteArray\n    val fis = FileInputStream(File(filePath))\n    val bos = ByteArrayOutputStream()\n    val b = ByteArray(1024)\n    var n: Int\n    while (fis.read(b).also { n = it } != -1) {\n        bos.write(b, 0, n)\n    }\n    fis.close()\n    bos.close()\n    buffer = bos.toByteArray()\n    return buffer\n}\n<\/code><\/pre>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/ff-251x300.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/ff-251x300.jpg\" alt=\"\" \/><\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/text-2.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/text-2.png\" alt=\"\" \/><\/a><\/p>\n<h4>3.\u5075\u6e2c\u8165\u7fb6\u8272<\/h4>\n<h5>POST\u7684JSON<\/h5>\n<pre><code class=\"language-JSON line-numbers\">{\n   \"requests\":[\n      {\n         \"features\":[\n            {\n               \"type\":\"SAFE_SEARCH_DETECTION\"\n            }\n         ],\n         \"image\":{\n            \"content\":\"Base64\u904e\u7684File\"\n         }\n      }\n   ]\n}\n<\/code><\/pre>\n<h5>Okhttp<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u5c07File\u8f49byteArray\u518d\u7528Base64\nval encode = Base64.encodeToString(file2byte(path), Base64.DEFAULT)\nval mediaType = \"application\/json; charset=utf-8\".toMediaTypeOrNull()\nval body: RequestBody = RequestBody.create(mediaType, \"{\\\"requests\\\": [{\\\"features\\\": [{\\\"type\\\": \\\"SAFE_SEARCH_DETECTION\\\"},],\\\"image\\\": {\\\"content\\\": \\\"  $encode \\\"}}]}\")\n\nval client = OkHttpClient().newBuilder().build()\nval request: Request = Request.Builder()\n   .url(\"https:\/\/vision.googleapis.com\/v1\/images:annotate?key=\u6191\u8b49\u7684key\")\n   .method(\"POST\", body)\n   .build()\n\nclient.newCall(request).enqueue(object : Callback {\n   override fun onFailure(call: Call, e: IOException) {\n\n   }\n\n   override fun onResponse(call: Call, response: Response) {\n     response.body?.string()?.let { Log.d(\"GGG\", it) }\n   }\n})\n\nfun file2byte(filePath: String): ByteArray {\n    val buffer: ByteArray\n    val fis = FileInputStream(File(filePath))\n    val bos = ByteArrayOutputStream()\n    val b = ByteArray(1024)\n    var n: Int\n    while (fis.read(b).also { n = it } != -1) {\n        bos.write(b, 0, n)\n    }\n    fis.close()\n    bos.close()\n    buffer = bos.toByteArray()\n    return buffer\n}\n<\/code><\/pre>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/adult.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/adult.jpg\" alt=\"\" \/><\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/safeSearchAnnotation.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/10\/safeSearchAnnotation.png\" alt=\"\" \/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u555f\u52d5API \u4e26\u7533\u8acb\u6191\u8b49 Google API console 2.\u6587\u5b57OCR POST\u7684JSON { &#8220;r &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,129],"class_list":["post-816","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-vision"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=816"}],"version-history":[{"count":12,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/816\/revisions"}],"predecessor-version":[{"id":1284,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/816\/revisions\/1284"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}