下面列出了javax.imageio.spi.ImageWriterSpi#canEncodeImage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Checks that the provided <code>ImageWriter</code> can encode
* the provided <code>ImageTypeSpecifier</code> or not. If not, an
* <code>IIOException</code> will be thrown.
* @param writer The provided <code>ImageWriter</code>.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
public static void validate(PDImageXObject ximage, int bpc, int width, int height,
String format, String colorSpaceName) throws IOException
{
// check the dictionary
assertNotNull(ximage);
COSStream cosStream = ximage.getCOSObject();
assertNotNull(cosStream);
assertEquals(COSName.XOBJECT, cosStream.getItem(COSName.TYPE));
assertEquals(COSName.IMAGE, cosStream.getItem(COSName.SUBTYPE));
assertTrue(ximage.getCOSObject().getFilteredLength() > 0);
assertEquals(bpc, ximage.getBitsPerComponent());
assertEquals(width, ximage.getWidth());
assertEquals(height, ximage.getHeight());
assertEquals(format, getSuffix(ximage));
assertEquals(colorSpaceName, ximage.getColorSpace().getName());
// check the image
assertNotNull(ximage.getImage());
assertEquals(ximage.getWidth(), ximage.getImage().getWidth());
assertEquals(ximage.getHeight(), ximage.getImage().getHeight());
boolean canEncode = true;
boolean writeOk;
// jdk11+ no longer encodes ARGB jpg
// https://bugs.openjdk.java.net/browse/JDK-8211748
if ("jpg".equals(format) && ximage.getImage().getType() == BufferedImage.TYPE_INT_ARGB)
{
ImageWriter writer = ImageIO.getImageWritersBySuffix(format).next();
ImageWriterSpi originatingProvider = writer.getOriginatingProvider();
canEncode = originatingProvider.canEncodeImage(ximage.getImage());
}
if (canEncode)
{
writeOk = ImageIO.write(ximage.getImage(), format, new ByteArrayOutputStream());
assertTrue(writeOk);
}
writeOk = ImageIO.write(ximage.getOpaqueImage(), format, new ByteArrayOutputStream());
assertTrue(writeOk);
}
/** Checks that the provided <code>ImageWriter</code> can encode
* the provided <code>ImageTypeSpecifier</code> or not. If not, an
* <code>IIOException</code> will be thrown.
* @param writer The provided <code>ImageWriter</code>.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
/** Checks that the provided <code>ImageWriter</code> can encode
* the provided <code>ImageTypeSpecifier</code> or not. If not, an
* <code>IIOException</code> will be thrown.
* @param writer The provided <code>ImageWriter</code>.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
/** Checks that the provided {@code ImageWriter} can encode
* the provided {@code ImageTypeSpecifier} or not. If not, an
* {@code IIOException} will be thrown.
* @param writer The provided {@code ImageWriter}.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
/** Checks that the provided <code>ImageWriter</code> can encode
* the provided <code>ImageTypeSpecifier</code> or not. If not, an
* <code>IIOException</code> will be thrown.
* @param writer The provided <code>ImageWriter</code>.
* @param type The image to be tested.
* @throws IIOException If the writer cannot encoded the provided image.
*/
public static final void canEncodeImage(ImageWriter writer,
ImageTypeSpecifier type)
throws IIOException {
ImageWriterSpi spi = writer.getOriginatingProvider();
if(type != null && spi != null && !spi.canEncodeImage(type)) {
throw new IIOException(I18N.getString("ImageUtil2")+" "+
writer.getClass().getName());
}
}
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes =
((SinglePixelPackedSampleModel)sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) ||
(i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) ||
((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes =
((SinglePixelPackedSampleModel)sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) ||
(i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) ||
((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
LinkedHashSet<String> natives = new LinkedHashSet<>(1);
if (df == null) {
return natives;
}
String charset = df.getParameter("charset");
String baseType = df.getPrimaryType() + "/" + df.getSubType();
String mimeType = baseType;
if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
mimeType += ";charset=" + charset;
}
// Add a mapping to the MIME native whenever the representation class
// doesn't require translation.
if (df.getRepresentationClass() != null &&
(df.isRepresentationClassInputStream() ||
df.isRepresentationClassByteBuffer() ||
byte[].class.equals(df.getRepresentationClass()))) {
natives.add(mimeType);
}
if (DataFlavor.imageFlavor.equals(df)) {
String[] mimeTypes = ImageIO.getWriterMIMETypes();
if (mimeTypes != null) {
for (int i = 0; i < mimeTypes.length; i++) {
Iterator writers =
ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
while (writers.hasNext()) {
ImageWriter imageWriter = (ImageWriter)writers.next();
ImageWriterSpi writerSpi =
imageWriter.getOriginatingProvider();
if (writerSpi != null &&
writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
natives.add(mimeTypes[i]);
break;
}
}
}
}
} else if (DataTransferer.isFlavorCharsetTextType(df)) {
// stringFlavor is semantically equivalent to the standard
// "text/plain" MIME type.
if (DataFlavor.stringFlavor.equals(df)) {
baseType = "text/plain";
}
for (String encoding : DataTransferer.standardEncodings()) {
if (!encoding.equals(charset)) {
natives.add(baseType + ";charset=" + encoding);
}
}
// Add a MIME format without specified charset.
if (!natives.contains(baseType)) {
natives.add(baseType);
}
}
return natives;
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
public boolean filter(Object elt) {
ImageWriterSpi spi = (ImageWriterSpi)elt;
return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
spi.canEncodeImage(type);
}
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
LinkedHashSet<String> natives = new LinkedHashSet<>(1);
if (df == null) {
return natives;
}
String charset = df.getParameter("charset");
String baseType = df.getPrimaryType() + "/" + df.getSubType();
String mimeType = baseType;
if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
mimeType += ";charset=" + charset;
}
// Add a mapping to the MIME native whenever the representation class
// doesn't require translation.
if (df.getRepresentationClass() != null &&
(df.isRepresentationClassInputStream() ||
df.isRepresentationClassByteBuffer() ||
byte[].class.equals(df.getRepresentationClass()))) {
natives.add(mimeType);
}
if (DataFlavor.imageFlavor.equals(df)) {
String[] mimeTypes = ImageIO.getWriterMIMETypes();
if (mimeTypes != null) {
for (int i = 0; i < mimeTypes.length; i++) {
Iterator writers =
ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
while (writers.hasNext()) {
ImageWriter imageWriter = (ImageWriter)writers.next();
ImageWriterSpi writerSpi =
imageWriter.getOriginatingProvider();
if (writerSpi != null &&
writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
natives.add(mimeTypes[i]);
break;
}
}
}
}
} else if (DataTransferer.isFlavorCharsetTextType(df)) {
// stringFlavor is semantically equivalent to the standard
// "text/plain" MIME type.
if (DataFlavor.stringFlavor.equals(df)) {
baseType = "text/plain";
}
for (String encoding : DataTransferer.standardEncodings()) {
if (!encoding.equals(charset)) {
natives.add(baseType + ";charset=" + encoding);
}
}
// Add a MIME format without specified charset.
if (!natives.contains(baseType)) {
natives.add(baseType);
}
}
return natives;
}
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes =
((SinglePixelPackedSampleModel)sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) ||
(i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) ||
((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes =
((SinglePixelPackedSampleModel)sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) ||
(i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) ||
((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}
public List getPlatformMappingsForFlavor(DataFlavor df) {
List natives = new ArrayList(1);
if (df == null) {
return natives;
}
String charset = df.getParameter("charset");
String baseType = df.getPrimaryType() + "/" + df.getSubType();
String mimeType = baseType;
if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
mimeType += ";charset=" + charset;
}
// Add a mapping to the MIME native whenever the representation class
// doesn't require translation.
if (df.getRepresentationClass() != null &&
(df.isRepresentationClassInputStream() ||
df.isRepresentationClassByteBuffer() ||
byteArrayClass.equals(df.getRepresentationClass()))) {
natives.add(mimeType);
}
if (DataFlavor.imageFlavor.equals(df)) {
String[] mimeTypes = ImageIO.getWriterMIMETypes();
if (mimeTypes != null) {
for (int i = 0; i < mimeTypes.length; i++) {
Iterator writers =
ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
while (writers.hasNext()) {
ImageWriter imageWriter = (ImageWriter)writers.next();
ImageWriterSpi writerSpi =
imageWriter.getOriginatingProvider();
if (writerSpi != null &&
writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
natives.add(mimeTypes[i]);
break;
}
}
}
}
} else if (DataTransferer.isFlavorCharsetTextType(df)) {
// stringFlavor is semantically equivalent to the standard
// "text/plain" MIME type.
if (DataFlavor.stringFlavor.equals(df)) {
baseType = "text/plain";
}
for (String encoding : DataTransferer.standardEncodings()) {
if (!encoding.equals(charset)) {
natives.add(baseType + ";charset=" + encoding);
}
}
// Add a MIME format without specified charset.
if (!natives.contains(baseType)) {
natives.add(baseType);
}
}
return natives;
}
protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
ImageWriterSpi spi = this.getOriginatingProvider();
if (!spi.canEncodeImage(imgType)) {
return false;
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
}
if (compressionType == BI_RLE8 && bpp != 8) {
// only 8bpp images can be encoded as BI_RLE8
return false;
}
if (bpp == 16) {
/*
* Technically we expect that we may be able to
* encode only some of SinglePixelPackedSampleModel
* images here.
*
* In addition we should take into account following:
*
* 1. BI_RGB case, according to the MSDN description:
*
* The bitmap has a maximum of 2^16 colors. If the
* biCompression member of the BITMAPINFOHEADER is BI_RGB,
* the bmiColors member of BITMAPINFO is NULL. Each WORD
* in the bitmap array represents a single pixel. The
* relative intensities of red, green, and blue are
* represented with five bits for each color component.
*
* 2. BI_BITFIELDS case, according ot the MSDN description:
*
* Windows 95/98/Me: When the biCompression member is
* BI_BITFIELDS, the system supports only the following
* 16bpp color masks: A 5-5-5 16-bit image, where the blue
* mask is 0x001F, the green mask is 0x03E0, and the red mask
* is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
* is 0x001F, the green mask is 0x07E0, and the red mask is
* 0xF800.
*/
boolean canUseRGB = false;
boolean canUseBITFIELDS = false;
SampleModel sm = imgType.getSampleModel();
if (sm instanceof SinglePixelPackedSampleModel) {
int[] sizes =
((SinglePixelPackedSampleModel)sm).getSampleSize();
canUseRGB = true;
canUseBITFIELDS = true;
for (int i = 0; i < sizes.length; i++) {
canUseRGB &= (sizes[i] == 5);
canUseBITFIELDS &= ((sizes[i] == 5) ||
(i == 1 && sizes[i] == 6));
}
}
return (((compressionType == BI_RGB) && canUseRGB) ||
((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
}
return true;
}