骰子作画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Hebut_HappyBirthday;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.invoke.ConstantCallSite;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class HebutHappyBirthday {
public BufferedImage hebutImg;
public static int rgb2gray(int argb) {
int alpha = (argb >> 24) & 0xFF;
int red = (argb >> 16) & 0xFF;
int green = (argb >> 8) & 0xFF;
int blue = argb & 0xFF;
return (int)(0.3*red + 0.59*green + 0.11*blue);
}

public static int sixStepGary(int x) {
int i;
int c = 256/10;
for(i = 0; i <= 9; i++) {
if(i*c < x && x <= (i+1)*c) return i;
}
return 9;
}
public static String getXChar(int x, int i) {
String str[] = new String[10];
str[0] = " "; str[1] = " 0"; str[2] = "0 0";
str[3] = "0 "; str[4] = "0 0"; str[5] = "000";
if(x == 0 || (x == 1 && i != 2) || (x == 2 && i == 2) || (x == 4 && i == 2))
return str[0];
if((x == 2 && i == 1) || (x == 3 && i == 1))
return str[1];
if(i == 2 && (x == 1 || x == 2 || x == 3 || x == 5))
return str[2];
if(i == 3 && (x == 2 || x == 3))
return str[3];
if((x == 8 && i == 2) || x == 6 || (x == 7 && (i == 1 || i == 3)) || (x == 5 && (i == 1 || i == 3)) || x == 4)
return str[4];
if(x == 9 || (x == 8 && (i == 1 || i == 3)) || (x == 7 && i == 2))
return str[5];
return null;
}
public HebutHappyBirthday() throws Exception {
hebutImg = ImageIO.read(new File("/media/hejun/新加卷/hebut520.jpg"));
final int MAX_N = 10000;
int i,j,k,l,ii,jj = 0,kk,ll,n = 1;
int[][] rgb = new int[MAX_N][MAX_N];
int[][] ansrgb = new int[MAX_N][MAX_N];
int width = hebutImg.getWidth();
int height = hebutImg.getHeight();
int minx = hebutImg.getMinX();
int miny = hebutImg.getMinY();
//System.out.println(minx + " " + miny + " " + width + " " + height);
for(i = minx; i < height; i++) {
for(j = miny; j < width; j++) {
rgb[i][j] = rgb2gray(hebutImg.getRGB(j, i));//获得像素值
}
}
int sum,count;
for(i = minx, ii = 0; i < height; i += n, ii++) {
for(j = miny, jj = 0; j < width; j += n, jj++) {
sum = count = 0;
for(k = i, kk = 0; kk < n && k < height; k++, kk++) {
for(l = j, ll = 0; ll < n && l < width; l++, ll++) {
sum += rgb[k][l];
count++;
}
}
ansrgb[ii][jj] = sixStepGary(sum/count);
}
}
String str;
for(i = 0; i < ii; i++) {
for(k = 1; k <= 3; k++) {
str = "";
for(j = 0; j < jj; j++) {
str += getXChar(ansrgb[i][j], k);
}
System.out.println(str);
}
}
}
public static void main(String[] args) throws Exception {
HebutHappyBirthday hebutHappyBirthday = new HebutHappyBirthday();
}
}
emm.坚持原创技术分享,您的支持将鼓励我这个小菜鸡的创作!