标签:style blog http java color os strong 文件
在之前写了一篇blog:java画图程序_图片用字母画出来
主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试)。
就把源码发布出来。
项目结构:
资源文件:
原图:http://image.mamicode.com/info/201407/20180110153956310549.png
运行效果:
原图:http://image.mamicode.com/info/201407/20180110153956314455.png
===================================================
源码部分:
===================================================
/imageHandler/src/com/b510/image/client/Client.java
1 /** 2 * 3 */ 4 package com.b510.image.client; 5 6 import java.io.File; 7 8 import com.b510.image.common.Common; 9 import com.b510.image.util.ImageUtil; 10 import com.b510.image.util.TextUtil; 11 12 /** 13 * This project is a tool for processing the image. <br>Including TWO functions : 14 * <li>Color image to converted black and white picture. 15 * <li>Imitating the original image to paint into the TXT file with alphabets. 16 * You can change the code according to your requirement. 17 * 18 * @author Hongten 19 * @mail hongtenzone@foxmail.com 20 * @created Jul 23, 2014 21 */ 22 public class Client { 23 24 public static void main(String[] args) { 25 colorImage2BWPic(); 26 painting(); 27 } 28 29 private static void painting() { 30 double[][] result = ImageUtil.getImageGRB(Common.ORIGINAL_IMAGE); 31 StringBuffer stringBuffer = ImageUtil.getCanvas(result); 32 TextUtil.write2File(stringBuffer); 33 } 34 35 /** 36 * Color image to converted black and white picture. 37 */ 38 private static void colorImage2BWPic() { 39 File input = new File(Common.ORIGINAL_IMAGE); 40 File out = new File(Common.PROCESSED_IMAGE); 41 ImageUtil.colorImage2BlackAndWhitePicture(input, out); 42 } 43 }
/imageHandler/src/com/b510/image/common/Common.java
1 /** 2 * 3 */ 4 package com.b510.image.common; 5 6 /** 7 * @author Hongten 8 * @created Jul 23, 2014 9 */ 10 public class Common { 11 12 public static final String PATH = "src/com/b510/image/resources/"; 13 14 public static final String ORIGINAL_IMAGE = PATH + "original_image.png"; 15 public static final String PROCESSED_IMAGE = PATH + "processed_image.png"; 16 17 public static final String OUTPUT_TXT = PATH + "output.txt"; 18 19 public static final String PROCESSED_SUCCESS = "Processed successfully..."; 20 public static final String PROCESS_ERROR = "Processing encounters error!"; 21 22 public static final String R = "R"; 23 public static final String A = "A"; 24 public static final String X = "X"; 25 public static final String M = "M"; 26 public static final String W = "W"; 27 public static final String H = "H"; 28 public static final String E = "E"; 29 public static final String J = "J"; 30 public static final String L = "L"; 31 public static final String C = "C"; 32 public static final String V = "V"; 33 public static final String Z = "Z"; 34 public static final String Q = "Q"; 35 public static final String T = "T"; 36 public static final String r = "r"; 37 public static final String s = "s"; 38 public static final String w = "w"; 39 public static final String u = "u"; 40 public static final String l = "l"; 41 public static final String i = "i"; 42 public static final String e = "e"; 43 public static final String m = "m"; 44 public static final String a = "a"; 45 public static final String COMMA = ","; 46 public static final String BLANK = " "; 47 48 public static final String NEW_LINE = "\n"; 49 }
/imageHandler/src/com/b510/image/util/ImageUtil.java
1 /** 2 * 3 */ 4 package com.b510.image.util; 5 6 import java.awt.Image; 7 import java.awt.color.ColorSpace; 8 import java.awt.image.BufferedImage; 9 import java.awt.image.ColorConvertOp; 10 import java.io.File; 11 import java.io.FileOutputStream; 12 import java.io.IOException; 13 14 import javax.imageio.ImageIO; 15 16 import com.b510.image.common.Common; 17 import com.sun.image.codec.jpeg.JPEGCodec; 18 import com.sun.image.codec.jpeg.JPEGImageEncoder; 19 20 /** 21 * @author Hongten 22 * @created Jul 23, 2014 23 */ 24 public class ImageUtil { 25 26 private static int height = 0; 27 private static int width = 0; 28 29 /** 30 * Color image is converted to black and white picture. 31 * @param input 32 * @param out 33 */ 34 public static void colorImage2BlackAndWhitePicture(File input, File out) { 35 try { 36 Image image = ImageIO.read(input); 37 int srcH = image.getHeight(null); 38 int srcW = image.getWidth(null); 39 BufferedImage bufferedImage = new BufferedImage(srcW, srcH, BufferedImage.TYPE_3BYTE_BGR); 40 bufferedImage.getGraphics().drawImage(image, 0, 0, srcW, srcH, null); 41 bufferedImage = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bufferedImage, null); 42 FileOutputStream fos = new FileOutputStream(out); 43 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); 44 encoder.encode(bufferedImage); 45 fos.close(); 46 System.out.println(Common.PROCESSED_SUCCESS); 47 } catch (Exception e) { 48 throw new IllegalStateException(Common.PROCESS_ERROR, e); 49 } 50 } 51 52 /** 53 * Get the image(*.jpg, *.png.etc) GRB value 54 * @param filePath the original image path 55 * @return 56 */ 57 public static double[][] getImageGRB(String filePath) { 58 File file = new File(filePath); 59 double[][] result = null; 60 if (!file.exists()) { 61 return result; 62 } 63 try { 64 BufferedImage bufImg = readImage(file); 65 result = new double[height][width]; 66 for (int y = 0; y < height; y++) { 67 for (int x = 0; x < width; x++) { 68 double temp = Double.valueOf(bufImg.getRGB(x, y) & 0xFFFFFF); 69 result[y][x] = Math.floor(Math.cbrt(temp)); 70 } 71 } 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } 75 return result; 76 } 77 78 /** 79 * Read the original image to convert BufferedImage 80 * @param file Original image path 81 * @return 82 * @see BufferedImage 83 * @throws IOException 84 */ 85 private static BufferedImage readImage(File file) throws IOException { 86 BufferedImage bufImg = ImageIO.read(file); 87 height = bufImg.getHeight(); 88 width = bufImg.getWidth(); 89 return bufImg; 90 } 91 92 /** 93 * Get the canvas, which is made up of alphabets. 94 * @param result 95 * @return 96 */ 97 public static StringBuffer getCanvas(double[][] result) { 98 StringBuffer stringBuffer = new StringBuffer(); 99 for (int y = 0; y < height; y++) { 100 for (int x = 0; x < width; x++) { 101 fullBlank(result, stringBuffer, y, x); 102 } 103 stringBuffer.append(Common.NEW_LINE); 104 } 105 return new StringBuffer(stringBuffer.substring(0, stringBuffer.length() - 1)); 106 } 107 108 /** 109 * Full blank 110 * @param result 111 * @param stringBuffer 112 * @param y 113 * @param x 114 */ 115 private static void fullBlank(double[][] result, StringBuffer stringBuffer, int y, int x) { 116 if (result[y][x] > 0.0 && result[y][x] < 168.0) { 117 fullBlackColor(result, stringBuffer, y, x); 118 } else if (result[y][x] >= 168.0 && result[y][x] < 212.0) { 119 fullGreyColor(result, stringBuffer, y, x); 120 } else { 121 fullWhiteColor(result, stringBuffer, y, x); 122 } 123 } 124 125 /** 126 * Full black color, and you can change the alphabet if you need. 127 * @param result 128 * @param stringBuffer 129 * @param y 130 * @param x 131 */ 132 private static void fullBlackColor(double[][] result, StringBuffer stringBuffer, int y, int x) { 133 if (result[y][x] > 0.0 && result[y][x] < 25.0) { 134 stringBuffer.append(Common.R); 135 } else if (result[y][x] >= 25.0 && result[y][x] < 50.0) { 136 stringBuffer.append(Common.R); 137 } else if (result[y][x] >= 50.0 && result[y][x] < 75.0) { 138 stringBuffer.append(Common.A); 139 } else if (result[y][x] >= 75.0 && result[y][x] < 100.0) { 140 stringBuffer.append(Common.X); 141 } else if (result[y][x] >= 100.0 && result[y][x] < 125.0) { 142 stringBuffer.append(Common.R); 143 } else if (result[y][x] >= 125.0 && result[y][x] < 150.0) { 144 stringBuffer.append(Common.A); 145 } else if (result[y][x] >= 150.0 && result[y][x] < 154.0) { 146 stringBuffer.append(Common.X); 147 } else if (result[y][x] >= 154.0 && result[y][x] < 158.0) { 148 stringBuffer.append(Common.M); 149 } else if (result[y][x] >= 158.0 && result[y][x] < 162.0) { 150 stringBuffer.append(Common.W); 151 } else if (result[y][x] >= 162.0 && result[y][x] < 168.0) { 152 stringBuffer.append(Common.M); 153 } 154 } 155 156 /** 157 * Full grey color 158 * @param result 159 * @param stringBuffer 160 * @param y 161 * @param x 162 */ 163 private static void fullGreyColor(double[][] result, StringBuffer stringBuffer, int y, int x) { 164 if (result[y][x] >= 168.0 && result[y][x] < 172.0) { 165 stringBuffer.append(Common.H); 166 } else if (result[y][x] >= 172.0 && result[y][x] < 176.0) { 167 stringBuffer.append(Common.E); 168 } else if (result[y][x] >= 176.0 && result[y][x] < 180.0) { 169 stringBuffer.append(Common.H); 170 } else if (result[y][x] >= 180.0 && result[y][x] < 184.0) { 171 stringBuffer.append(Common.H); 172 } else if (result[y][x] >= 184.0 && result[y][x] < 188.0) { 173 stringBuffer.append(Common.J); 174 } else if (result[y][x] >= 188.0 && result[y][x] < 192.0) { 175 stringBuffer.append(Common.L); 176 } else if (result[y][x] >= 192.0 && result[y][x] < 196.0) { 177 stringBuffer.append(Common.C); 178 } else if (result[y][x] >= 196.0 && result[y][x] < 200.0) { 179 stringBuffer.append(Common.V); 180 } else if (result[y][x] >= 200.0 && result[y][x] < 204.0) { 181 stringBuffer.append(Common.Z); 182 } else if (result[y][x] >= 204.0 && result[y][x] < 208.0) { 183 stringBuffer.append(Common.Q); 184 } else if (result[y][x] >= 208.0 && result[y][x] < 212.0) { 185 stringBuffer.append(Common.T); 186 } 187 } 188 189 /** 190 * Full white color 191 * @param result 192 * @param stringBuffer 193 * @param y 194 * @param x 195 */ 196 private static void fullWhiteColor(double[][] result, StringBuffer stringBuffer, int y, int x) { 197 if (result[y][x] >= 212.0 && result[y][x] < 216.0) { 198 stringBuffer.append(Common.r); 199 } else if (result[y][x] >= 216.0 && result[y][x] < 220.0) { 200 stringBuffer.append(Common.s); 201 } else if (result[y][x] >= 220.0 && result[y][x] < 224.0) { 202 stringBuffer.append(Common.w); 203 } else if (result[y][x] >= 224.0 && result[y][x] < 228.0) { 204 stringBuffer.append(Common.u); 205 } else if (result[y][x] >= 228.0 && result[y][x] < 232.0) { 206 stringBuffer.append(Common.l); 207 } else if (result[y][x] >= 232.0 && result[y][x] < 236.0) { 208 stringBuffer.append(Common.i); 209 } else if (result[y][x] >= 236.0 && result[y][x] < 240.0) { 210 stringBuffer.append(Common.e); 211 } else if (result[y][x] >= 240.0 && result[y][x] < 244.0) { 212 stringBuffer.append(Common.COMMA); 213 } else if (result[y][x] >= 244.0 && result[y][x] < 248.0) { 214 stringBuffer.append(Common.m); 215 } else if (result[y][x] >= 248.0 && result[y][x] < 252.0) { 216 stringBuffer.append(Common.a); 217 } else if (result[y][x] >= 252.0 && result[y][x] < 257.0) { 218 stringBuffer.append(Common.BLANK); 219 } 220 } 221 }
/imageHandler/src/com/b510/image/util/TextUtil.java
1 /** 2 * 3 */ 4 package com.b510.image.util; 5 6 import java.io.BufferedWriter; 7 import java.io.File; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 11 import com.b510.image.common.Common; 12 13 /** 14 * @author Hongten 15 * @created Jul 23, 2014 16 */ 17 /** 18 * @author Hongten 19 * @created 2014-7-26 20 */ 21 public class TextUtil { 22 23 /** 24 * Write the string to file. 25 * @param stringBuffer 26 */ 27 public static void write2File(StringBuffer stringBuffer) { 28 File f = new File(Common.OUTPUT_TXT); 29 BufferedWriter output = null; 30 try { 31 output = new BufferedWriter(new FileWriter(f)); 32 output.write(stringBuffer.toString()); 33 output.close(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 }
源码下载:http://files.cnblogs.com/hongten/imageHandler.rar
========================================================
More reading,and english is important.
I‘m Hongten
========================================================
java画图程序_图片用字母画出来_源码发布,布布扣,bubuko.com
标签:style blog http java color os strong 文件
原文地址:http://www.cnblogs.com/hongten/p/hongten_java_draw_with_verbs_published.html