2012-04-28 93 views
-1

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),颜色看起来不错,但图片本身缩小。

我在这个问题上停滞不前。任何帮助表示赞赏。谢谢。

+0

如果您的问题已经回答了,或者如果它不再有效,请勾选以选择最合适的答案,以便每个人都知道问题已得到解决。谢谢。 – wattostudios 2012-05-14 13:39:06

回答

1

BufferedImage绝对是一个很好的开始。 PixelFormat中的许多值将与BufferedImage中的值匹配 - 它们各自具有24位和32位RGB/ARGB值,均具有5-5-5和5-6-5组合等等。

如果您遇到问题,请发布一些代码,我们会看看它,并尝试提供帮助。我推荐的方法是在字节顺序上(像素为int s),直到获得期望的结果 - 尝试将BufferedImage绘制到JPanel等GUI对象上,以便看到它的外观。

如果你有一个int数组[]为你的像素值,这是代码,我通常用来显示数组为图像...

int[] pixels; 
ColorModel model = new DirectColorModel(32,0x00ff0000,0x0000ff00,0x000000ff,0xff000000); 
Image image = new JLabel().createImage(new MemoryImageSource(width,height,model,pixels,0,width)); 
+0

感谢您的回答。我用我的代码更新了这篇文章。创建缓冲图像后,红色和蓝色的颜色互换 – nixspirit 2012-04-28 11:03:22

+0

我肯定会建议你将'byte []'转换为'int []''的最新方法 - 这是我以前采用的方法,它的效果最好。我添加了通常用于生成图像的代码 - 可能会对您有所帮助。否则,我只是建议你的宽度和高度不太对。 – wattostudios 2012-04-29 03:03:17