要使用OCRKing API,首先需要注冊一個賬戶并獲得API密鑰。然后,按照以下步驟使用Java進行API調用:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://api.ocrking.com/ok.html");
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addPart("file", new FileBody(new File("path/to/your/image.jpg")));
httpPost.setEntity(entityBuilder.build());
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseString);
String result = jsonNode.get("result").asText();
以上就是使用Java調用OCRKing API的基本步驟。根據OCRKing API的具體文檔和需求,可能還需要設置其他請求參數和處理其他響應字段。