骰子作画 发表于 2018-03-16 | 更新于 2018-10-09 | 分类于 Demo | 评论数: | 阅读次数: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091package 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.坚持原创技术分享,您的支持将鼓励我这个小菜鸡的创作! 打赏 微信支付