旋转位图图像

时间:2015-04-12 07:19:57

标签: android rotation libgdx

我拍摄了我的应用程序的屏幕截图,并尝试使用facebook SDK在Facebook上发布。但是当ShareDialog与Image一起出现时,它是颠倒的......所以我需要重新旋转它。 这就是我创建图像的方式:

private void saveScreenshot() {
    try{
        FileHandle fh;
        do{
            fh = new FileHandle(Gdx.files.getLocalStoragePath() + "stoneIMG" + counter++ + ".png");
        }while(fh.exists());
        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
        System.out.println(fh.toString());


    }catch(Exception e) {

    }
}

在这里我拿到它:

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

    if(yDown) {
        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;
}

然后我尝试分享照片:

public void sharePhoto() {
    String filePath = Gdx.files.getLocalStoragePath() + "stoneIMG" + counter + ".png";
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

1 个答案:

答案 0 :(得分:2)

如果要将位图旋转180度,可以使用以下代码:

Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

Matrix matrix = new Matrix();
matrix.postRotate(180);

Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);