java Java中的图像转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14618953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:50:55  来源:igfitidea点击:

Image Conversion In Java

javaimage

提问by vvekselva

I've designed a rest service to respond the images stored in the database for the users. The service works fine. The service responds the images in jpg format. If the user uploads the image in the jpg format the the response is also fine, if the image is some other formats, then the response cannot be rendered as image. I need to construct a jpg converter or encoder, for all the input image types. Is there are any possible way to achieve this.

我设计了一个休息服务来为用户响应存储在数据库中的图像。该服务工作正常。该服务以 jpg 格式响应图像。如果用户上传jpg格式的图片,响应也可以,如果图片是其他格式,则响应无法渲染为图像。我需要为所有输入图像类型构建一个 jpg 转换器或编码器。是否有任何可能的方法来实现这一目标。

回答by MadProgrammer

You should take a look at ImageIO. It has support for reading and writing JPEG, PNG, BMP, WBMP and GIF.

你应该看看ImageIO。它支持读取和写入 JPEG、PNG、BMP、WBMP 和 GIF。

The JAI API also supplies TIFF support and I've used a RAW plugin for Nikon cameras before as well.

JAI API 还提供 TIFF 支持,我之前也为尼康相机使用过 RAW 插件。

Check out Working with Imagesand the JavaDocsfor more info.

查看使用图像JavaDocs了解更多信息。

Updated with Example

更新示例

Without the source image is not going to be possible to do a proper test, but this is the basic work flow.

没有源图像将无法进行适当的测试,但这是基本的工作流程。

I've used Fileas my inputs, but to demonstrate the basic concept, I've create InputStreamand OutputStream(as ImageIOcan read/write Files)

我已经用作File我的输入,但为了演示基本概念,我创建了InputStreamOutputStreamImageIO可以读/写File

File inputFile = new File("/path/to/image.png");
File outputFile = new File("Test.jpg");
try (InputStream is = new FileInputStream(inputFile)) {
    BufferedImage image = ImageIO.read(is);
    try (OutputStream os = new FileOutputStream(outputFile)) {
        ImageIO.write(image, "jpg", os);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
} catch (Exception exp) {
    exp.printStackTrace();
}

Updated

更新

So using the above code, I was able to convert a PNG file I created in paint to JPG...

所以使用上面的代码,我能够将我在paint中创建的PNG文件转换为JPG...

PNG/JPG

PNG/JPG

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

You could also try converting the input stream and output stream to an ImageInputStreamand ImageOutputStream, although this is normally done when you want to look up the providers for a given image format.

您还可以尝试将输入流和输出流转换为ImageInputStreamand ImageOutputStream,尽管这通常是在您想要查找给定图像格式的提供程序时完成的。

File inputFile = new File("...");
File outputFile = new File("Test.jpg");
try (InputStream is = new FileInputStream(inputFile)) {
    ImageInputStream iis = ImageIO.createImageInputStream(is);
    BufferedImage image = ImageIO.read(iis);
    try (OutputStream os = new FileOutputStream(outputFile)) {
        ImageOutputStream ios = ImageIO.createImageOutputStream(os);
        ImageIO.write(image, "jpg", ios);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
} catch (Exception exp) {
    exp.printStackTrace();
}