java画图程序_图片用字母画出来_源码发布_版本二

在上一个版本:java画图程序_图片用字母画出来_源码发布 基础上,增加了图片同比例缩放,使得大像素图片可以很好地显示画在Notepad++中。

项目结构:

运行效果1:

原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_result1.png

运行效果2:

原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_result2.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.ScaledImageUtil;
11 import com.b510.image.util.TextUtil;
12
13 /**
14  * This project is a tool for processing the image. <br>Including TWO functions :
15  * <li>Color image to converted black and white picture.
16  * <li>Imitating the original image to paint into the TXT file with alphabets.
17  * You can change the code according to your requirement.
18  *
19  * @author Hongten
20  * @mail [email protected]
21  * @created Jul 23, 2014
22  */
23 public class Client {
24
25     public static String SCALED_IMAGE = Common.SCALED + Common.FULL_STOP + ScaledImageUtil.getPostfix(Common.ORIGINAL);
26
27     public static void main(String[] args) {
28         //colorImage2BWPic();
29         painting(Common.ORIGINAL, SCALED_IMAGE, 1);
30 //        remoteImageHandler("E:/images/ipad/photo"); //NOTE: Provider your folder path, which includ some pictures
31     }
32
33     /**
34      * Continuously review images
35      * @param remotePath Other folder path(Including pictures)
36      */
37     private static void remoteImageHandler(String remotePath) {
38         File file = new File(remotePath);
39         File[] fileList = file.listFiles();
40         for (int i = 0; i < fileList.length; i++) {
41             if(fileList[i].isFile()){
42                 String originalImagePath = fileList[i].getAbsolutePath();
43                 System.out.println("Processing ... " + originalImagePath);
44                 SCALED_IMAGE =  Common.SCALED + Common.FULL_STOP + ScaledImageUtil.getPostfix(originalImagePath);
45                 painting(originalImagePath, SCALED_IMAGE, 1);
46             }
47             try {
48                 Thread.sleep(4000); // every four seconds
49             } catch (InterruptedException e) {
50                 e.printStackTrace();
51             }
52         }
53     }
54
55     private static void painting(String originalImagePath, String scalImagepath, int scaled) {
56         ScaledImageUtil.scaledImage(originalImagePath, scaled, scalImagepath);
57         double[][] result = ImageUtil.getImageGRB(scalImagepath);
58         StringBuffer stringBuffer = ImageUtil.getCanvas(result);
59         TextUtil.write2File(stringBuffer);
60     }
61
62     /**
63      * Color image to converted black and white picture.
64      */
65     private static void colorImage2BWPic() {
66         File input = new File(Common.ORIGINAL_IMAGE);
67         File out = new File(Common.PROCESSED_IMAGE);
68         ImageUtil.colorImage2BlackAndWhitePicture(input, out);
69     }
70 }

/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 = PATH + "original.jpg";
15     public static final String SCALED = PATH + "scaled";
16
17     public static final String ORIGINAL_IMAGE = PATH + "original_image.png";
18     public static final String PROCESSED_IMAGE =  PATH + "processed_image.png";
19
20     public static final String OUTPUT_TXT = PATH + "output.txt";
21
22     public static final String PROCESSED_SUCCESS = "Processed successfully...";
23     public static final String PROCESS_ERROR = "Processing encounters error!";
24
25     public static final String R = "R";
26     public static final String A = "A";
27     public static final String X = "X";
28     public static final String M = "M";
29     public static final String W = "W";
30     public static final String H = "H";
31     public static final String E = "E";
32     public static final String J = "J";
33     public static final String L = "L";
34     public static final String C = "C";
35     public static final String V = "V";
36     public static final String Z = "Z";
37     public static final String Q = "Q";
38     public static final String T = "T";
39     public static final String r = "r";
40     public static final String s = "s";
41     public static final String w = "w";
42     public static final String u = "u";
43     public static final String l = "l";
44     public static final String i = "i";
45     public static final String e = "e";
46     public static final String m = "m";
47     public static final String a = "a";
48     public static final String COMMA = ",";
49     public static final String FULL_STOP = ".";
50     public static final String BLANK = " ";
51
52     public static final String NEW_LINE = "\n";
53 }

