量化图像

时间:2016-11-23 09:17:12

标签: java eclipse

我对如何量化或压缩已转换为ASCII的图像大小感到非常头疼;我的老师告诉我,这个想法是分组#10; 10 x 10像素"进入一个块,然后将其应用于原始图像。我目前的代码如下:

public static char[][] imageToASCII(BufferedImage img)
{
    //converting colored image to gray scale
    BufferedImage bufImg = toGrayScale(img);
    //read the height and width of original image
    int w = img.getWidth();
    int h = img.getHeight();

    //two-dimensional array for output of pixels
    char[][] res = new char[h][w];
    for(int j=0; j<bufImg.getHeight(); j++)
    {
        for(int i=0; i<bufImg.getWidth(); i++)
        {

    //get the color of pixels
    int values = bufImg.getRGB(i,j);
    Color oldColor = new Color(values);

    //red value
    int red = oldColor.getRed();        

    //green value
    int green = oldColor.getGreen();    

    //blue value
    int blue = oldColor.getBlue();

    //formula for gray value (given by Murphy)
    double grayVal = 0.299 * red + 0.587 * green + 0.114 * blue; //formula for gray scale

    //convert shades of gray into ASCII symbols according to their respective values
    if (grayVal >= 230)
      res[j][i] = ' ';
    else if (grayVal >= 200 && grayVal < 230) 
      res[j][i] = '.';
    else if (grayVal >= 180 && grayVal < 200) 
      res[j][i] = '*';
    else if (grayVal >= 160 && grayVal < 180)
      res[j][i] = ':';
    else if (grayVal >= 130 && grayVal < 160) 
      res[j][i] = 'o';
    else if (grayVal >= 100 && grayVal < 130) 
      res[j][i] = '&';
    else if (grayVal >= 70 && grayVal < 100) 
      res[j][i] = '8';
    else if (grayVal >=50 && grayVal < 70) 
      res[j][i] = '#';
    else 
      res[j][i] = '@';

    }
}
//return array
return res;
}
}   

0 个答案:

没有答案