1

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);
Benni
  • 969
  • 1
  • 19
  • 29

1 Answers1

2

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);
Alexanus
  • 679
  • 4
  • 22
  • I used it this way and it actually did the job. But it also "reversed" the image like a mirror affect, so things were faced the opposite way. Anything to do about that? @Alexanus – Benni Apr 12 '15 at 21:21
  • The method i posted should not mirror the image. Anyways if you want to mirror a bitmap take a look at this: http://stackoverflow.com/questions/8552298/how-to-mirror-an-image-file-2-2 you can use matrix.preScale(-1.0f, 1.0f) to mirror the image on the x axis, or matrix.preScale(1.0f, -1.0f) to mirror it on the y axis. – Alexanus Apr 13 '15 at 05:54