/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/ScaledImageUtil.java

 1 /**
 2  *
 3  */
 4 package com.b510.image.util;
 5
 6 import java.awt.Graphics2D;
 7 import java.awt.Image;
 8 import java.awt.RenderingHints;
 9 import java.awt.image.BufferedImage;
10 import java.io.File;
11 import java.io.IOException;
12
13 import javax.imageio.ImageIO;
14 import javax.swing.ImageIcon;
15
16 import com.b510.image.common.Common;
17
18 /**
19  * @author Hongten
20  * @created 2014-7-26
21  */
22 public class ScaledImageUtil {
23
24     public static int snapHeightMax = 196;
25     public static int snapWidthMax = 200;
26
27     public static void scaledImage(String originalImagePath, int scaled, String scaledImagePath) {
28         try {
29             ImageIcon icon = getImage(ImageIO.read(new File(originalImagePath)), 5);
30             BufferedImage savedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
31             savedImage.createGraphics().drawImage(icon.getImage(), 0, 0, null);
32             ImageIO.write(savedImage, getPostfix(originalImagePath), new File(scaledImagePath));
33             System.out.println(Common.PROCESSED_SUCCESS);
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37     }
38
39     public static String getPostfix(String inputFilePath) {
40         return inputFilePath.substring(inputFilePath.lastIndexOf(Common.FULL_STOP) + 1);
41     }
42
43     public static ImageIcon getImage(Image img, int scaled) {
44         ImageIcon icon = new ImageIcon(getImages(img, scaled));
45         return icon;
46     }
47
48     public static Image getImages(Image img, int scaled) {
49         int heigth = 0;
50         int width = 0;
51
52         if (scaled < 25) {
53             scaled = 25;
54         }
55
56         String sScaled = String.valueOf(Math.ceil((double) scaled / 25));
57         int indexX = sScaled.indexOf(".");
58         scaled = Integer.parseInt(sScaled.substring(0, indexX));
59
60         double scaleds = getScaling(img.getWidth(null), img.getHeight(null), scaled);
61
62         try {
63             heigth = (int) (img.getHeight(null) * scaleds);
64             width = (int) (img.getWidth(null) * scaleds);
65         } catch (Exception e) {
66             e.printStackTrace();
67         }
68
69         return getScaledImage(img, width, heigth);
70     }
71
72     public static Image getScaledImage(Image srcImg, int width, int heigth) {
73         BufferedImage resizedImg = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
74         Graphics2D g2 = resizedImg.createGraphics();
75         g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
76         g2.drawImage(srcImg, 0, 0, width, heigth, null);
77         g2.dispose();
78
79         Image image = srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_DEFAULT);
80
81         return resizedImg;
82     }
83
84     /**
85      * Get the image zoom ratio
86      * @param sourceWidth
87      * @param sourceHeight
88      * @return scaled
89      */
90     public static double getScaling(int sourceWidth, int sourceHeight, int scaled) {
91         double widthScaling = ((double) snapWidthMax * (double) scaled) / (double) sourceWidth;
92         double heightScaling = ((double) snapHeightMax * (double) scaled) / (double) sourceHeight;
93
94         double scaling = (widthScaling < heightScaling) ? widthScaling : heightScaling;
95
96         return scaling;
97     }
98 }

/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_v1.2.rar

========================================================

More reading,and english is important.

I‘m Hongten

========================================================

时间: 2024-08-03 23:54:05

java画图程序_图片用字母画出来_源码发布_版本二的相关文章

java画图程序_图片用字母画出来_源码发布

在之前写了一篇blog:java画图程序_图片用字母画出来 主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试). 就把源码发布出来. 项目结构: 资源文件: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_imagehandler_resource.png 运行效果: 原图:http://images.cnblogs.com/cnblogs_com/hongten/356

java画图程序_图片用字母画出来

最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongten/gallery/image/143365.html 第二张:林允儿 原图:http://images.cnblogs.com/cnblogs_com/hongten/356471/o_star.png 第三张:郭静 原图:http://www.cnblogs.com/hongten/galle

