Esempio n. 1
0
  public void saveFrametoPNG(String filename) {
    final int frameWidth = _canvas.getWidth();
    final int frameHeight = _canvas.getHeight();
    final ByteBuffer pixelsRGB = Direct.newByteBuffer(frameWidth * frameHeight * 3);
    _canvas.runWithContext(
        new Runnable() {
          public void run() {
            // glPushAttrib(GL_PIXEL_MODE_BIT);
            glReadBuffer(GL_BACK);
            glPixelStorei(GL_PACK_ALIGNMENT, 1);
            glReadPixels(0, 0, frameWidth, frameHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelsRGB);
            // glPopAttrib();
          }
        });
    int[] pixelInts = new int[frameWidth * frameHeight];
    int p = frameWidth * frameHeight * 3;
    int q; // Index into ByteBuffer
    int i = 0; // Index into target int[]
    int w3 = frameWidth * 3; // Number of bytes in each row
    for (int row = 0; row < frameHeight; row++) {
      p -= w3;
      q = p;
      for (int col = 0; col < frameWidth; col++) {
        int iR = pixelsRGB.get(q++);
        int iG = pixelsRGB.get(q++);
        int iB = pixelsRGB.get(q++);
        pixelInts[i++] =
            0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF);
      }
    }

    // Create a new BufferedImage from the pixeldata.
    BufferedImage bufferedImage =
        new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, frameWidth, frameHeight, pixelInts, 0, frameWidth);

    try {
      javax.imageio.ImageIO.write(bufferedImage, "PNG", new File(filename));
    } catch (IOException e) {
      System.out.println("Error: ImageIO.write.");
      e.printStackTrace();
    }

    /* End code taken from: http://www.felixgers.de/teaching/jogl/imagingProg.html */

    /*
     final BufferedImage image = new BufferedImage(
     this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
     Graphics gr = image.getGraphics();
     this.printAll(gr);
     gr.dispose();
    try {
    	ImageIO.write(image, "PNG", new File(filename));
      } catch (IOException e) {
           System.out.println( "Error: ImageIO.write." );
           e.printStackTrace();
      }
    */
  }
Esempio n. 2
0
  /** Do screen capture and save it as image */
  private static void captureScreenAndSave() {

    try {
      Robot robot = new Robot();
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
      System.out.println("About to screen capture - " + rectangle);
      java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle);
      javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg"));
      robot.delay(3000);
    } catch (Throwable t) {
      System.out.println("WARNING: Exception thrown while screen capture!");
      t.printStackTrace();
    }
  }