gpt4 book ai didi

java - 查询图像方向

转载 作者:行者123 更新时间:2023-12-01 13:00:49 26 4
gpt4 key购买 nike

我正在使用类似于 Java - get pixel array from image 的代码获取对 BMP 图像像素数据的低级访问,如下所示:

BufferedImage image = ImageIO.read(is);
DataBuffer buffer = image.getRaster().getDataBuffer();
byte[] rawPixels = ((DataBufferByte) buffer).getData();

结果数组从下到上放置(即它的第一个字节是最后一个图像行的开头),考虑到BMP files usually have the same layout,这是有道理的。 .

在这种情况下,我想通过翻转线路来向调用者隐藏这些低级细节。有没有办法可以查询加载的 BufferedImage 的像素方向/布局?

最佳答案

我检查了 Java 7 BMPImageReader 的源代码,它在读取时确实从下到上的顺序转换为自上而下的顺序,正如我所期望的那样。因此,DataBuffer 的后备数组将采用正常的自上而下的顺序。我无法在 Windows 上使用 Oracle Java 7 JRE 重现此行为。

OP 已验证问题确实存在于代码的另一部分,而不是作为问题的一部分发布。

我认为所描述的内容可能是可能的,使用 SampleModel 的特殊子类来转换所有传入的 y 坐标,但没有标准方法来查询方向(所有 Rasters 假定为自上而下)。

无论如何,只是为了好玩,我创建了一些代码来测试它是否可能。下面是一个完全可运行的示例。

public class SampleModelOrientationTest {
public static void main(String[] args) {
BufferedImage image = new BufferedImage(16, 9, BufferedImage.TYPE_3BYTE_BGR);
WritableRaster raster = image.getRaster();
DataBuffer dataBuffer = raster.getDataBuffer();

SampleModel sampleModel = image.getSampleModel();

QueryingDataBuffer queryBuffer = new QueryingDataBuffer(dataBuffer, sampleModel.getWidth(), sampleModel.getNumDataElements());
sampleModel.getDataElements(0, 0, null, queryBuffer);
System.out.println(queryBuffer.getOrientation());

queryBuffer.resetOrientation();
SampleModel bottomUpSampleModel = new BottomUpSampleModel(sampleModel);
bottomUpSampleModel.getDataElements(0, 0, null, queryBuffer);
System.out.println(queryBuffer.getOrientation());
}

private static class QueryingDataBuffer extends DataBuffer {
enum Orientation {
Undefined,
TopDown,
BottomUp,
Unsupported
}

private final int width;
private final int numDataElements;

private Orientation orientation = Orientation.Undefined;

public QueryingDataBuffer(final DataBuffer dataBuffer, final int width, final int numDataElements) {
super(dataBuffer.getDataType(), dataBuffer.getSize());
this.width = width;
this.numDataElements = numDataElements;
}

@Override public int getElem(final int bank, final int i) {
if (bank == 0 && i < numDataElements && isOrientationUndefinedOrEqualTo(Orientation.TopDown)) {
orientation = Orientation.TopDown;
}
else if (bank == 0 && i >= (size - (width * numDataElements) - numDataElements) && isOrientationUndefinedOrEqualTo(Orientation.BottomUp)) {
orientation = Orientation.BottomUp;
}
else {
// TODO: Expand with more options as apropriate
orientation = Orientation.Unsupported;
}

return 0;
}

private boolean isOrientationUndefinedOrEqualTo(final Orientation orientation) {
return this.orientation == Orientation.Undefined || this.orientation == orientation;
}

@Override public void setElem(final int bank, final int i, final int val) {
}

public final void resetOrientation() {
orientation = Orientation.Undefined;
}

public final Orientation getOrientation() {
return orientation;
}
}

// TODO: This has to be generalized to be used for any BufferedImage type.
// I justy happen to know that 3BYTE_BGR uses PixelInterleavedSampleModel and has BGR order.
private static class BottomUpSampleModel extends PixelInterleavedSampleModel {
public BottomUpSampleModel(final SampleModel sampleModel) {
super(sampleModel.getDataType(), sampleModel.getWidth(), sampleModel.getHeight(),
sampleModel.getNumDataElements(), sampleModel.getNumDataElements() * sampleModel.getWidth(),
new int[] {2, 1, 0} // B, G, R
);
}

@Override public Object getDataElements(final int x, final int y, final Object obj, final DataBuffer data) {
return super.getDataElements(x, getHeight() - 1 - y, obj, data);
}

@Override public int getSample(final int x, final int y, final int b, final DataBuffer data) {
return super.getSample(x, getHeight() - 1 - y, b, data);
}
}
}

关于java - 查询图像方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23525626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com