Java界面程序实现图片的放大缩小

Java界面程序实现图片的放大缩小.这个程序简单地实现了图片的打开.保存.放大一倍.缩小一倍和固定缩放尺寸,但是并没有过多的涵盖对图片的细节处理,只是简单地实现了图片大小的放缩. 思维导图如下: 效果图如下: 代码如下: package picture; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.io.*; i

Java的深拷贝最新版欣欣十三水棋牌_房卡十三水全套棋牌源码下载和浅拷贝

关于最新版本的Java新版13水棋棋牌室13水全套棋牌源码下载超级MAN.,只是为了创建与已知对象相同的对象.在日常编码中,它可能用得不多,但这是面试官经常问的问题,理解深拷贝和浅拷贝的原理将导致对什么是更深入的理解调用Java中的值传递或引用传递.    回到顶峰    创建对象的1, 5种方法.        通过新关键词        这是通过用新的关键字调用类的参数化或非参数构造函数来创建对象的最常见的方法.例如,对象Obj=新对象():        (2)传递类类的NeWistSis

Java集合源码学习笔记(二)ArrayList分析

Java集合源码学习笔记(二)ArrayList分析 >>关于ArrayList ArrayList直接继承AbstractList,实现了List. RandomAccess.Cloneable.Serializable接口,为什么叫"ArrayList",因为ArrayList内部是用一个数组存储元素值,相当于一个可变大小的数组,也就是动态数组. (1)继承和实现继承了AbstractList,实现了List:ArrayList是一个数组队列,提供了相关的添加.删除.修

Java学习-025-类名或方法名应用之一 -- 调试源码

上文讲述了如何获取类名和方法名,敬请参阅: Java学习-024-获取当前类名或方法名二三文 . 通常在应用开发中,调试或查看是哪个文件中的方法调用了当前文件的此方法,因而在实际的应用中需要获取相应的包名.类名.方法名.行数,从而快速定位,及统计方法被调用的次数,生成类方法关系链. 相信爱钻研的小主们,通过上篇文章,已经懂得了,如何获取主调方法.从调方法.那我直接上码了,敬请各位小主参阅,若有不足之处,敬请各位大神指正,不胜感激! GetClassMethodName.java 源码内容如下所示

手机模板_苹果风格 iOS7 X3_X3.1版源码

[模板介绍:三十大功能与特性]:0.[具有蓝.橙.绿.宝石绿.青.酷黑.红.玫瑰红.粉红.紫.商务蓝.灰蓝12种配色],支持无限配色扩展! 支持iOS.Android系统,兼容多种移动终端!1.[支持图片墙瀑布][发帖.回帖等上传多张图片].2.[App化处理]让您的网站更像一个独立的App应用!3.[全局侧边栏滑出面板菜单]酷炫时尚,动感十足!4.支持全局页脚.头部固定导航:5.支持帖子列表样式和图文样式浏览:[注意:1.(iOS6版只具有质感蓝.酷炫黑两种配色):2.(iOS6版不支持图片墙

Linux内核编程:Linux2.6内核源码解析_进程遍历 &nbsp; &nbsp; &nbsp; &nbsp;

/*     *File    : test.c   *Author  : DavidLin        *Date    : 2014-12-07pm        *Email   : [email protected] or [email protected]        *world   : the city of SZ, in China        *Ver     : 000.000.001        *history :     editor      time    

【小白的java成长系列】——String类的深入分析(基于源码)

接着前面面向对象来说吧~今天来说说String类..其实String类也包含很多面向对象的知识的~ 首先来问一个问题:我们在开发过程中,如果要使用一个类的话,就要创建对象,这句话没什么问题吧~在实际开发的时候确实是这样的,只有创建了对象才能真正的去使用一个普通的类,我们一般创建对象,几乎所有的类创建对象都是要通过new关键字来创建的~ 问题就来了..为什么我们的String可以直接写成String str = "abc";这样子呢? 当然String类也可以通过new来创建对象的...