在上一个版本: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 hongtenzone@foxmail.com
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
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten