2012-09-28 3 views

답변

4

이제 매우 쉽습니다. Libgdx는 example을 제공합니다.

작동시키기 위해 하나의 명령문을 추가해야했습니다. 이미지를 직접 /screenshot1.png에 저장할 수 없습니다. 간단히 앞에 Gdx.files.getLocalStoragePath().

소스 코드 : http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/ :

public class ScreenshotFactory { 

    private static int counter = 1; 
    public static void saveScreenshot(){ 
     try{ 
      FileHandle fh; 
      do{ 
       fh = new FileHandle(Gdx.files.getLocalStoragePath() + "screenshot" + counter++ + ".png"); 
      }while (fh.exists()); 
      Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false); 
      PixmapIO.writePNG(fh, pixmap); 
      pixmap.dispose(); 
     }catch (Exception e){   
     } 
    } 

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){ 
     final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h); 

     if (yDown) { 
      // Flip the pixmap upside down 
      ByteBuffer pixels = pixmap.getPixels(); 
      int numBytes = w * h * 4; 
      byte[] lines = new byte[numBytes]; 
      int numBytesPerLine = w * 4; 
      for (int i = 0; i < h; i++) { 
       pixels.position((h - i - 1) * numBytesPerLine); 
       pixels.get(lines, i * numBytesPerLine, numBytesPerLine); 
      } 
      pixels.clear(); 
      pixels.put(lines); 
     } 

     return pixmap; 
    } 
} 
3

나는 여기 libGDX 포럼 회원이 제공하는 최소한의 .PNG 인코더 행운이 있었다 (나는 줄이기 위해 오프라인 pngcrush 사용 http://www.badlogicgames.com/forum/viewtopic.php?p=8358#p8358

주 인코더가 매우 단순한이기 때문에 결과 PNG 파일이 최적화되지 않은 것을 자신의 크기는 극적으로).

또한 알파 채널에 몇 가지 문제가있었습니다. 기본 화면 색상은 화면의 투명한 픽셀을 통해 표시되지만 화면에서 스크랩 된 픽셀에서는 나타나지 않습니다 (따라서 PNG 인코더의 실제 오류는 아닙니다). 배경이 검은 색이면 알파 채널이 픽셀 단위로 1.0인지 확인하십시오 (물론 스크린 샷의 투명도를 원하지 않는 한).

+2

Libgdx 지금 기본적으로 PNG 파일을 저장 지원은'PixmapIO.writePNG' 방법을 참조 gdx/graphics/PixmapIO.html # writePNG (com.badlogic.gdx.files.FileHandle, com.badlogic.gdx.graphics.Pixmap) –

관련 문제