java - 转换后如何压缩 ASCII 图像?

标签 java

我需要编写一个程序将图像转换为 ASCII 代码图像 我完成了第一步,但是要求我压缩图像

the requiremnet

my work now

我该怎么做?我只知道应该将每10*10像素压缩为一个

这是我的代码

// A method to convert color image to grayscale image
public static BufferedImage toGrayScale(Image img)
{
    // Convert image from type Image to BufferedImage
    BufferedImage bufImg = convert(img);

    // Scan through each row of the image
    for(int j=0; j<bufImg.getHeight(); j++)
    {
        // Scan through each columns of the image
        for(int i=0; i<bufImg.getWidth(); i++)
        {
            // Returns an integer pixel in the default RGB color model
            int values=bufImg.getRGB(i,j);
            // Convert the single integer pixel value to RGB color
            Color oldColor = new Color(values);

            int red = oldColor.getRed();        // get red value
            int green = oldColor.getGreen();    // get green value
            int blue = oldColor.getBlue();  // get blue value

            // Convert RGB to grayscale using formula
            // gray = 0.299 * R + 0.587 * G + 0.114 * B
            double grayVal = 0.299*red + 0.587*green + 0.114*blue;

            // Assign each channel of RGB with the same value
            Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal);

            // Get back the integer representation of RGB color
            // and assign it back to the original position
            bufImg.setRGB(i, j, newColor.getRGB());
        }
    }
    // return back the resulting image in BufferedImage type
    return bufImg;
}


  public static char[][] imageToASCII(Image img)
  {
    //convert the image to grayscale  
    BufferedImage bufImg = toGrayScale(img);  

    //get the width and height of the image
    int width = bufImg.getWidth();   
    int height = bufImg.getHeight();

    //These varibles are used to store the value of the colour model
    int C1, C2, C3; 

    //create an array to store the output of each pixel  
    char[][] imageToASCII = new char[height][width]; 
    for(int i = 0; i < height; i++){
          for(int j = 0; j < width; j++){
              //get the colour value of each pixel
              int value = bufImg.getRGB(j, i); 

              //get the value in the Blue sector of RGB model
              C1 = value & 255;      

              //get the value in the Green sector of RGB model
              C2 = (value >> 8) & 255; 

              //get the value in the Red sector of RGB model
              C3 = (value >> 16) & 255;

              //calculate the average gray value
              int g = (C1 + C2 + C3) / 3; 

              //check the given conditions and get the relative output  
              if (g >= 230)                         
                    imageToASCII[i][j] = ' ';
              else if (g >= 200)
                      imageToASCII[i][j] = '.';
              else if (g >= 180)
                      imageToASCII[i][j] = '*';
              else if (g>= 160)
                      imageToASCII[i][j] = ':';
              else if (g >= 130)
                      imageToASCII[i][j] = 'o';
              else if (g >= 100)
                      imageToASCII[i][j] = '&';
              else if (g >= 70)
                      imageToASCII[i][j] = '8';
              else if (g >= 50)
                      imageToASCII[i][j] = '#';
              else
                      imageToASCII[i][j] = '@';

          }
      }

//return the array
    return imageToASCII;
   }
}

最佳答案

如果您想将其压缩 10 (新宽度 = 旧宽度/10),则将此行添加到 imageToASCII() 中:

bufImg = convert(bufImg.getScaledInstance(bufImg.getWidth() / 10, bufImg.getHeight() / 24, BufferedImage.SCALE_SMOOTH));

此行将图像转换为较小的版本。

关于java - 转换后如何压缩 ASCII 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40703199/

相关文章:

使用数组时出现 java.lang.NullPointerException 错误

java - 创建代理对象以异步调用目标方法(拆分逻辑/渲染线程)

java - 如何解决错误? Swing 按钮重画

java - SimpleOpenNI 记录和回放用户跟踪数据

Java::在不同的类中使用鼠标监听器对象

java - 如何在 Java 项目 UML 中对资源进行建模?

javac 找不到符号构造函数服务

java - 当用户单击微调器中的一项时,它应该显示另一项数据将从 JSON Web 服务获取

java - 在控制台编译java文件

java - 使用Gradle将外部配置文件添加到jar