简体   繁体   English

转换后如何压缩ASCII图像?

[英]how can I compress ASCII image after I convert it?

I need to write a program that convert a image to an ASCII code image I finish the first step,but the requirement need me to compress the image 我需要编写一个程序,将图像转换为ASCII代码图像我完成了第一步,但要求我需要压缩图像

the requiremnet 要求

my work now 我现在的工作

how can I do this? 我怎样才能做到这一点? I only know that I should compress every 10*10 pixels into one 我只知道我应该将每个10 * 10像素压缩成一个

here is my code 这是我的代码

// 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;
   }
}

If you want to compress it by 10 ( new width = old width/10 ) then add this line into imageToASCII() : 如果要将其压缩10( new width = old width/10 ),则将此行添加到imageToASCII()

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

This line converts the image into a smaller version. 此行将图像转换为较小的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM