Java中與C#PixelFormat's成員匹配的內容。.Net PixelFormat有Java相當於?
即Format24bppRgb與BufferedImage.TYPE_INT_RGB匹配嗎?
這是我的代碼。我得到了它有淨的的PixelFormat = Format32bppArgb 我創建的BufferedImage這樣一個形象:
int sizeBytes = width * height;
DataBufferByte dataBuffer = new DataBufferByte(myImageBytes, sizeBytes);
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, // dataBuffer
width, // width
height, // height
width * 4, // scanlineStride
4, // pixelStride
new int[]{0, 1, 2, 3}, // bandOffsets
null); // location
java.awt.image.ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), // ColorSpace
new int[]{8, 8, 8, 8}, // bits
true, // hasAlpha
false, // isPreMultiplied
ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
BufferedImage result = new BufferedImage(colorModel, raster, false, null);
後,我創建一個BufferedImage,紅色和藍色在它交換。
接下來,我試圖創建一個圖片作爲跟隨
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
WritableRaster r = result.getRaster();
int[] pixels = byteToInt(bytes);
r.setPixels(0, 0, width, height , pixels); // ! Here an exception occures, because after I converted the byte array to int one the width becomes too long.
字節數組用這種方法
private int[] byteToInt(byte[] pixels) {
int[] ints = new int[pixels.length/3];
int byteIdx = 0;
for (int pixel = 0; pixel < ints.length; pixel++) {
int red = (int) pixels[byteIdx++] & 0xFF;
int green = (int) pixels[byteIdx++] & 0xFF;
int blue = (int) pixels[byteIdx++] & 0xFF;
int rgb = (red << 16) | (green << 8) | blue;
ints[pixel] = rgb;
}
return ints;
}
的顏色現在看起來不錯轉換,但我得到異常
java.lang.ArrayIndexOutOfBoundsException: 27600
at sun.awt.image.ByteInterleavedRaster.setPixels(ByteInterleavedRaster.java:1106)
如果我使用較小的寬度(例如寬度/ 3),顏色看起來不錯,但圖片本身縮小。
我在這個問題上停滯不前。任何幫助表示讚賞。謝謝。
如果您的問題已經回答了,或者如果它不再有效,請勾選以選擇最合適的答案,以便每個人都知道問題已得到解決。謝謝。 – wattostudios 2012-05-14 13:39:06