Best Galen code snippet using com.galenframework.ocr.google.pojo.request.Image
Source: GoogleVisionOcrService.java
...13* See the License for the specific language governing permissions and14* limitations under the License.15******************************************************************************/16package com.galenframework.ocr;17import java.awt.image.BufferedImage;18import java.awt.image.RenderedImage;19import java.io.ByteArrayOutputStream;20import java.io.IOException;21import java.io.UncheckedIOException;22import java.nio.charset.Charset;23import java.util.ArrayList;24import java.util.Base64;25import java.util.List;26import javax.imageio.ImageIO;27import com.fasterxml.jackson.databind.JsonNode;28import com.fasterxml.jackson.databind.ObjectMapper;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;111 }112 throw new IOException("Response " + status + ". " + message);113 }114 }115 private static HttpResponse post(String url, Object requestObject) throws IOException {116 String json = objectMapper.writeValueAsString(requestObject);117 HttpPost httpPost = new HttpPost(url);118 httpPost.setEntity(new StringEntity(json));119 httpPost.setHeader("Accept", "application/json");120 httpPost.setHeader("Content-type", "application/json");121 return httpClient.execute(httpPost);122 }123 public static String imgToBase64String(final RenderedImage img, final String formatName) {124 final ByteArrayOutputStream os = new ByteArrayOutputStream();125 try {126 ImageIO.write(img, formatName, os);127 return Base64.getEncoder().encodeToString(os.toByteArray());128 } catch (final IOException ioe) {129 throw new UncheckedIOException(ioe);130 }131 }132}...
Source: Request.java
...19import com.google.gson.annotations.SerializedName;20public class Request {21 @SerializedName("image")22 @Expose23 private Image image;24 @SerializedName("features")25 @Expose26 private List<Feature> features = null;27 public Image getImage() {28 return image;29 }30 public void setImage(Image image) {31 this.image = image;32 }33 public List<Feature> getFeatures() {34 return features;35 }36 public void setFeatures(List<Feature> features) {37 this.features = features;38 }39}...
Source: Image.java
...15******************************************************************************/16package com.galenframework.ocr.google.pojo.request;17import com.google.gson.annotations.Expose;18import com.google.gson.annotations.SerializedName;19public class Image {20 @SerializedName("content")21 @Expose22 private String content;23 public String getContent() {24 return content;25 }26 public void setContent(String content) {27 this.content = content;28 }29}...
Image
Using AI Code Generation
1import com.galenframework.ocr.google.pojo.request.Image;2import com.galenframework.ocr.google.pojo.request.Feature;3import com.galenframework.ocr.google.pojo.request.Request;4import com.galenframework.ocr.google.pojo.request.OcrRequest;5import com.galenframework.ocr.google.pojo.response.OcrResponse;6import com.galenframework.ocr.google.pojo.response.TextAnnotation;7import com.galenframework.ocr.google.pojo.response.Vertex;8import com.galenframework.ocr.google.pojo.response.BoundingPoly;9import com.galenframework.ocr.google.pojo.response.Page;10import com.galenframework.ocr.google.pojo.response.Word;11import com.galenframework.ocr.google.pojo.response.Symbol;12import com.galenframework.ocr.google.pojo.response.Paragraph;13import com.galenframework.ocr.google.pojo.response.Block;14import com.galenframework.ocr.google.pojo.response.Line;15import com.galenframework.ocr.google.pojo.response.Property;16import com.galenframework.ocr.google.pojo.response.DetectedLanguage
Image
Using AI Code Generation
1import com.galenframework.ocr.google.pojo.request.Image;2import com.galenframework.ocr.google.pojo.request.OcrRequest;3import com.galenframework.ocr.google.pojo.response.OcrResponse;4import com.galenframework.ocr.google.service.OcrService;5import com.galenframework.ocr.google.service.OcrServiceException;6public class Main {7 public static void main(String[] args) throws OcrServiceException, IOException {8 OcrService ocrService = new OcrService();9 Image image = new Image();10 image.setSource(new FileInputStream("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg"));11 OcrRequest ocrRequest = new OcrRequest();12 ocrRequest.setImage(image);13 ocrRequest.setLanguage("en");14 ocrRequest.setType("DOCUMENT_TEXT_DETECTION");15 OcrResponse ocrResponse = ocrService.ocr(ocrRequest);16 System.out.println(ocrResponse);17 }18}19{20 {21 "fullTextAnnotation": {22 {23 {24 {25 {26 {27 }28 }29 }30 },31 {32 {33 {34 {35 }36 }37 }38 },39 {40 {41 {
Image
Using AI Code Generation
1package com.galenframework.ocr.google.pojo.request;2public class Image {3private String content;4private String source;5public String getContent() {6return content;7}8public void setContent(String content) {9this.content = content;10}11public String getSource() {12return source;13}14public void setSource(String source) {15this.source = source;16}17}18package com.galenframework.ocr.google.pojo.request;19public class Image {20private String content;21private String source;22public String getContent() {23return content;24}25public void setContent(String content) {26this.content = content;27}28public String getSource() {29return source;30}31public void setSource(String source) {32this.source = source;33}34}35package com.galenframework.ocr.google.pojo.request;36public class Image {37private String content;38private String source;39public String getContent() {40return content;41}42public void setContent(String content) {43this.content = content;44}45public String getSource() {46return source;47}48public void setSource(String source) {49this.source = source;50}51}52package com.galenframework.ocr.google.pojo.request;53public class Image {54private String content;55private String source;56public String getContent() {57return content;58}59public void setContent(String content) {60this.content = content;61}62public String getSource() {63return source;64}65public void setSource(String source) {66this.source = source;67}68}69package com.galenframework.ocr.google.pojo.request;70public class Image {71private String content;72private String source;73public String getContent() {74return content;75}76public void setContent(String content) {77this.content = content;78}79public String getSource() {80return source;81}82public void setSource(String source) {83this.source = source;84}85}86package com.galenframework.ocr.google.pojo.request;87public class Image {88private String content;89private String source;90public String getContent() {
Image
Using AI Code Generation
1package com.galenframework.ocr.google.pojo.request;2public class Image {3 private String content;4 public Image(String content) {5 this.content = content;6 }7 public String getContent() {8 return content;9 }10 public void setContent(String content) {11 this.content = content;12 }13}14package com.galenframework.ocr.google.pojo.request;15public class Feature {16 private String type;17 private int maxResults;18 public Feature(String type, int maxResults) {19 this.type = type;20 this.maxResults = maxResults;21 }22 public String getType() {23 return type;24 }25 public void setType(String type) {26 this.type = type;27 }28 public int getMaxResults() {29 return maxResults;30 }31 public void setMaxResults(int maxResults) {32 this.maxResults = maxResults;33 }34}35package com.galenframework.ocr.google.pojo.request;36import java.util.List;37public class Request {38 private List<Feature> features;39 private Image image;40 public Request(List<Feature> features, Image image) {41 this.features = features;42 this.image = image;43 }44 public List<Feature> getFeatures() {45 return features;46 }47 public void setFeatures(List<Feature> features) {48 this.features = features;49 }50 public Image getImage() {51 return image;52 }53 public void setImage(Image image) {54 this.image = image;55 }56}57package com.galenframework.ocr.google.pojo.request;58import java.util.List;59public class GoogleCloudVisionRequest {60 private List<Request> requests;61 public GoogleCloudVisionRequest(List<Request> requests) {62 this.requests = requests;63 }64 public List<Request> getRequests() {65 return requests;66 }67 public void setRequests(List<Request> requests) {68 this.requests = requests;69 }70}
Image
Using AI Code Generation
1package com.galenframework.ocr.google.pojo.request;2import com.fasterxml.jackson.annotation.JsonProperty;3public class Image {4@JsonProperty("content")5private String content;6public String getContent() {7return content;8}9public void setContent(String content) {10this.content = content;11}12}13package com.galenframework.ocr.google.pojo.request;14import com.fasterxml.jackson.annotation.JsonProperty;15public class Feature {16@JsonProperty("type")17private String type;18@JsonProperty("maxResults")19private int maxResults;20public String getType() {21return type;22}23public void setType(String type) {24this.type = type;25}26public int getMaxResults() {27return maxResults;28}29public void setMaxResults(int maxResults) {30this.maxResults = maxResults;31}32}33package com.galenframework.ocr.google.pojo.request;34import java.util.List;35import com.fasterxml.jackson.annotation.JsonProperty;36public class Request {37@JsonProperty("image")38private Image image;39@JsonProperty("features")40private List<Feature> features;41@JsonProperty("imageContext")42private ImageContext imageContext;43public Image getImage() {44return image;45}46public void setImage(Image image) {47this.image = image;48}49public List<Feature> getFeatures() {50return features;51}52public void setFeatures(List<Feature> features) {53this.features = features;54}55public ImageContext getImageContext() {56return imageContext;57}58public void setImageContext(ImageContext imageContext) {59this.imageContext = imageContext;60}61}62package com.galenframework.ocr.google.pojo.request;63import java.util.List;64import com.fasterxml.jackson.annotation.JsonProperty;65public class ImageContext {66@JsonProperty("languageHints")67private List<String> languageHints;68public List<String> getLanguageHints() {69return languageHints;70}71public void setLanguageHints(List<String> languageHints) {72this.languageHints = languageHints;73}
Check out the latest blogs from LambdaTest on this topic:
Automation frameworks enable automation testers by simplifying the test development and execution activities. A typical automation framework provides an environment for executing test plans and generating repeatable output. They are specialized tools that assist you in your everyday test automation tasks. Whether it is a test runner, an action recording tool, or a web testing tool, it is there to remove all the hard work from building test scripts and leave you with more time to do quality checks. Test Automation is a proven, cost-effective approach to improving software development. Therefore, choosing the best test automation framework can prove crucial to your test results and QA timeframes.
The web paradigm has changed considerably over the last few years. Web 2.0, a term coined way back in 1999, was one of the pivotal moments in the history of the Internet. UGC (User Generated Content), ease of use, and interoperability for the end-users were the key pillars of Web 2.0. Consumers who were only consuming content up till now started creating different forms of content (e.g., text, audio, video, etc.).
Mobile devices and mobile applications – both are booming in the world today. The idea of having the power of a computer in your pocket is revolutionary. As per Statista, mobile accounts for more than half of the web traffic worldwide. Mobile devices (excluding tablets) contributed to 54.4 percent of global website traffic in the fourth quarter of 2021, increasing consistently over the past couple of years.
JUnit is one of the most popular unit testing frameworks in the Java ecosystem. The JUnit 5 version (also known as Jupiter) contains many exciting innovations, including support for new features in Java 8 and above. However, many developers still prefer to use the JUnit 4 framework since certain features like parallel execution with JUnit 5 are still in the experimental phase.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!