如何将 16 位灰度图像写入 jpeg?
我有一个从短裤数组创建的每像素 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你必须先将其转换为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