コード例 #1
0
  public ImageData recognize(byte[] jpeg) {

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addBinaryBody("imgFile", jpeg, ContentType.DEFAULT_BINARY, "cam.jpg");

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(uri);
    post.addHeader("Authorization", credentials.createAuthorizationHeaderValue());
    post.setEntity(builder.build());

    try {
      HttpResponse response = httpclient.execute(post);
      VisualRecognitionResponse r =
          objectMapper.readValue(
              EntityUtils.toString(response.getEntity()), VisualRecognitionResponse.class);
      return r.getImages().get(0);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
コード例 #2
0
 public VisualRecognitionService() {
   objectMapper = new CdiConfig().getObjectMapper();
   credentials = BluemixServices.getVisualRecognitionConfig().getCredentials();
   uri = credentials.getUrl() + "/v1/tag/recognize";
 }