图片转base64
import java.io.*;
import java.util.Base64;
public class ImageToBase64Converter {
public static void main(String[] args) {
String imagePath = "path/to/your/image.png"; // 替换为你的图片路径
String outputFilePath = "out.txt";
try {
File imageFile = new File(imagePath);
FileInputStream fis = new FileInputStream(imageFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
byte[] imageBytes = bos.toByteArray();
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));
writer.write(base64Image);
writer.close();
System.out.println("Base64 representation of the image has been written to out.txt.");
fis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
base64转图片
import java.io.*;
import java.util.Base64;
public class Base64ToImageConverter {
public static void main(String[] args) {
String inputFilePath = "out.txt";
String outputImagePath = "output.png";
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
StringBuilder base64Image = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
base64Image.append(line);
}
byte[] imageBytes = Base64.getDecoder().decode(base64Image.toString());
FileOutputStream fos = new FileOutputStream(outputImagePath);
fos.write(imageBytes);
fos.close();
System.out.println("Image has been successfully written to output.png.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
扩展需求:批量操作
批量操作。将 E:\资料\gw-资料 路径下的所有png格式图片,转成base64 ,输出到E:\资料\gw-资料\out.txt 。保留原文件名称的情况下,再将out.txt里面的base64 还原成多个png图片
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
public class BatchImageToBase64Converter {
public static void main(String[] args) {
String inputFolderPath = "E:\\资料\\gw-资料";
String outputFilePath = "E:\\资料\\gw-资料\\out.txt";
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8"));
Files.walk(Paths.get(inputFolderPath))
.filter(Files::isRegularFile)
.filter(path -> path.toString().toLowerCase().endsWith(".png"))
.forEach(path -> {
try {
byte[] imageBytes = Files.readAllBytes(path);
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
writer.write("File: " + path.getFileName() + "\n");
writer.write(base64Image + "\n");
writer.write("----------\n");
} catch (IOException e) {
e.printStackTrace();
}
});
writer.close();
System.out.println("Base64 representations of images have been written to out.txt.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
还原:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
public class BatchBase64ToImageConverter {
public static void main(String[] args) {
String inputFilePath = "E:\\资料\\gw-资料\\out.txt";
String outputFolderPath = "E:\\资料\\gw-资料\\restored_images";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("File: ")) {
String fileName = line.substring("File: ".length());
String base64Data = reader.readLine().trim();
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
Path outputPath = Paths.get(outputFolderPath, fileName);
Files.createDirectories(outputPath.getParent());
Files.write(outputPath, imageBytes);
System.out.println("Image \"" + fileName + "\" restored.");
}
}
reader.close();
System.out.println("All images have been restored.");
} catch (IOException e) {
e.printStackTrace();
}
}
}