下面列出了java.awt.image.DataBufferUShort#getData() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void WritePNGfile(String imagename, short[] linear, int width, int height) {
BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB);
DataBufferUShort sh = (DataBufferUShort) buf.getRaster().getDataBuffer();
short[] shd = sh.getData();
System.arraycopy(linear, 0, shd, 0, Math.min(linear.length, shd.length));
try {
ImageIO.write(buf, "PNG", new File(imagename));
} catch (IOException e) {
e.printStackTrace();
}
}
private Object extractImageData(BufferedImage img){
DataBuffer buf = img.getRaster().getDataBuffer();
switch (buf.getDataType()){
case DataBuffer.TYPE_BYTE:
DataBufferByte byteBuf = (DataBufferByte) buf;
return byteBuf.getData();
case DataBuffer.TYPE_USHORT:
DataBufferUShort shortBuf = (DataBufferUShort) buf;
return shortBuf.getData();
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary
parameters, int index, DecodeOptions options) throws IOException
{
DecodeResult result = new DecodeResult(new COSDictionary());
result.getParameters().addAll(parameters);
BufferedImage image = readJPX(encoded, options, result);
Raster raster = image.getRaster();
switch (raster.getDataBuffer().getDataType())
{
case DataBuffer.TYPE_BYTE:
DataBufferByte byteBuffer = (DataBufferByte) raster.getDataBuffer();
decoded.write(byteBuffer.getData());
return result;
case DataBuffer.TYPE_USHORT:
DataBufferUShort wordBuffer = (DataBufferUShort) raster.getDataBuffer();
for (short w : wordBuffer.getData())
{
decoded.write(w >> 8);
decoded.write(w);
}
return result;
case DataBuffer.TYPE_INT:
// not yet used (as of October 2018) but works as fallback
// if we decide to convert to BufferedImage.TYPE_INT_RGB
int[] ar = new int[raster.getNumBands()];
for (int y = 0; y < image.getHeight(); ++y)
{
for (int x = 0; x < image.getWidth(); ++x)
{
raster.getPixel(x, y, ar);
for (int i = 0; i < ar.length; ++i)
{
decoded.write(ar[i]);
}
}
}
return result;
default:
throw new IOException("Data type " + raster.getDataBuffer().getDataType() + " not implemented");
}
}
/**
* Get the pixel values of the raster as "unsigned shorts"
*
* @param raster
* image raster
* @return "unsigned short" pixel values
*/
public short[] getPixelValues(WritableRaster raster) {
DataBufferUShort buffer = (DataBufferUShort) raster.getDataBuffer();
short[] pixelValues = buffer.getData();
return pixelValues;
}