java讀取圖片轉化爲字符畫

public static String _BitmapConvert(String imgpath) {
    StringBuffer _sb = new StringBuffer();
    File imgfile = new File(imgpath);
    char[] charset = { 'M', '8', '0', 'V', '1', 'i', ':', '*', '|', '.', ' ' };
    try {
        BufferedImage buff = ImageIO.read(imgfile);
 
        int bitmapH = buff.getHeight();
        int bitmapW = buff.getWidth();
 
        for (int y = 0; y < bitmapH; y++) {
            for (int x = 0; x < bitmapW; x++) {
                int rgb = buff.getRGB(x, y);
                Color c = new Color(rgb);
                int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
                _sb.append(charset[(int) ((cc * 10 - 1) / 255)]);
            }
            _sb.append("\r\n");
        }
    } catch (Exception e) {
    }
    return _sb.toString();
}
發佈了34 篇原創文章 · 獲贊 39 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章