问题
In Java, is it expected to be able to have a BufferedImage
such that getColorModel().hasAlpha()
will return true, but getAlphaRaster()
will return null?
I ask because there's a library I'm using (PDFBox specifically, in the PDJpeg
class) that breaks on such an image.
In this particular case I'm creating the image very simply using:
BufferedImage bi = ImageIO.read(new FileInputStream("/Users/dan/Downloads/test.png"));
I've attached the particular image that's failing for me below this question.
Is there some sort of parameter I can pass to ImageIO or some kind of transformation I can do to my BufferedImage
after it's loaded so that it won't run into this problem?
I'm running Java 1.7.0_40 if it matters.
Stack trace for completeness:
java.lang.NullPointerException
at java.awt.image.ComponentColorModel.isCompatibleRaster(ComponentColorModel.java:2787)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:629)
at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.createImageStream(PDJpeg.java:159)
at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.<init>(PDJpeg.java:133)

回答1:
Yes. As the JavaDoc states:
This method assumes that for all
ColorModel
objects other thanIndexColorModel
, if theColorModel
supports alpha, there is a separate alpha channel which is stored as the last band of image data. If the image uses anIndexColorModel
that has alpha in the lookup table, this method returnsnull
since there is no spatially discrete alpha channel.
Your image is a palette PNG with a transparent index. ImageIO will read this into a BufferedImage
with IndexColorModel
(ie., no discrete alpha channel).
You can convert the image to a different BufferedImage
type (like TYPE_INT_RGB
), by creating a blank image of the same size, getting its graphics, and draw the original onto it:
BufferedImage origininal = ...;
BufferedImage copy = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = copy.createGraphics();
try {
g.drawImage(original, 0, 0, null);
}
finally {
g.dispose();
}
You can probably also pass the image type as an ImageTypeSpecifier
on the ImageReadParam
passed to the ImageReader
. But it requires quite a bit more code for the reading part.
来源:https://stackoverflow.com/questions/20428255/hasalpha-vs-getalpharaster