转换后如何压缩ASCII图像?

时间:2016-11-20 10:56:07

标签: 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;
   }
}

1 个答案:

答案 0 :(得分:0)

如果您要将其压缩10(new width = old width/10),请将此行添加到imageToASCII()

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

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