Benni
Benni

Reputation: 969

Rotate bitmap image

Im taking a screenshot of my app and try to post it on facebook using the facebook SDK. But as the ShareDialog appears with the Image, it´s upside down.. So I need to re-rotate it. This is how I create the 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) {

    }
}

And here I fetch it:

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;
}

Then I try to share the photo:

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);

Upvotes: 1

Views: 326

Answers (1)

Alexanus
Alexanus

Reputation: 689

If you want to rotate a bitmap by 180 degrees you can use this code:

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);

Upvotes: 2

Related Questions