Java以浮点精度创建BufferedImage [英] Java create BufferedImage with float precision

查看:78
本文介绍了Java以浮点精度创建BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java创建了一个地图编辑器.问题是,我对每个字节值都有步骤,因此映射不平滑.是否可以将BufferedImage栅格数据更改为浮动数据并在其上绘制浮动精度?

I created a map editor in Java. The problem is, I have steps for every byte value, so the map isn't smooth. Is it possible to change the BufferedImage raster data to float data and draw in float precision on it?

推荐答案

要回答您的问题,是的,您可以创建具有浮点精度的 BufferedImage .但是,尚不清楚这是否可以帮助您解决问题.

To answer your question, yes, you can create a BufferedImage with float precision. It is however a little unclear if this will help you solve your problem.

无论如何,以下是用于创建精度为 float BufferedImage 的工作示例代码:

In any case, here's working example code for creating a BufferedImage with float precision:

public class FloatImage {
    public static void main(String[] args) {
        // Define dimensions and layout of the image
        int w = 300;
        int h = 200;
        int bands = 4; // 4 bands for ARGB, 3 for RGB etc
        int[] bandOffsets = {0, 1, 2, 3}; // length == bands, 0 == R, 1 == G, 2 == B and 3 == A

        // Create a TYPE_FLOAT sample model (specifying how the pixels are stored)
        SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, w, h, bands, w  * bands, bandOffsets);
        // ...and data buffer (where the pixels are stored)
        DataBuffer buffer = new DataBufferFloat(w * h * bands);

        // Wrap it in a writable raster
        WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);

        // Create a color model compatible with this sample model/raster (TYPE_FLOAT)
        // Note that the number of bands must equal the number of color components in the 
        // color space (3 for RGB) + 1 extra band if the color model contains alpha 
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel colorModel = new ComponentColorModel(colorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT);

        // And finally create an image with this raster
        BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);

        System.out.println("image = " + image);
    }
}

对于地图高程数据,使用单个波段( bands = 1; bandOffsets = {0}; )和灰度颜色空间( ColorSpace.CS_GRAY ),并且不使用透明度可能更有意义.

For map elevation data, using a single band (bands = 1; bandOffsets = {0};) and a grayscale color space (ColorSpace.CS_GRAY) and no transparency may make more sense.

这篇关于Java以浮点精度创建BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