【发布时间】:2011-05-18 14:59:37
【问题描述】:
我需要在 Java 中翻转其行倒置的原始图像。倒置我的意思是,图像的第一行存储在文件的末尾。
我设法通过使用辅助缓冲区重新排序图像行来实现我想要的。我在下面包含了我的代码。
我认为这可以通过转换坐标来优化,避免内存复制。我试图实现一个可以反转行的 DataBuffer,但我使用的栅格需要一个 DataBufferByte(最终类)。
有谁知道一种更优化的方式来做我想做的事?
谢谢
...
int w = 640;
int h = 480;
byte[] flippedData = new byte[640*480*4];
int scanLineLength = w*4;
for(int i=0;i!=h; ++i) {
System.arraycopy(originalData, scanLineLength*i, flippedData, scanLineLength*(h-i)-scanLineLength, scanLineLength);
}
DataBuffer db = new DataBufferByte(flippedData,flippedData.length);
WritableRaster raster = Raster.createInterleavedRaster(db, w, h, scanLineLength, 4, new int[]{2,1,0}, new Point(0,0));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
BufferedImage img = new BufferedImage(cm, raster, false, null);
ImageIO.write(img, "JPEG", new File("out.jpg"));
【问题讨论】:
-
您对像素编码和 ColorSpace 进行硬编码的原因是什么?如果您执行 GraphicsEnvironment.getLocalGraphicsEnvironment()。 getDefaultScreenDevice().getDefaultConfiguration()。 createCompatibleImage(...) 您将拥有最适合您的程序运行平台的像素编码。
-
我是你的代码,你将 DataBuffer 变量传递给 Raster,而不是 DataBufferByte。
-
@Webinator:因为我不想在屏幕上显示图像
-
@codymanix:不。我正在传递一个 DataBufferByte
标签: java graphics image-processing awt