JAVA图片数据转七色数据
说明: 循环读取图片每一个像素点,获取rgb数据再转成hsv数据。
根据hsv转换成七色数据。
public class ImageToSevenColorData {
// 定义七种颜色的常量
private static final int BLACK = 0;
private static final int WHITE = 1;
private static final int GREEN = 2;
private static final int BLUE = 3;
private static final int RED = 4;
private static final int YELLOW = 5;
private static final int ORANGE = 6;
private static final int[] COLORS = {
0x000000, // 黑色
0xFFFFFF, // 白色
0x00FF00, // 绿色
0x0000FF, // 蓝色
0xFF0000, // 红色
0xFFFF00, // 黄色
0xFFA500 // 橙色
};
public static void main(String[] args) {
try {
// Step 1: 读取图片
BufferedImage image = ImageIO.read(new File("F:\\1\\input_image.jpg"));
int newWidth = 600; // 新的宽度
int newHeight = 448; // 新的高度
// 创建一个缓冲图像,用于调整尺寸
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
// 绘制原始图片到调整尺寸的图像上
Graphics graphics = resizedImage.createGraphics();
graphics.drawImage(image, 0, 0, newWidth, newHeight, null);
graphics.dispose();
ImageIO.write(resizedImage, "jpg", new File("F:\\1\\output_image.jpg"));
String text ="";
String imageText1 = "";
int i = 1;
for (int y = 0; y < newHeight; y++) {
for (int x = 0; x < newWidth; x++) {
int rgb = resizedImage.getRGB(x, y);
int sevenColorData = convertToSevenColor(rgb);
text += sevenColorData;
// if (text.length() == 134400) {
// imageText1 += text;
// saveStringToTxt(text, "F:\\1\\output_seven_color_data_part"+i+".txt");
// text = "";
// i++;
// }
}
}
//生产该数据的图片
int[][] pixelData = parsePixelData(text, newWidth, newHeight);
String outputPath = "F:\\1\\output_last.jpg";
generateImage(pixelData, outputPath);
// saveStringToTxt(imageText1, "F:\\1\\output_seven_color_data_all.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void saveStringToTxt(String content, String fileName) {
try {
// 指定要写入的txt文件路径
// String fileName = "F://output.txt";
// 创建FileWriter对象,第二个参数为true表示以追加的方式写入
FileWriter writer = new FileWriter(fileName, true);
// 将字符串写入文件
writer.write(content);
// 关闭文件写入流
writer.close();
System.out.println("字符串已成功写入txt文件。");
} catch (IOException e) {
e.printStackTrace();
}
}
// 方法:将RGB颜色值转换为七色图数据
public static int convertToSevenColor(int rgbColor) {
Color color = new Color(rgbColor);
float[] hsv = new float[3];
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsv);
// 取出色调 (H)
float hue = hsv[0];
float saturation = hsv[1];
float value = hsv[2];
if (saturation < 0.2) {
if (value > 0.5) {
return WHITE;
}else {
return BLACK;
}
}
if (value < 0.3) {
return BLACK;
}
if (hue < 0.04) {
return RED;
}else if (hue < 0.12) {
return ORANGE;
}else if (hue < 0.18) {
return YELLOW;
}else if (hue < 0.5){ //0.43
return GREEN;
}else if (hue < 0.7){
return BLUE;
}else {
return RED;
}
}
// 解析像素数据字符串并生成像素数组
public static int[][] parsePixelData(String pixelDataString, int width, int height) {
int[][] pixelData = new int[height][width];
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
char colorChar = pixelDataString.charAt(index);
int colorIndex = Character.getNumericValue(colorChar);
pixelData[y][x] = colorIndex;
index++;
}
}
return pixelData;
}
// 生产图像
public static void generateImage(int[][] pixelData, String outputPath) {
int width = pixelData[0].length;
int height = pixelData.length;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int colorIndex = pixelData[y][x];
int color = COLORS[colorIndex];
image.setRGB(x, y, color);
}
}
// 保存图像为JPG格式
try {
File output = new File(outputPath);
ImageIO.write(image, "jpg", output);
System.out.println("图像保存成功!");
} catch (IOException e) {
System.out.println("图像保存失败:" + e.getMessage());
}
}
}