Android StudioでGoogle Cloud Visionを使ったアプリを作っています。
googleが配布しているサンプルコードを改造して
公式が配布しているコードのGithubへのリンク
自分の作ったプロジェクトに落とし込みたいのですが、タイトルのエラーでうまく動きません。
文字数の関係でコードのサンプルからほとんど変えていない部分を省いています。
kotlin
1 2 3class resitoActivity : AppCompatActivity() { 4 5 private val CLOUD_VISION_API_KEY = "AIzaSyAsW84t4WfbN2EBqxmJccaEmAGJ7z1xBEA" 6 val FILE_NAME = "temp.jpg" 7 private val ANDROID_CERT_HEADER = "X-Android-Cert" 8 private val ANDROID_PACKAGE_HEADER = "X-Android-Package" 9 private val MAX_LABEL_RESULTS = 10 10 private val MAX_DIMENSION = 1200 11 12 private val TAG = resitoActivity::class.java.simpleName 13 private val GALLERY_PERMISSIONS_REQUEST = 0 14 private val GALLERY_IMAGE_REQUEST = 1 15 val CAMERA_PERMISSIONS_REQUEST = 2 16 val CAMERA_IMAGE_REQUEST = 3 17 18 private var mImageDetails: TextView? = null 19 private var mMainImage: ImageView? = null 20 21 22 override fun onCreate(savedInstanceState: Bundle?) { 23 24 super.onCreate(savedInstanceState) 25 setContentView(R.layout.activity_resito) 26 27 val toolbar = findViewById<Toolbar>(R.id.toolbar) 28 setSupportActionBar(toolbar) 29 // ギャラリーから画像選択か写真とるか選ぶやつ 30 31 //ギャラリー見るやつ 32 33 // カメラ起動 34 35 // カメラファイル取得 36 37 //ギャラリーイメージがあったらimageをアップロードする 38 39 //カメラで撮ったイメージがあったらimageをアップロードする 40 41 // ギャラリーかカメラか選択したのをどっちか判断して起動するやつ 42 43 44 45 private class LableDetectionTask internal constructor( 46 activity: resitoActivity, 47 private val mRequest: Vision.Images.Annotate) :AsyncTask<Any, Void, String>() { 48 private val mActivityWeakReference: WeakReference<resitoActivity> 49 50 init { 51 mActivityWeakReference = WeakReference(activity) 52 } 53 54 override fun doInBackground(vararg params: Any): String { 55 val tag = resitoActivity::class.java.simpleName 56 try { 57 Log.d(tag, "created Cloud Vision request object, sending request") 58 val response = mRequest.execute() 59 return convertResponseToString(response) 60 61 62 } catch (e: GoogleJsonResponseException) { 63 Log.d(tag, "failed to make API request because " + e.getContent()) 64 } catch (e: IOException) { 65 Log.d(tag, "failed to make API request because of other IOException " + e.message) 66 } 67 68 return "Cloud Vision API request failed. Check logs for details." 69 } 70 71 override fun onPostExecute(result: String) { 72 val activity : resitoActivity? = mActivityWeakReference.get() 73 if (activity != null && !activity.isFinishing) { 74 val imageDetail = activity.findViewById<TextView>(R.id.image_details) 75 imageDetail.text = result 76 } 77 } 78 } 79 80 private fun callCloudVision(bitmap: Bitmap) { 81 // Switch text to loading 82 mImageDetails?.setText(R.string.loading_message) 83 84 // Do the real work in an async task, because we need to use the network anyway 85 try { 86 val labelDetectionTask = LableDetectionTask(this, prepareAnnotationRequest(bitmap)) 87 labelDetectionTask.execute() 88 } catch (e: IOException) { 89 Log.d(TAG, "failed to make API request because of other IOException " + e.message) 90 } 91 92 } 93 94 private fun scaleBitmapDown(bitmap: Bitmap, maxDimension: Int): Bitmap { 95 96 val originalWidth = bitmap.width 97 val originalHeight = bitmap.height 98 var resizedWidth = maxDimension 99 var resizedHeight = maxDimension 100 101 if (originalHeight > originalWidth) { 102 resizedHeight = maxDimension 103 resizedWidth = (resizedHeight * originalWidth.toFloat() / originalHeight.toFloat()).toInt() 104 } else if (originalWidth > originalHeight) { 105 resizedWidth = maxDimension 106 resizedHeight = (resizedWidth * originalHeight.toFloat() / originalWidth.toFloat()).toInt() 107 } else if (originalHeight == originalWidth) { 108 resizedHeight = maxDimension 109 resizedWidth = maxDimension 110 } 111 return Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false) 112 } 113 114 private fun convertResponseToString(response: BatchAnnotateImagesResponse): String { 115 val message = StringBuilder("I found these things:\n\n") 116 117 val label = response.getResponses().get(0).getFullTextAnnotation() 118 var str: String? = null 119 str = label.getText() 120 121 var matchdate: String? = null 122 var regaxdate: String? = null 123 var regaxprice: String? = null 124 val priceList = ArrayList<String>() 125 val goodsList = ArrayList<String>() 126 var firstidx = 0 127 var lastidx = 0 128 129 //日付取得の処理 130 regaxdate = "\d{4}-\d{1,2}-\d{1,2}|\d{4}/\d{1,2}/\d{1,2}|\d{4}年\d{1,2}月\d{1,2}日" 131 val pdate = Pattern.compile(regaxdate) 132 val mdate = pdate.matcher(str!!) 133 if (mdate.find()) { 134 matchdate = mdate.group() 135 } 136 137 //値段取得の処理 138 regaxprice = "¥([,0-9]+)" 139 val pprice = Pattern.compile(regaxprice) 140 val mprice = pprice.matcher(str!!) 141 while (mprice.find()) { 142 priceList.add(mprice.group()) 143 } 144 145 //品名取得の処理 146 val regaxkei = ".*計.*" 147 val pkei = Pattern.compile(regaxkei) 148 val mkeinohou = pkei.matcher(str!!) //計マッチ 149 150 if (mkeinohou.find()) { 151 val matchdatehinmei = mdate.group() 152 firstidx = str!!.indexOf(matchdatehinmei) + matchdatehinmei.length 153 val matchkeihinmei = mkeinohou.group() 154 lastidx = str.indexOf(matchkeihinmei) 155 val datekarakei = str.substring(firstidx, lastidx) 156 datekarakei.replace("0", "") 157 datekarakei.replace("\", "") 158 message.append(datekarakei) 159 } else { 160 message.append("'計'がマッチしませんでした。") 161 } 162 163 if (str != null) { 164 // message.append("" + 165 // "文字認識できてます。" + "\n"); 166 if (matchdate != null) { 167 message.append(matchdate + "\n") 168 } else { 169 message.append("日付マッチしませんでした。" + "\n") 170 } 171 if (priceList != null) { 172 for (i in 0 until priceList.size - 1) { 173 message.append(priceList[i] + "\n") 174 } 175 } else { 176 message.append("価格マッチしませんでした。" + "\n") 177 } 178 } else { 179 message.append("認識されませんでした。" + "\n") 180 } 181 return message.toString() 182 183 } 184} 185
エラーの出ている箇所はoverride fun doInBackground(vararg params: Any): Stringの中の
kotlin
1return convertResponseToString(response)
で自分のプロジェクトに落とし込む前のサンプルプロジェクトではコンパイルは通っていました。
どう直したら解消できるか教えていただきたいです。
あなたの回答
tips
プレビュー