Exemplo n.º 1
0
  /**
   * Capture the current image with the size as it is displayed and retrieve it as Bitmap.
   *
   * @return current output as Bitmap
   * @throws InterruptedException
   */
  public Bitmap capture() throws InterruptedException {
    final Semaphore waiter = new Semaphore(0);

    final int width = mGLSurfaceView.getMeasuredWidth();
    final int height = mGLSurfaceView.getMeasuredHeight();

    // Take picture on OpenGL thread
    final int[] pixelMirroredArray = new int[width * height];
    mGPUImage.runOnGLThread(
        new Runnable() {
          @Override
          public void run() {
            final IntBuffer pixelBuffer = IntBuffer.allocate(width * height);
            GLES20.glReadPixels(
                0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
            int[] pixelArray = pixelBuffer.array();

            // Convert upside down mirror-reversed image to right-side up normal image.
            for (int i = 0; i < height; i++) {
              for (int j = 0; j < width; j++) {
                pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
              }
            }
            waiter.release();
          }
        });
    requestRender();
    waiter.acquire();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
    return bitmap;
  }