判断base64中是不是只包含一种颜色
打印结果为true说明只有一种颜色
public static void main(String[] args) {
// 替换为你的图片路径
String filePath = "F:\\image\\a1.png";
try {
String base64Image = convertImageToBase64(filePath);
System.out.println(base64Image);
boolean isSingleColor = isSingleColor(base64Image);
System.out.println("图像是否只有一种颜色: " + isSingleColor);
} catch (IOException e) {
e.printStackTrace();
}
}
// 将图像文件转换为 Base64 字符串
public static String convertImageToBase64(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
return Base64.getEncoder().encodeToString(bytes);
}
// 判断 Base64 图像是否只有一种颜色
public static boolean isSingleColor(String base64Image) {
try {
// 解码 Base64 字符串为字节数组
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
// 从字节数组创建 BufferedImage
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
if (image == null) {
throw new IOException("图像为空,请检查 Base64 字符串。");
}
// 获取第一个像素的颜色
int firstColor = image.getRGB(0, 0);
// 检查所有像素的颜色
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
if (image.getRGB(x, y) != firstColor) {
return false; // 找到不同颜色
}
}
}
return true; // 所有像素颜色相同
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
将base64转成图片保存本地
public static void main(String[] args) {
// 替换为你的图片路径
try {
String base64Image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABOwAAAnUCAYAAAC6u/mFAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAACAASURBVHic7MEBAQAAAICQ/q/uCAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
// 调用方法保存图片
saveBase64ImageToFile(base64Image, "F:\\image\\output.png");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveBase64ImageToFile(String base64Image, String filePath) {
// 去除前缀(如果有)
if (base64Image.startsWith("data:image/png;base64,")) {
base64Image = base64Image.substring("data:image/png;base64,".length());
}
// 解码Base64字符串
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
// 保存到文件
try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
fos.write(imageBytes);
System.out.println("图片保存成功:" + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}