[英]Flip/Mirror the contents of ByteBuffer (Image Buffer) horizontally/vertically
我將圖像存儲在 Java ByteBuffer 對象中。 我正在使用這樣的基本偏移函數獲取每個坐標:
public static int offset(int x, int y, int z, int width, int height) {
return (z * height + y) * width + x;
}
你可以得到這樣的顏色:
int r = imageBuffer.get(offset + 0) & 0xFF; //0xFF is the hexadecimal way of writing 255, turns this byte into an unsigned byte
int g = imageBuffer.get(offset + 1) & 0xFF;
int b = imageBuffer.get(offset + 2) & 0xFF;
int a = imageBuffer.get(offset + 3) & 0xFF;
我想水平/垂直翻轉 ByteBuffer 中的圖像,但我的大多數搜索都會對flip()
查詢。 我想知道這是否可以僅使用 ByteBuffer 來實現,或者我是否將不得不提出更復雜的解決方案? 在我的處理結束時,它仍然需要是一個 ByteBuffer。
以下是我掌握的信息:
private ByteBuffer imageBuffer;
private int imageWidth, imageHeight;
任何幫助表示贊賞! 感謝您的時間!
不用擔心! 我自己設法做到了。 只需制作一個字節數組副本,然后用它寫入一個新的字節緩沖區即可獲得所需的結果。
public void mirror(boolean flipHorizontally, boolean flipVertically) {
byte[] pixels = new byte[imageBuffer.capacity()];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int offset = NNGINEUtil.offset(x, y, width);
int flipX = (flipHorizontally ? (width - 1) - x : x);
int flipY = (flipVertically ? (height - 1) - y: y);
int invOffset = NNGINEUtil.offset(flipX, flipY, width);
byte r = imageBuffer.get(invOffset + 0);
byte g = imageBuffer.get(invOffset + 1);
byte b = imageBuffer.get(invOffset + 2);
byte a = imageBuffer.get(invOffset + 3);
pixels[offset + 0] = r;
pixels[offset + 1] = g;
pixels[offset + 2] = b;
pixels[offset + 3] = a;
}
}
imageBuffer = ByteBuffer.wrap(pixels);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.