Java怎么实现bmp和jpeg图片格式互转
作者:RiverSoul
时间:2023-04-28
浏览:5
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();
}
}
作者最新文章
漫蛙manwa永久网址及最新入口分享
2026-11-22 13:58
AO3中文版官网入口2026最新地址
2026-11-22 12:05
剧云网页登录入口 剧云AI电脑端访问地址
2026-11-21 13:07
AO3主页入口及最新镜像链接合集
2026-11-19 13:38
Yandex国际版入口及官网链接
2026-11-14 10:20
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































