java画图程序_图片用字母画出来_源码发布
2014-07-26 12:24
399 查看
在之前写了一篇blog:java画图程序_图片用字母画出来
主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试)。
就把源码发布出来。
项目结构:
资源文件:
原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_resource.png
运行效果:
原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_cat.png
===================================================
源码部分:
===================================================
/imageHandler/src/com/b510/image/client/Client.java
/imageHandler/src/com/b510/image/common/Common.java
/imageHandler/src/com/b510/image/util/ImageUtil.java
/imageHandler/src/com/b510/image/util/TextUtil.java
源码下载:http://files.cnblogs.com/hongten/imageHandler.rar
========================================================
More reading,and english is important.
I'm Hongten
========================================================
主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试)。
就把源码发布出来。
项目结构:
资源文件:
原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_resource.png
运行效果:
原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_cat.png
===================================================
源码部分:
===================================================
/imageHandler/src/com/b510/image/client/Client.java
/** * */ package com.b510.image.client; import java.io.File; import com.b510.image.common.Common; import com.b510.image.util.ImageUtil; import com.b510.image.util.TextUtil; /** * This project is a tool for processing the image. <br>Including TWO functions : * <li>Color image to converted black and white picture. * <li>Imitating the original image to paint into the TXT file with alphabets. * You can change the code according to your requirement. * * @author Hongten * @mail hongtenzone@foxmail.com * @created Jul 23, 2014 */ public class Client { public static void main(String[] args) { colorImage2BWPic(); painting(); } private static void painting() { double[][] result = ImageUtil.getImageGRB(Common.ORIGINAL_IMAGE); StringBuffer stringBuffer = ImageUtil.getCanvas(result); TextUtil.write2File(stringBuffer); } /** * Color image to converted black and white picture. */ private static void colorImage2BWPic() { File input = new File(Common.ORIGINAL_IMAGE); File out = new File(Common.PROCESSED_IMAGE); ImageUtil.colorImage2BlackAndWhitePicture(input, out); } }
/imageHandler/src/com/b510/image/common/Common.java
/** * */ package com.b510.image.common; /** * @author Hongten * @created Jul 23, 2014 */ public class Common { public static final String PATH = "src/com/b510/image/resources/"; public static final String ORIGINAL_IMAGE = PATH + "original_image.png"; public static final String PROCESSED_IMAGE = PATH + "processed_image.png"; public static final String OUTPUT_TXT = PATH + "output.txt"; public static final String PROCESSED_SUCCESS = "Processed successfully..."; public static final String PROCESS_ERROR = "Processing encounters error!"; public static final String R = "R"; public static final String A = "A"; public static final String X = "X"; public static final String M = "M"; public static final String W = "W"; public static final String H = "H"; public static final String E = "E"; public static final String J = "J"; public static final String L = "L"; public static final String C = "C"; public static final String V = "V"; public static final String Z = "Z"; public static final String Q = "Q"; public static final String T = "T"; public static final String r = "r"; public static final String s = "s"; public static final String w = "w"; public static final String u = "u"; public static final String l = "l"; public static final String i = "i"; public static final String e = "e"; public static final String m = "m"; public static final String a = "a"; public static final String COMMA = ","; public static final String BLANK = " "; public static final String NEW_LINE = "\n"; }
/imageHandler/src/com/b510/image/util/ImageUtil.java
/** * */ package com.b510.image.util; import java.awt.Image; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.awt.image.ColorConvertOp; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.b510.image.common.Common; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * @author Hongten * @created Jul 23, 2014 */ public class ImageUtil { private static int height = 0; private static int width = 0; /** * Color image is converted to black and white picture. * @param input * @param out */ public static void colorImage2BlackAndWhitePicture(File input, File out) { try { Image image = ImageIO.read(input); int srcH = image.getHeight(null); int srcW = image.getWidth(null); BufferedImage bufferedImage = new BufferedImage(srcW, srcH, BufferedImage.TYPE_3BYTE_BGR); bufferedImage.getGraphics().drawImage(image, 0, 0, srcW, srcH, null); bufferedImage = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bufferedImage, null); FileOutputStream fos = new FileOutputStream(out); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); encoder.encode(bufferedImage); fos.close(); System.out.println(Common.PROCESSED_SUCCESS); } catch (Exception e) { throw new IllegalStateException(Common.PROCESS_ERROR, e); } } /** * Get the image(*.jpg, *.png.etc) GRB value * @param filePath the original image path * @return */ public static double[][] getImageGRB(String filePath) { File file = new File(filePath); double[][] result = null; if (!file.exists()) { return result; } try { BufferedImage bufImg = readImage(file); result = new double[height][width]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double temp = Double.valueOf(bufImg.getRGB(x, y) & 0xFFFFFF); result[y][x] = Math.floor(Math.cbrt(temp)); } } } catch (IOException e) { e.printStackTrace(); } return result; } /** * Read the original image to convert BufferedImage * @param file Original image path * @return * @see BufferedImage * @throws IOException */ private static BufferedImage readImage(File file) throws IOException { BufferedImage bufImg = ImageIO.read(file); height = bufImg.getHeight(); width = bufImg.getWidth(); return bufImg; } /** * Get the canvas, which is made up of alphabets. * @param result * @return */ public static StringBuffer getCanvas(double[][] result) { StringBuffer stringBuffer = new StringBuffer(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { fullBlank(result, stringBuffer, y, x); } stringBuffer.append(Common.NEW_LINE); } return new StringBuffer(stringBuffer.substring(0, stringBuffer.length() - 1)); } /** * Full blank * @param result * @param stringBuffer * @param y * @param x */ private static void fullBlank(double[][] result, StringBuffer stringBuffer, int y, int x) { if (result[y][x] > 0.0 && result[y][x] < 168.0) { fullBlackColor(result, stringBuffer, y, x); } else if (result[y][x] >= 168.0 && result[y][x] < 212.0) { fullGreyColor(result, stringBuffer, y, x); } else { fullWhiteColor(result, stringBuffer, y, x); } } /** * Full black color, and you can change the alphabet if you need. * @param result * @param stringBuffer * @param y * @param x */ private static void fullBlackColor(double[][] result, StringBuffer stringBuffer, int y, int x) { if (result[y][x] > 0.0 && result[y][x] < 25.0) { stringBuffer.append(Common.R); } else if (result[y][x] >= 25.0 && result[y][x] < 50.0) { stringBuffer.append(Common.R); } else if (result[y][x] >= 50.0 && result[y][x] < 75.0) { stringBuffer.append(Common.A); } else if (result[y][x] >= 75.0 && result[y][x] < 100.0) { stringBuffer.append(Common.X); } else if (result[y][x] >= 100.0 && result[y][x] < 125.0) { stringBuffer.append(Common.R); } else if (result[y][x] >= 125.0 && result[y][x] < 150.0) { stringBuffer.append(Common.A); } else if (result[y][x] >= 150.0 && result[y][x] < 154.0) { stringBuffer.append(Common.X); } else if (result[y][x] >= 154.0 && result[y][x] < 158.0) { stringBuffer.append(Common.M); } else if (result[y][x] >= 158.0 && result[y][x] < 162.0) { stringBuffer.append(Common.W); } else if (result[y][x] >= 162.0 && result[y][x] < 168.0) { stringBuffer.append(Common.M); } } /** * Full grey color * @param result * @param stringBuffer * @param y * @param x */ private static void fullGreyColor(double[][] result, StringBuffer stringBuffer, int y, int x) { if (result[y][x] >= 168.0 && result[y][x] < 172.0) { stringBuffer.append(Common.H); } else if (result[y][x] >= 172.0 && result[y][x] < 176.0) { stringBuffer.append(Common.E); } else if (result[y][x] >= 176.0 && result[y][x] < 180.0) { stringBuffer.append(Common.H); } else if (result[y][x] >= 180.0 && result[y][x] < 184.0) { stringBuffer.append(Common.H); } else if (result[y][x] >= 184.0 && result[y][x] < 188.0) { stringBuffer.append(Common.J); } else if (result[y][x] >= 188.0 && result[y][x] < 192.0) { stringBuffer.append(Common.L); } else if (result[y][x] >= 192.0 && result[y][x] < 196.0) { stringBuffer.append(Common.C); } else if (result[y][x] >= 196.0 && result[y][x] < 200.0) { stringBuffer.append(Common.V); } else if (result[y][x] >= 200.0 && result[y][x] < 204.0) { stringBuffer.append(Common.Z); } else if (result[y][x] >= 204.0 && result[y][x] < 208.0) { stringBuffer.append(Common.Q); } else if (result[y][x] >= 208.0 && result[y][x] < 212.0) { stringBuffer.append(Common.T); } } /** * Full white color * @param result * @param stringBuffer * @param y * @param x */ private static void fullWhiteColor(double[][] result, StringBuffer stringBuffer, int y, int x) { if (result[y][x] >= 212.0 && result[y][x] < 216.0) { stringBuffer.append(Common.r); } else if (result[y][x] >= 216.0 && result[y][x] < 220.0) { stringBuffer.append(Common.s); } else if (result[y][x] >= 220.0 && result[y][x] < 224.0) { stringBuffer.append(Common.w); } else if (result[y][x] >= 224.0 && result[y][x] < 228.0) { stringBuffer.append(Common.u); } else if (result[y][x] >= 228.0 && result[y][x] < 232.0) { stringBuffer.append(Common.l); } else if (result[y][x] >= 232.0 && result[y][x] < 236.0) { stringBuffer.append(Common.i); } else if (result[y][x] >= 236.0 && result[y][x] < 240.0) { stringBuffer.append(Common.e); } else if (result[y][x] >= 240.0 && result[y][x] < 244.0) { stringBuffer.append(Common.COMMA); } else if (result[y][x] >= 244.0 && result[y][x] < 248.0) { stringBuffer.append(Common.m); } else if (result[y][x] >= 248.0 && result[y][x] < 252.0) { stringBuffer.append(Common.a); } else if (result[y][x] >= 252.0 && result[y][x] < 257.0) { stringBuffer.append(Common.BLANK); } } }
/imageHandler/src/com/b510/image/util/TextUtil.java
/** * */ package com.b510.image.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.b510.image.common.Common; /** * @author Hongten * @created Jul 23, 2014 */ /** * @author Hongten * @created 2014-7-26 */ public class TextUtil { /** * Write the string to file. * @param stringBuffer */ public static void write2File(StringBuffer stringBuffer) { File f = new File(Common.OUTPUT_TXT); BufferedWriter output = null; try { output = new BufferedWriter(new FileWriter(f)); output.write(stringBuffer.toString()); output.close(); } catch (IOException e) { e.printStackTrace(); } } }
源码下载:http://files.cnblogs.com/hongten/imageHandler.rar
========================================================
More reading,and english is important.
I'm Hongten
========================================================
相关文章推荐
- java画图程序_图片用字母画出来_源码发布_版本二
- java画图程序_图片用字母画出来
- 发布一个很COOL的图片验证码程序[含源码]
- VS2015+QT5.8 程序打包发布详解(包含图片打包,附工具和源码)
- 发布一个很COOL的图片验证码程序[含源码]
- LocalOS 0.1.1 版发布(内附本程序源码及Java版网游找CALL示例源码)
- 发布一个很COOL的图片验证码程序[含源码]
- LocalOS 0.1.1 版发布(内附本程序源码及Java版网游找CALL示例源码)
- 发布一个很COOL的图片验证码程序[含源码](转)
- asp.net发布一个很COOL的图片验证码程序[含源码]
- 发布一个很COOL的图片验证码程序[含源码]
- LocalOS 0.1.1 版发布(内附本程序源码及Java版网游找CALL示例源码)
- 发布一个很COOL的图片验证码程序[含源码]
- LocalOS 0.1.1 版发布(内附本程序源码及Java版网游找CALL示例源码)
- 今天发现一个以前写的管理代码片段的程序,把源码发出来!
- 如何有效防止Java程序源码被人偷窥?
- 发布一个图片库轮显WebPart (源码)
- Java发布程序的运行
- java Applet 程序老是运行不出来 xiexie da jia
- 一个很COOL的图片验证码程序[含源码]