java实现图片转化为字符图片--最终代码

代码

package main;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import calculate.CalculateCharArea;
import calculate.CalculateCharArea.CharArea;
import util.TransTool;

public class Starter {

    private static char [] gradations = new char[256];

    private final static int WIDTH = 100;


    /**
     * 
     * 对图片进行缩放,缩放至设定好的width
     * @param source_bi
     * @return
     * @throws IOException 
     */
    private static BufferedImage zoom(BufferedImage source_bi) throws IOException{
        int height =  source_bi.getHeight();
        int width = source_bi.getWidth();
        BufferedImage bi = new BufferedImage(WIDTH, height * WIDTH / width , BufferedImage.TYPE_BYTE_GRAY);

        Graphics g = bi.getGraphics();
        g.drawImage(source_bi, 0, 0, WIDTH, height * WIDTH / width , null);
        g.dispose();

        File file = new File("e://final.jpg");

        ImageIO.write(bi, "jpg", file);

        return bi;
    }

    /**
     *  将计算出来的字符数据转化为256色阶中对应位置
     * @param arrays
     */
    public static void hashChars2Arrays(CharArea[] arrays){
        int min = arrays[0].pixNum;
        int max = arrays[ arrays.length - 1].pixNum;
        int gap = max - min;

//      double ratio = gap / 256.0 ;
//      System.out.println(ratio);

        for (CharArea charArea : arrays) {
            gradations[(int)((charArea.pixNum - min) * 255 / gap)] = charArea.c;
        }

        char c = gradations[0];
        for(int i = 1 ; i < 255 ; i++){
            if(gradations[i] == '\0')
                gradations[i] = c;
            else
                c = gradations[i];
        }

        for (char s : gradations) {
            System.out.print(s+" ");
        }
    }

    public static void print(int height, int width, BufferedImage bi){
        int b = 0;
         for (int i = 0; i < height; i++) {  
                for (int j = 0; j < width; j++) {  
                    int pixel = bi.getRGB(j, i); // 下面三行代码将一个数字转换为RGB数字  
                    b = (pixel & 0xff);
                    System.out.printf("%2s", gradations[b]);
                }  
                System.out.print('\n');
            }

    }

    public static void main(String[] args) throws IOException {

        //引入文件
        InputStream imageis = TransTool.class.getClassLoader().getResourceAsStream("image/test.jpg");

        BufferedImage bi = zoom(TransTool.trans2GrayImBuffer(imageis));
        int height = bi.getHeight();
        int width = bi.getWidth();

        CharArea[] arrays = CalculateCharArea.getArrays();

        hashChars2Arrays(arrays);
        gradations[0] = ' ';
        print(height, width, bi);

    }
}

原图

这里写图片描述

转化后

这里写图片描述

代码地址

https://github.com/finepix/image2Char

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值