github.com/bazelbuild/rules_webtesting@v0.2.0/java/com/google/testing/web/screenshotter/Screenshot.java (about)

     1  // Copyright 2017 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // //////////////////////////////////////////////////////////////////////////////
    16  //
    17  package com.google.testing.web.screenshotter;
    18  
    19  import com.google.errorprone.annotations.Immutable;
    20  import java.awt.image.BufferedImage;
    21  import java.io.ByteArrayInputStream;
    22  import java.io.IOException;
    23  import java.util.Base64;
    24  import javax.imageio.ImageIO;
    25  import javax.imageio.ImageReader;
    26  import org.json.JSONException;
    27  import org.json.JSONObject;
    28  
    29  /** A wrapper around a screenshot returned by Screenshotter. */
    30  @Immutable
    31  public final class Screenshot {
    32    private static final String FORMAT = "png";
    33  
    34    private final String base64;
    35  
    36    Screenshot(JSONObject response) throws JSONException {
    37      this.base64 = response.getString("value");
    38    }
    39  
    40    /** Returns the BASE 64 string returned by the screenshot endpoint. */
    41    public String asBase64() {
    42      return base64;
    43    }
    44  
    45    /**
    46     * Returns the decoded bytes for the screenshot. Every call to this returns a new copy of the
    47     * decoded bytes.
    48     */
    49    public byte[] asBytes() {
    50      return Base64.getDecoder().decode(base64);
    51    }
    52  
    53    /**
    54     * Returns a BufferedImage of the screenshot. Every call to this returns a new copy of the image.
    55     */
    56    public BufferedImage asImage() throws IOException {
    57      ImageReader imageReader = ImageIO.getImageReadersByFormatName(FORMAT).next();
    58      imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(asBytes())), true);
    59      return imageReader.read(0);
    60    }
    61  
    62    @Override
    63    public boolean equals(Object other) {
    64      if (other == null) {
    65        return false;
    66      }
    67      if (!(other instanceof Screenshot)) {
    68        return false;
    69      }
    70      Screenshot o = (Screenshot) other;
    71      return asBase64().equals(o.asBase64());
    72    }
    73  
    74    @Override
    75    public int hashCode() {
    76      return asBase64().hashCode();
    77    }
    78  }