旋转位图图像



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

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);
如果要

将位图旋转 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);

最新更新