Java中实现BMP和JPEG图片格式互转的方法
作者:RainLight
时间:2023-04-25
来源:互联网
浏览: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();
}
}
作者最新文章
傲梅轻松备份
2026-09-16 17:40
photoshop路径工具在哪 怎么用
2026-09-16 13:46
PDF怎么批量添加页码?页码位置和起始页怎么设置?
2026-09-04 14:03
GitLab新手创建项目并推送第一次提交的操作指南
2026-09-03 06:05
PDF怎么编辑修改内容?4招处理方法整理
2026-09-02 18:44
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















