Решение, которое я использовал, состоит в том, чтобы сгенерировать тело вручную, используя генератор отклика Multipart.
С старой версии 1.8.0:
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "utf-8")
json.setContentType("application/json")
// Step 2. Generating file part
val filePart = new FilePart("file", new ByteArrayPartSource("image.png", image), "image/png", null)
// Step 3. Build up the Multiparts
val reqE = new MultipartRequestEntity(
Array(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = new ByteArrayOutputStream()
reqE.writeRequest(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType()).
post(bos.toByteArray());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
С последней версией 1.9.31:
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "application/json", Charset.forName("utf-8"))
// Step 2. Generating file part
val filePart = new ByteArrayPart("image.png", scaledImage, "image/png")
// Step 3. Build up the Multiparts
val reqE = MultipartUtils.newMultipartBody(
util.Arrays.asList(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = ByteBuffer.allocate(scaledImage.length + 1024)
reqE.read(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType())
.post(bos.array());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
Что не работает? Не могли бы вы уточнить? – MirMasej
Извини, мой плохой. Open-ocr не может читать заголовки запросов – mavarazy