我设计了一个休息服务来响应用户存储在数据库中的图像.服务很好.该服务以jpg格式响应图像.如果用户以jpg格式上传图像,则响应也很好,如果图像是某些其他格式,则响应不能呈现为图像.我需要为所有输入图像类型构建一个jpg转换器或编码器.有没有可能的方法来实现这一目标.
Mad*_*mer 11
你应该看看ImageIO.它支持读写JPEG,PNG,BMP,WBMP和GIF.
JAI API也提供TIFF支持,我之前也使用过RAW插件用于尼康相机.
更新了示例
没有源图像就不可能进行适当的测试,但这是基本的工作流程.
我用File我的输入,但表现出的基本概念,我已经创建InputStream和OutputStream(如ImageIO可以读/写FileS)
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();
}
Run Code Online (Sandbox Code Playgroud)
更新
所以使用上面的代码,我能够将我在paint中创建的PNG文件转换为JPG ...
PNG/JPG


您也可以尝试将输入流和输出流转换为ImageInputStream和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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17495 次 |
| 最近记录: |