= PDDocument.load(File(pdfPath)) val renderer = PDFRenderer(document) val requestBuilder = BatchAnnotateImagesRequest.newBuilder() for (pageIndex in 0 until document.numberOfPages) { val image: BufferedImage = renderer.renderImageWithDPI(pageIndex, 300f) val byteArrayOutputStream = ByteArrayOutputStream() ImageIO.write(image, "png", byteArrayOutputStream) val imageBytes = ByteString.copyFrom(byteArrayOutputStream.toByteArray()) // Vision APIリクエストの構築 val visionImage = Image.newBuilder().setContent(imageBytes).build() val feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build() val request = AnnotateImageRequest.newBuilder().addFeatures(feature) .setImage(visionImage).build() requestBuilder.addRequests(request) } document.close() ① ② ①Cloud Vision APIは画像ファイルしか受け付けないので、PDFを画像に変換する ②画像に変換したPDFを渡してCloud Vision APIのrequestを作成する
= ImageAnnotatorClient.create(settings) val response = client.batchAnnotateImages(requestBuilder.build()) val list = response.responsesList.first().fullTextAnnotation.text.split("\r\n", "\n") client.close() ①Cloud Vision APIのclientを生成 ②requestを送信して分析結果を取得し、各行ごとの値のlistにsplitする ① ②