如何将 16 位灰度图像写入 jpeg?

发布于 2024-12-28 14:53:03 字数 1477 浏览 1 评论 0原文

我有一个从短裤数组创建的每像素 16 位灰度 BufferedImage

 private BufferedImage get16bitImage(short[] pixels) {
    ColorModel colorModel = new ComponentColorModel(
            ColorSpace.getInstance(ColorSpace.CS_GRAY),
            new int[]{16},
            false,
            false,
            Transparency.OPAQUE,
            DataBuffer.TYPE_USHORT);
    DataBufferUShort db = new DataBufferUShort(pixels, pixels.length);
    WritableRaster raster = Raster.createInterleavedRaster(
            db,
            imgD.width,
            imgD.height,
            imgD.width,
            1,
            new int[1],
            null);
    return new BufferedImage(colorModel, raster, false, null);
}

尝试保存它时:

  ImageIO.write(img, "PNG", new File(resultImgNamePNG)); // works fine
  ImageIO.write(img, "BMP", new File(resultImgNameBMP)); // doesn't work, returns false
  ImageIO.write(img, "JPEG", new File(resultImgNameJPEG)); // doesnt work, returns false

我尝试使用 JAI:

public void writeImageToJPEG(File out, BufferedImage image, float quality) throws IOException {
    JPEGEncodeParam param = new JPEGEncodeParam();
    param.setQuality(quality);
    ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", new FileOutputStream(out), param);

    encoder.encode(image);
}

encoder.encode(image) throws java.lang.RuntimeException:只能写入 1 或 3 段字节数据。

I have 16-bit per pixel grayscale BufferedImage created from an array of shorts:

 private BufferedImage get16bitImage(short[] pixels) {
    ColorModel colorModel = new ComponentColorModel(
            ColorSpace.getInstance(ColorSpace.CS_GRAY),
            new int[]{16},
            false,
            false,
            Transparency.OPAQUE,
            DataBuffer.TYPE_USHORT);
    DataBufferUShort db = new DataBufferUShort(pixels, pixels.length);
    WritableRaster raster = Raster.createInterleavedRaster(
            db,
            imgD.width,
            imgD.height,
            imgD.width,
            1,
            new int[1],
            null);
    return new BufferedImage(colorModel, raster, false, null);
}

When trying to save it:

  ImageIO.write(img, "PNG", new File(resultImgNamePNG)); // works fine
  ImageIO.write(img, "BMP", new File(resultImgNameBMP)); // doesn't work, returns false
  ImageIO.write(img, "JPEG", new File(resultImgNameJPEG)); // doesnt work, returns false

I tried using JAI:

public void writeImageToJPEG(File out, BufferedImage image, float quality) throws IOException {
    JPEGEncodeParam param = new JPEGEncodeParam();
    param.setQuality(quality);
    ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", new FileOutputStream(out), param);

    encoder.encode(image);
}

encoder.encode(image) throws java.lang.RuntimeException: Only 1, or 3-band byte data may be written.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2025-01-04 14:53:03

我认为你必须先将其转换为8位。如果这是用于显示目的,java无论如何都会在显示之前将其转换为8位。

您可以做一些我有时看到的事情,实际上可以改善显示的图像,即对值进行非线性缩放(例如使用对数刻度),这样的细节当然取决于您生成的图像。

有关此类效果的更多信息,请参见: http://www.java.net/external?url=http://www.cs.unm.edu/~brayer/vision/perception.html

I think you have to convert it to 8-bit first. If this is used for display purposes, java converts it to 8-bit bit before display anyways.

You can do something that I've seen sometimes actually improve the displayed image which is doing non-linear scaling of the values (using a log scale for example) such detail depends on the image you are generating ofcourse.

More on such effect here: http://www.java.net/external?url=http://www.cs.unm.edu/~brayer/vision/perception.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文