public byte[] takeScreenshot() throws AndroidDeviceException {
    if (device == null) {
      throw new AndroidDeviceException("Device not accessible via ddmlib.");
    }
    RawImage rawImage;
    try {
      rawImage = device.getScreenshot();
    } catch (IOException ioe) {
      throw new AndroidDeviceException("Unable to get frame buffer: " + ioe.getMessage());
    } catch (TimeoutException e) {
      log.log(Level.SEVERE, e.getMessage(), e);
      throw new AndroidDeviceException(e.getMessage());
    } catch (AdbCommandRejectedException e) {
      log.log(Level.SEVERE, e.getMessage(), e);
      throw new AndroidDeviceException(e.getMessage());
    }

    // device/adb not available?
    if (rawImage == null) return null;

    BufferedImage image =
        new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_3BYTE_BGR);

    int index = 0;
    int IndexInc = rawImage.bpp >> 3;
    for (int y = 0; y < rawImage.height; y++) {
      for (int x = 0; x < rawImage.width; x++) {
        image.setRGB(x, y, rawImage.getARGB(index));
        index += IndexInc;
      }
    }
    return toByteArray(image);
  }
Constructor mean to be used with Android Hardware devices because a reference to the
{@link IDevice} will be available immediately after they are connected.

@param device
selendroid-standalone/src/main/java/io/selendroid/standalone/android/impl/AbstractDevice.java