java - 查询图像方向

标签 java bufferedimage javax.imageio bmp

我正在使用类似于 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/

相关文章:

java 提取jpeg图像细节

java - 如何解决 Java awt/swing 图像打印中的滞后问题?

java - ImageIO.read() 返回 403 错误

java - hibernate 实体 setter 中的额外操作

java - 当我尝试在 Spring 中下载 CSV 时获得 CPU 100%

java - 在 Android 中使用 SSL 证书

java - 在 Swing 中的 BufferedImage 中绘画

java - 如何在 android 中使用 autocompleteTextView 执行 Trie 搜索?

java - 为什么 BufferedImage 不能正常工作?!!是因为我用错了吗?

java - 为什么 ImageIO.write() 方法修改像素值?