java实现图片转化为字符图片--统计字符所占的面积

字符面积

一个字符所需要覆盖的区域,比如#~比较,很明显,#所占的区域大一点。

代码实现

package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;


/**
 * @author F-zx
 * 计算出每个字符需要占用的面积,用来表示每一个像素点需要用的字符,//取ASCII码的33----125 92个
 * 取没个字符的size为FONT_SIZE,所以这个图片的width为FONT_SIZE*92
 */
public class CalculateCharArea {

    /**
     * @author F-zx
     * 对compareable实现,目的在于对该类进行排序,按照pixNum排序,其实可以不用排序,这里只是想复习一个该接口的实现
     */
    private static class CharArea implements Comparable<CharArea>{
        public int pixNum ;//该字符所有的有色点的个数
        public char c;//该字符

        public CharArea(int pixNum , char c) {
            this.c = c;
            this.pixNum = pixNum;
        }



        @Override
        public int compareTo(CharArea o) {
            if(o.pixNum <= this.pixNum)
                return 1;
            else
                return -1;
        }
    }

    private final static int NUM = 92;
    private final static int HEIGHT = 50;
    private final static int FONT_SIZE = 50;
    private final static int WIDTH = FONT_SIZE * NUM ;

    private static StringBuffer buffer = null;
    private static File file = null;
    private static BufferedImage bImage  = null;

    private static CharArea[] chars = new CharArea[NUM];



    /** 
     * 建立一个字符的序列
     * @return
     */
    private static String build(){
        buffer = new StringBuffer();

        //取ASCII码的33----125 92个
        for(int i = 33 ; i < 125 ; i++)
            buffer.append((char)i);

        return buffer.toString();

    }

    /**
     * 绘制出相应的图片
     */
    private static void paint(){
        file = new File("e://resources/char.jpg");  

        bImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_BINARY);

        Font font = new Font(null, Font.PLAIN, FONT_SIZE);

        Graphics2D g = bImage.createGraphics();
        g.setBackground(Color.white);
        g.setColor(Color.black);
        g.setFont(font);
        g.clearRect(0, 0, WIDTH, HEIGHT);

        String strs = build();
        char[] c = strs.toCharArray();

        int i = 0 ;
        for (char d : c) {
            g.drawString(""+d, FONT_SIZE * ( i++ ), 40);
        }

        g.dispose();

        try {
            ImageIO.write(bImage, "jpg", file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("ok");
    }

    /**
     * 计算出没个的面积,然后排序一下,这里计算没个区域(size*size)的大小,在其中寻找有色点的个数,然后对这些进行排序,引入一个数据结构,
     * ps:对于每个点,-1表示该点为白色
     * @throws IOException 
     */
    private static void calculate() throws IOException{

        if(file == null)
            file = new File("e://resources/char.jpg");  
        if(bImage == null)
            bImage = ImageIO.read(file);

        int count = 0;//每个字符的有色点个数
        for(int j = 1 ; j <= NUM ; j ++){                   //个数循环
            count = 0;
            chars[j-1] = new CharArea( 0 , (char) (j+32) ); //因为字符是从33开始的,所以j+32对应的是33的起始位置
            for(int k = (j-1)*50 ; k < 50*j ; k++){         //没个字符的宽度循环
                for(int m = 0 ; m < 50 ; m++ ){             //每个字符的高度循环
                    int rgb = bImage.getRGB(k, m);

                    count += (rgb != -1) ? 1: 0;
//                  System.out.println(rgb); //测试点的颜色输出
                }
            }
            chars[j-1].pixNum = count;
        }

    }

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

//      for (CharArea area : chars) {
//          System.out.println(area.c+" "+area.pixNum);
//      }

        Arrays.sort(chars);

        for (CharArea area : chars) {
            System.out.println(area.c+" "+area.pixNum);
        }
    }
}

详情请看代码注释

输出结果

ok
. 16
, 32
: 32
` 32
' 46
; 48
- 56
" 92
! 108
_ 112
* 122
i 128
^ 142
l 144
r 145
/ 148
\ 148
< 158
> 158
j 166
( 170
) 170
+ 176
t 177
I 180
= 184
| 184
1 185
f 201
v 203
7 215
{ 218
? 221
[ 224
] 224
x 232
c 239
L 248
J 249
u 257
n 261
y 262
z 265
T 276
s 281
k 284
o 284
h 302
Y 308
4 317
2 319
3 320
F 320
e 327
5 331
a 344
p 344
0 345
q 345
b 347
d 347
w 364
C 367
9 373
6 379
V 379
P 385
Z 390
m 391
A 392
U 399
8 400
X 400
g 409
E 420
& 429
S 429
H 432
$ 435
K 437
# 445
O 456
D 466
% 468
G 469
R 491
Q 492
B 510
N 526
W 606
M 653
@ 844

后续

接下来需要做的就是将这些信息对应256个色阶分配下去,然后绘制出相应的字符图像。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值