Java 保存自定义缓冲区映像
我用Java 保存自定义缓冲区映像,java,bufferedimage,Java,Bufferedimage,我用short[]表示原始灰度图像像素。我想从中创建buffereImage,并将其保存为PNG。 因为没有为buffereImage定义TYPE\u SHORT\u GRAY,所以我自己这样创建了一个: short[] myRawImageData; // Create signed 16 bit data buffer, and compatible sample model DataBuffer dataBuffer = new DataBufferShort(myRawImageDat
short[]
表示原始灰度图像像素。我想从中创建buffereImage
,并将其保存为PNG。
因为没有为buffereImage
定义TYPE\u SHORT\u GRAY
,所以我自己这样创建了一个:
short[] myRawImageData;
// Create signed 16 bit data buffer, and compatible sample model
DataBuffer dataBuffer = new DataBufferShort(myRawImageData, w * h);
SampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w, h, 1, w, new int[] {0});
// Create a raster from sample model and data buffer
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null);
// Create a 16 bit signed gray color model
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
// Finally create the signed 16 bit image
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
try (FileOutputStream fos = new FileOutputStream("/tmp/tst.png")) {
ImageIO.write(image, "png", fos);// <--- Here goes the exception
} catch (Exception ex) {
ex.printStackTrace();
}
short[]myRawImageData;
//创建签名16位数据缓冲区和兼容的示例模型
DataBuffer DataBuffer=新的DataBufferShort(myRawImageData,w*h);
SampleModel SampleModel=新组件SampleModel(DataBuffer.TYPE_SHORT,w,h,1,w,new int[]{0});
//从样例模型和数据缓冲区创建光栅
WritableRaster raster=raster.createWritableRaster(sampleModel,dataBuffer,null);
//创建16位有符号的灰色模型
ColorSpace ColorSpace=ColorSpace.getInstance(ColorSpace.CS\u GRAY);
ColorModel ColorModel=新组件ColorModel(colorSpace、false、false、Transparency.不透明、DataBuffer.TYPE_SHORT);
//最后创建签名的16位图像
BuffereImage image=新的BuffereImage(colorModel、光栅、colorModel.isAlphaPremultiplied(),null);
try(FileOutputStream fos=newfileoutputstream(“/tmp/tst.png”)){
ImageIO.write(image,“png”,fos);//你的代码对我来说很好,我得到你的错误的唯一途径是我更改了带偏移量。你能给我们更多的代码吗
编辑
如果数据集中有负面数据,则可能应该使用ushort而不是short
int h = 64, w = 64;
short[] myRawImageData = new short[4096];
for (int i = 0; i < 4096; i++){
//this rolls over into negative numbers
myRawImageData[i] = (short) (i * 14);
}
// Create signed 16 bit data buffer, and compatible sample model
DataBuffer dataBuffer = new DataBufferUShort(myRawImageData, w * h);
SampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_USHORT, w, h, 1, w, new int[] {0});
// Create a raster from sample model and data buffer
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null);
// Create a 16 bit signed gray color model
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
// Finally create the signed 16 bit image
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
try (FileOutputStream fos = new FileOutputStream("/tmp/tst.png")) {
ImageIO.write(image, "png", fos);// <--- Here goes the exception
} catch (Exception ex) {
ex.printStackTrace();
}
inth=64,w=64;
short[]myRawImageData=新的short[4096];
对于(int i=0;i<4096;i++){
//这会变成负数
myRawImageData[i]=(短)(i*14);
}
//创建签名16位数据缓冲区和兼容的示例模型
DataBuffer DataBuffer=新的DataBufferUShort(myRawImageData,w*h);
SampleModel SampleModel=新组件SampleModel(DataBuffer.TYPE_USHORT,w,h,1,w,newint[]{0});
//从样例模型和数据缓冲区创建光栅
WritableRaster raster=raster.createWritableRaster(sampleModel,dataBuffer,null);
//创建16位有符号的灰色模型
ColorSpace ColorSpace=ColorSpace.getInstance(ColorSpace.CS\u GRAY);
ColorModel ColorModel=新组件ColorModel(colorSpace、false、false、Transparency.不透明、DataBuffer.TYPE_USHORT);
//最后创建签名的16位图像
BuffereImage image=新的BuffereImage(colorModel、光栅、colorModel.isAlphaPremultiplied(),null);
try(FileOutputStream fos=newfileoutputstream(“/tmp/tst.png”)){
图像IO.write(图像“png”,fos);//你应该a)将你的问题作为一个问题来说明,b)描述发生了什么,你期望发生什么,它们之间的区别以及你遇到的具体错误。对我来说仍然很有用,也许你的输入数据就是问题所在。我的输入数据包含负数。你的数据也包含负数吗?也许你应该把它当作usho一个负值并不能真正映射到一种颜色,如果你想要最后一点数据,请让程序知道。因此DataBuffer.TYPE_USHORT
可能是你的答案。我已经结束了将值从short
移动到USHORT
的过程,方法是添加2^15,然后用TYPE_USHORT\u GRAY
TYPE创建缓冲图像。现在一切正常