How to use GoogleRequest class of com.galenframework.ocr.google.pojo.request package

Best Galen code snippet using com.galenframework.ocr.google.pojo.request.GoogleRequest

Source:GoogleVisionOcrService.java Github

copy

Full Screen

...29import com.galenframework.config.GalenConfig;30import com.galenframework.config.GalenProperty;31import com.galenframework.ocr.google.pojo.GoogleModel;32import com.galenframework.ocr.google.pojo.request.Feature;33import com.galenframework.ocr.google.pojo.request.GoogleRequest;34import com.galenframework.ocr.google.pojo.request.Image;35import com.galenframework.ocr.google.pojo.request.Request;36import com.galenframework.page.Rect;37import com.galenframework.validation.ValidationErrorException;38import org.apache.commons.io.IOUtils;39import org.apache.http.HttpResponse;40import org.apache.http.client.HttpClient;41import org.apache.http.client.methods.HttpPost;42import org.apache.http.entity.StringEntity;43import org.apache.http.impl.client.HttpClients;44/**45 * Implementation of the OcrService.46 * Cache the model, so if the same image is used, will send a single OCR request to the service.47 * @author guy arieli, Ivan Shubin48 *49 */50public class GoogleVisionOcrService implements OcrService {51 private final static String BASE_URL = "https://vision.googleapis.com/v1/images:annotate?key=";52 final static HttpClient httpClient = HttpClients.createDefault();53 final static ObjectMapper objectMapper = new ObjectMapper();54 @Override55 public OcrResult findOcrText(BufferedImage image, Rect rect) throws ValidationErrorException {56 if (rect.getRight() > image.getWidth() && rect.getBottom() > rect.getHeight()) {57 throw new ValidationErrorException("Could not extract element image. Looks like it is located outside of screenshot area");58 }59 try {60 BufferedImage croppedImage = image.getSubimage(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight());61 GoogleModel model = getGoogleModel(croppedImage);62 if (model.responses != null && !model.responses.isEmpty()) {63 String resultedText = model.responses.get(0).fullTextAnnotation.text;64 if (resultedText == null) {65 resultedText = "";66 }67 return new OcrResult(new String(resultedText.getBytes(Charset.forName("utf-8"))), rect);68 } else {69 throw new NullPointerException("Got empty result");70 }71 } catch (Exception e) {72 throw new ValidationErrorException("Google vision API error: " + e.getMessage(), e);73 }74 }75 public static GoogleModel getGoogleModel(BufferedImage img) throws Exception {76 String key = GalenConfig.getConfig().readProperty(GalenProperty.GALEN_OCR_GOOGLE_VISION_KEY);77 if (key == null) {78 throw new RuntimeException("Missing property " + GalenProperty.GALEN_OCR_GOOGLE_VISION_KEY + ". See https://cloud.google.com/vision/docs/auth for more info");79 }80 GoogleRequest grequest = new GoogleRequest();81 List<Request> requests = new ArrayList<>();82 Request request = new Request();83 requests.add(request);84 Image image = new Image();85 image.setContent(imgToBase64String(img, "PNG"));86 request.setImage(image);87 grequest.setRequests(requests);88 List<Feature> features = new ArrayList<>();89 Feature feature = new Feature();90 feature.setType("TEXT_DETECTION");91 feature.setMaxResults(1);92 request.setFeatures(features);93 features.add(feature);94 return postOcrImage(key, grequest);95 }96 private static GoogleModel postOcrImage(String key, GoogleRequest grequest) throws IOException {97 String url = BASE_URL + key;98 HttpResponse response = post(url, grequest);99 int status = response.getStatusLine().getStatusCode();100 String responseText = IOUtils.toString(response.getEntity().getContent());101 if (status < 400) {102 System.out.println("\n" + responseText);103 return objectMapper.readValue(responseText, GoogleModel.class);104 } else {105 String message;106 try {107 JsonNode tree = objectMapper.readTree(responseText);108 message = tree.get("error").get("message").asText();109 } catch (Exception ex) {110 message = responseText;...

Full Screen

Full Screen

Source:GoogleRequest.java Github

copy

Full Screen

...16package com.galenframework.ocr.google.pojo.request;17import java.util.List;18import com.google.gson.annotations.Expose;19import com.google.gson.annotations.SerializedName;20public class GoogleRequest {21 @SerializedName("requests")22 @Expose23 private List<Request> requests = null;24 public List<Request> getRequests() {25 return requests;26 }27 public void setRequests(List<Request> requests) {28 this.requests = requests;29 }30}...

Full Screen

Full Screen

GoogleRequest

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.google.pojo.request.GoogleRequest;2import com.galenframework.ocr.google.pojo.request.Request;3import com.galenframework.ocr.google.pojo.request.Feature;4import com.galenframework.ocr.google.pojo.request.Features;5import com.galenframework.ocr.google.pojo.request.Image;6import com.galenframework.ocr.google.pojo.request.ImageContext;7import com.galenframework.ocr.google.pojo.request.ImageContexts;8import com.galenframework.ocr.google.pojo.request.ImageRequest;9import com.galenframework.ocr.google.pojo.request.Pages;10import com.galenframework.ocr.google.pojo.request.Requests;11import com.galenframework.ocr.google.pojo.response.GoogleResponse;12import com.galenframework.ocr.google.pojo.response.Response;13import com.galenframework.ocr.google.pojo.response.TextAnnotations;14import com.galenframework.ocr.google.pojo.response.Vertex;15import com.galenframework.ocr.google.GoogleVisionAPI;16public class TestGoogleVisionAPI {17public static void main(String[] args) {18GoogleRequest googleRequest = new GoogleRequest();19Request request = new Request();20Features features = new Features();21Feature feature = new Feature();22feature.setType("TEXT_DETECTION");23feature.setMaxResults(1);24features.addFeature(feature);25request.setFeatures(features);26ImageContexts imageContexts = new ImageContexts();27ImageContext imageContext = new ImageContext();28Pages pages = new Pages();29pages.addPage(1);30imageContext.setPages(pages);31imageContexts.addImageContext(imageContext);32request.setImageContexts(imageContexts);33Requests requests = new Requests();34requests.addRequest(request);35googleRequest.setRequests(requests);

Full Screen

Full Screen

GoogleRequest

Using AI Code Generation

copy

Full Screen

1package com.galenframework.ocr.google.pojo.request;2import java.util.ArrayList;3import java.util.List;4import com.google.gson.annotations.Expose;5import com.google.gson.annotations.SerializedName;6public class GoogleRequest {7@SerializedName("requests")8private List<Request> requests = new ArrayList<Request>();9public List<Request> getRequests() {10return requests;11}12public void setRequests(List<Request> requests) {13this.requests = requests;14}15}16package com.galenframework.ocr.google.pojo.response;17import java.util.ArrayList;18import java.util.List;19import com.google.gson.annotations.Expose;20import com.google.gson.annotations.SerializedName;21public class GoogleResponse {22@SerializedName("responses")23private List<Response> responses = new ArrayList<Response>();24public List<Response> getResponses() {25return responses;26}27public void setResponses(List<Response> responses) {28this.responses = responses;29}30}31package com.galenframework.ocr.google.pojo.request;32import com.google.gson.annotations.Expose;33import com.google.gson.annotations.SerializedName;34public class Request {35@SerializedName("image")36private Image image;37@SerializedName("features")38private List<Feature> features = new ArrayList<Feature>();39public Image getImage() {40return image;41}42public void setImage(Image image) {43this.image = image;44}45public List<Feature> getFeatures() {46return features;47}48public void setFeatures(List<Feature> features) {49this.features = features;50}51}52package com.galenframework.ocr.google.pojo.response;53import java.util.ArrayList;54import java.util.List;55import com.google.gson.annotations.Ex

Full Screen

Full Screen

GoogleRequest

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.google.pojo.request.GoogleRequest;2import com.galenframework.ocr.google.pojo.request.GoogleRequestBuilder;3import com.galenframework.ocr.google.pojo.request.GoogleRequestBuilder.GoogleRequestBuilderException;4import com.galenframework.ocr.google.pojo.request.GoogleRequestBuilder.GoogleRequestBuilderException.GoogleRequestBuilderExceptionType;5import com.galenframework.ocr.google.pojo.request.GoogleRequests;6import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException;7import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType;8import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType;9import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType;10import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType;11import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeType;12import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeType;13import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeTypeType;14import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeTypeTypeType.GoogleRequestsExceptionTypeTypeTypeTypeTypeTypeTypeType;15import com.galenframework.ocr.google.pojo.request.GoogleRequests.GoogleRequestsException.GoogleRequestsExceptionType.GoogleRequestsExceptionTypeType.GoogleRequestsExceptionTypeTypeType.GoogleRequestsExceptionTypeTypeTypeType.GoogleRequestsExceptionTypeTypeType

Full Screen

Full Screen

GoogleRequest

Using AI Code Generation

copy

Full Screen

1package com.galenframework.ocr.google.pojo.request;2import com.fasterxml.jackson.annotation.JsonProperty;3public class GoogleRequest {4 private String image;5 private String[] features;6 private String imageContext;7 @JsonProperty("image")8 public String getImage() { return image; }9 @JsonProperty("image")10 public void setImage(String value) { this.image = value; }11 @JsonProperty("features")12 public String[] getFeatures() { return features; }13 @JsonProperty("features")14 public void setFeatures(String[] value) { this.features = value; }15 @JsonProperty("imageContext")16 public String getImageContext() { return imageContext; }17 @JsonProperty("imageContext")18 public void setImageContext(String value) { this.imageContext = value; }19}20package com.galenframework.ocr.google.pojo.response;21import com.fasterxml.jackson.annotation.JsonProperty;22public class GoogleResponse {23 private String[] textAnnotations;24 @JsonProperty("textAnnotations")25 public String[] getTextAnnotations() { return textAnnotations; }26 @JsonProperty("textAnnotations")27 public void setTextAnnotations(String[] value) { this.textAnnotations = value; }28}29package com.galenframework.ocr.google.pojo.response;30import com.fasterxml.jackson.annotation.JsonProperty;31public class GoogleResponse {32 private String[] textAnnotations;33 @JsonProperty("textAnnotations")34 public String[] getTextAnnotations() { return textAnnotations; }35 @JsonProperty("textAnnotations")36 public void setTextAnnotations(String[] value) { this.textAnnotations = value; }37}38package com.galenframework.ocr.google.pojo.response;39import com.fasterxml.jackson.annotation.JsonProperty;40public class GoogleResponse {41 private String[] textAnnotations;42 @JsonProperty("textAnnotations")43 public String[] getTextAnnotations() { return textAnnotations; }44 @JsonProperty("textAnnotations")45 public void setTextAnnotations(String[] value) { this.textAnnotations = value; }46}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Galen automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in GoogleRequest

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful