2015-04-12 213 views
0

我正在截取我的應用程序的截圖並嘗試使用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

如果你想旋轉位圖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); 
+0

我以這種方式使用它,它確實做了這項工作。但它也像鏡子一樣「顛倒」了影像,所以事情正好相反。任何事情要做? @Alexanus – Benni

+0

我發佈的方法不應該鏡像映像。無論如何,如果你想鏡像一個位圖看看這個:http://stackoverflow.com/questions/8552298/how-to-mirror-an-image-file-2-2你可以使用matrix.preScale(-1.0 f,1.0f)在x軸上鏡像圖像,或matrix.preScale(1.0f,-1.0f)在y軸上鏡像。 – Alexanus