Java怎么实现bmp和jpeg图片格式互转
作者:RiverSoul
时间:2023-04-26
来源:互联网
浏览:0
Bmp转JpegpublicstaticStringbmp2Jpeg(StringfilePath,StringoutPath){try{longstart=System.currentTimeMillis();//加载bmp图片Filefile=newFile(filePath);Imageimg=ImageIO.read(file);BufferedImagetag=newBufferedImage(img.getWidth(null),img.getHeight(null),BufferedImag
Bmp转Jpeg
public static String bmp2Jpeg(String filePath, String outPath) {
try {
long start = System.currentTimeMillis();
// 加载bmp图片
File file = new File(filePath);
Image img = ImageIO.read(file);
BufferedImage tag = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img.getScaledInstance(img.getWidth(null), img.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
// 输出为Jpeg
FileOutputStream out = new FileOutputStream(outPath);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
log.info("bmp 转 JPEG,共耗时: " + (System.currentTimeMillis() - start) + " 毫秒");
return outPath;
} catch (IOException e) {
e.printStackTrace();
}
return outPath;
}Jpeg转Bmp
public static void jpeg2Bmp(String inputPath, String outputPath) {
try {
long start = System.currentTimeMillis();
// 加载Jpeg图片资源
FileImageInputStream fiis = new FileImageInputStream(new File(inputPath));
FileImageOutputStream fios = new FileImageOutputStream(new File(outputPath));
ImageReader jpegReader = null;
Iterator it1 = ImageIO.getImageReadersByFormatName("jpeg");
if (it1.hasNext()) {
jpegReader = it1.next();
}
jpegReader.setInput(fiis);
ImageWriter bmpWriter = null;
Iterator it2 = ImageIO.getImageWritersByFormatName("bmp");
if (it2.hasNext()) {
bmpWriter = it2.next();
}
bmpWriter.setOutput(fios);
BufferedImage br = jpegReader.read(0);
bmpWriter.write(br);
fiis.close();
fios.close();
log.info("jpeg 转 bmp,共耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
} catch (IOException e) {
e.printStackTrace();
}
}
作者最新文章
微软推出Project Zenith:面向Windows 11开发者的AI硬件加速方案
2026-09-08 18:15
打破流量垄断,让平台经济释放普惠红利
2026-09-08 18:07
Arm AGI CPU详解:136核Neoverse V3,3nm双芯粒架构与AI数据中心部署
2026-09-08 17:18
Windows安装Docker教程:启用WSL2并运行第一个容器验证
2026-09-04 09:26
PDF转Word操作指南:在线与本地转换方法及格式检查
2026-09-03 16:03
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















