I am trying to use Mime Types to define the output for ImageIO. The simple write() method
我正在尝试使用Mime Types来定义ImageIO的输出。简单的write()方法
public static boolean write(RenderedImage im,
String formatName,
OutputStream output)
throws IOException
uses " informal name of the format." (e.g. "png"). Is there a simple equivalent for mimetypes (e.g. "image/png") or failing that code that achieves the purpose of writing an image? The only starting point I have found is
使用“格式的非正式名称”。 (例如“png”)。是否存在mimetypes的简单等价物(例如“image / png”)或者是否达不到编写图像目的的代码?我发现的唯一起点是
public static Iterator<ImageWriter> getImageWritersByMIMEType(String MIMEType)
which seems much more complex, requiring ImageWriter
, IIOStream
etc. and I haven't managed to create a solution.
这似乎要复杂得多,需要ImageWriter,IIOStream等,而我还没有设法创建解决方案。
UPDATE: The reason I am using MIME is that it is a formal part of the image in SVG, e.g.
更新:我使用MIME的原因是它是SVG中图像的正式部分,例如
xlink:href="data:image/png;base64,iVBORw0KGgoAAAA...
and it seemed appropriate to use it rather than converting it to less defined "informal" formats. I managed to find a precise solution (in java2s.com) to my question and have added it as an answer.
使用它而不是将其转换为不太明确的“非正式”格式似乎是合适的。我设法在我的问题中找到了一个精确的解决方案(在java2s.com中),并将其添加为答案。
2 个解决方案
#1
3
I have now found a formal solution to my question on a java2s tutorial
我现在已经找到了一个关于java2s教程的问题的正式解决方案
public class Main {
static public void main(String args[]) throws Exception {
int width = 200, height = 200;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.fillRect(0, 0, width - 1, height - 1);
Iterator imageWriters = getImageWritersByMIMEType("image/gif");
ImageWriter imageWriter = (ImageWriter) imageWriters.next();
File file = new File("filename.gif");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
imageWriter.setOutput(ios);
imageWriter.write(bi);
}
(edited as corrected by @haraldK ).
(编辑经@haraldK更正)。
#2
2
I printed the list of ImageIO informal formats, using the getReaderFormatNames method, on my Windows XP, Java 6 computer.
我在Windows XP,Java 6计算机上使用getReaderFormatNames方法打印了ImageIO非正式格式列表。
BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif
Here are the equivalent MIME types
以下是等效的MIME类型
image/bmp
image/jpeg
image/png
image/gif
I couldn't find a MIME for the WBMP format.
我找不到WBMP格式的MIME。
Seems like you could just strip off the "image/" from the MIME to pass the informal format to the ImageIO read or write methods.
好像您可以从MIME中剥离“image /”以将非正式格式传递给ImageIO读取或写入方法。
#1
3
I have now found a formal solution to my question on a java2s tutorial
我现在已经找到了一个关于java2s教程的问题的正式解决方案
public class Main {
static public void main(String args[]) throws Exception {
int width = 200, height = 200;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.fillRect(0, 0, width - 1, height - 1);
Iterator imageWriters = getImageWritersByMIMEType("image/gif");
ImageWriter imageWriter = (ImageWriter) imageWriters.next();
File file = new File("filename.gif");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
imageWriter.setOutput(ios);
imageWriter.write(bi);
}
(edited as corrected by @haraldK ).
(编辑经@haraldK更正)。
#2
2
I printed the list of ImageIO informal formats, using the getReaderFormatNames method, on my Windows XP, Java 6 computer.
我在Windows XP,Java 6计算机上使用getReaderFormatNames方法打印了ImageIO非正式格式列表。
BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif
Here are the equivalent MIME types
以下是等效的MIME类型
image/bmp
image/jpeg
image/png
image/gif
I couldn't find a MIME for the WBMP format.
我找不到WBMP格式的MIME。
Seems like you could just strip off the "image/" from the MIME to pass the informal format to the ImageIO read or write methods.
好像您可以从MIME中剥离“image /”以将非正式格式传递给ImageIO读取或写入方法。