java批量转换图片格式,这里只是实现了更新后缀格式的,图片大小未做处理

package com.qiao.testImage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;


public class imageChange {
    public static void main(String[] args) throws IOException {
        List<String> item=new ArrayList<String>();
        File file = new File("D:\\test\\temp\\temp");
        File[] files = file.listFiles();
//        System.out.println("files长度"+files.length);
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            file1.getName();
//            System.out.println("输出文件后缀名"+file1.getName());
            if (file1.getName().indexOf(".jpg")>-1) {
                item.add("D:\\test\\temp\\temp\\"+files[i].getName());
                System.out.println("输出文件files[i].getName():"+files[i].getName());
            }
        }
        for (int i = 0; i < item.size(); i++) {
            System.out.println("item集合内部数据"+item.get(i));
             String type= "png";
             File src = new File(item.get(i));
             File dir = new File("D:\\test\\image");
             trans(src, dir, type);
        }
        
    }
    
     public static void trans(File imgFile,File outDir,String type) throws IOException {
          BufferedImage img = ImageIO.read(imgFile);
          //设置文件名
          String srcName = imgFile.getName().replace(".jpg", "")+"."+type;
          File out = new File(outDir+File.separator+srcName);
          ImageIO.write(img,type,out);
          
     }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Java代码示例,可以将一个文件夹中所有的图片转换成base64格式,并将结果保存到Excel中: ```java import java.io.*; import java.util.Base64; import org.apache.poi.ss.usermodel.*; public class ImageToBase64Excel { public static void main(String[] args) { String folderPath = "C:/images"; // 图片所在文件夹路径 String excelPath = "C:/images.xlsx"; // 保存Excel文件路径 // 创建Excel工作簿 Workbook workbook = WorkbookFactory.create(new File(excelPath)); Sheet sheet = workbook.createSheet("Images"); int rowNum = 0; // 遍历文件夹中的所有图片文件 File folder = new File(folderPath); File[] files = folder.listFiles(); for (File file : files) { if (file.isFile() && file.getName().toLowerCase().endsWith(".jpg")) { // 读取图片文件并转换成base64格式 byte[] imageBytes = readBytesFromFile(file); String base64String = Base64.getEncoder().encodeToString(imageBytes); // 保存base64格式到Excel中 Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(file.getName()); row.createCell(1).setCellValue(base64String); } } // 保存Excel文件 try (FileOutputStream outputStream = new FileOutputStream(excelPath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } // 读取文件字节流 private static byte[] readBytesFromFile(File file) { try (FileInputStream inputStream = new FileInputStream(file)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } return outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } } } ``` 该代码使用了Apache POI库来创建和操作Excel文件。在遍历文件夹中的所有图片文件时,只处理以“.jpg”为后缀的文件。对于每个图片文件,先读取其字节流,再将其转换成base64格式。最后,将文件名和base64格式保存到Excel文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时有晌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值