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
/**
*
*/
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.ScaledImageUtil;
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 String SCALED_IMAGE = Common.SCALED + Common.FULL_STOP + ScaledImageUtil.getPostfix(Common.ORIGINAL); public static void main(String[] args) {
//colorImage2BWPic();
painting(Common.ORIGINAL, SCALED_IMAGE, 1);
// remoteImageHandler("E:/images/ipad/photo"); //NOTE: Provider your folder path, which includ some pictures
} /**
* Continuously review images
* @param remotePath Other folder path(Including pictures)
*/
private static void remoteImageHandler(String remotePath) {
File file = new File(remotePath);
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if(fileList[i].isFile()){
String originalImagePath = fileList[i].getAbsolutePath();
System.out.println("Processing ... " + originalImagePath);
SCALED_IMAGE = Common.SCALED + Common.FULL_STOP + ScaledImageUtil.getPostfix(originalImagePath);
painting(originalImagePath, SCALED_IMAGE, 1);
}
try {
Thread.sleep(4000); // every four seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} private static void painting(String originalImagePath, String scalImagepath, int scaled) {
ScaledImageUtil.scaledImage(originalImagePath, scaled, scalImagepath);
double[][] result = ImageUtil.getImageGRB(scalImagepath);
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 = PATH + "original.jpg";
public static final String SCALED = PATH + "scaled"; 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 FULL_STOP = ".";
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/ScaledImageUtil.java
/**
*
*/
package com.b510.image.util; import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import com.b510.image.common.Common; /**
* @author Hongten
* @created 2014-7-26
*/
public class ScaledImageUtil { public static int snapHeightMax = 196;
public static int snapWidthMax = 200; public static void scaledImage(String originalImagePath, int scaled, String scaledImagePath) {
try {
ImageIcon icon = getImage(ImageIO.read(new File(originalImagePath)), 5);
BufferedImage savedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
savedImage.createGraphics().drawImage(icon.getImage(), 0, 0, null);
ImageIO.write(savedImage, getPostfix(originalImagePath), new File(scaledImagePath));
System.out.println(Common.PROCESSED_SUCCESS);
} catch (IOException e) {
e.printStackTrace();
}
} public static String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(Common.FULL_STOP) + 1);
} public static ImageIcon getImage(Image img, int scaled) {
ImageIcon icon = new ImageIcon(getImages(img, scaled));
return icon;
} public static Image getImages(Image img, int scaled) {
int heigth = 0;
int width = 0; if (scaled < 25) {
scaled = 25;
} String sScaled = String.valueOf(Math.ceil((double) scaled / 25));
int indexX = sScaled.indexOf(".");
scaled = Integer.parseInt(sScaled.substring(0, indexX)); double scaleds = getScaling(img.getWidth(null), img.getHeight(null), scaled); try {
heigth = (int) (img.getHeight(null) * scaleds);
width = (int) (img.getWidth(null) * scaleds);
} catch (Exception e) {
e.printStackTrace();
} return getScaledImage(img, width, heigth);
} public static Image getScaledImage(Image srcImg, int width, int heigth) {
BufferedImage resizedImg = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, width, heigth, null);
g2.dispose(); Image image = srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_DEFAULT); return resizedImg;
} /**
* Get the image zoom ratio
* @param sourceWidth
* @param sourceHeight
* @return scaled
*/
public static double getScaling(int sourceWidth, int sourceHeight, int scaled) {
double widthScaling = ((double) snapWidthMax * (double) scaled) / (double) sourceWidth;
double heightScaling = ((double) snapHeightMax * (double) scaled) / (double) sourceHeight; double scaling = (widthScaling < heightScaling) ? widthScaling : heightScaling; return scaling;
}
}
/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_v1.2.rar
========================================================
More reading,and english is important.
I'm Hongten
大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================
java画图程序_图片用字母画出来_源码发布_版本二的更多相关文章
- java画图程序_图片用字母画出来_源码发布
在之前写了一篇blog:java画图程序_图片用字母画出来 主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试). 就把源码发布出来. 项目结构 ...
- java画图程序_图片用字母画出来
最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongt ...
- Android 图片加载框架Glide4.0源码完全解析(二)
写在之前 上一篇博文写的是Android 图片加载框架Glide4.0源码完全解析(一),主要分析了Glide4.0源码中的with方法和load方法,原本打算是一起发布的,但是由于into方法复杂性 ...
- Java界面程序实现图片的放大缩小
Java界面程序实现图片的放大缩小.这个程序简单地实现了图片的打开.保存.放大一倍.缩小一倍和固定缩放尺寸,但是并没有过多的涵盖对图片的细节处理,只是简单地实现了图片大小的放缩. 思维导图如下: 效果 ...
- Java学习-025-类名或方法名应用之一 -- 调试源码
上文讲述了如何获取类名和方法名,敬请参阅: Java学习-024-获取当前类名或方法名二三文 . 通常在应用开发中,调试或查看是哪个文件中的方法调用了当前文件的此方法,因而在实际的应用中需要获取相应的 ...
- Java开源生鲜电商平台-商品表的设计(源码可下载)
Java开源生鲜电商平台-商品表的设计(源码可下载) 任何一个电商,无论是B2C还是B2B的电商,商品表的设计关系到整个系统架构的核心. 1. 商品基本信息表:用单词:goods做为商品表 2. 商品 ...
- Java开源生鲜电商平台-订单表的设计(源码可下载)
Java开源生鲜电商平台-订单表的设计(源码可下载) 场景分析说明: 买家(餐馆)用户,通过APP进行选菜,放入购物车,然后下单,最终支付的流程,我们称为下单过程. 买家可以在张三家买茄子,李四家买萝 ...
- 『TensorFlow』SSD源码学习_其一:论文及开源项目文档介绍
一.论文介绍 读论文系列:Object Detection ECCV2016 SSD 一句话概括:SSD就是关于类别的多尺度RPN网络 基本思路: 基础网络后接多层feature map 多层feat ...
- Java开源生鲜电商平台-盈利模式详解(源码可下载)
Java开源生鲜电商平台-盈利模式详解(源码可下载) 该平台提供一个联合买家与卖家的一个平台.(类似淘宝购物,这里指的是食材的购买.) 平台有以下的盈利模式:(类似的平台有美菜网,食材网等) 1. 订 ...
随机推荐
- Asyncio中的Task管理
#!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from random ...
- MYSQL建表语法(主键,外键,联合主键)
在看<Learning SQL>第二版, 慢慢打实SQL的基础. 建表一: ), lname ), gender ENUM(), city ), state ), country ), p ...
- OCJP(1Z0-851) 模拟题分析(七)-->214
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- C编译: 动态连接库 (.so文件)(转摘)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在“纸上谈兵: 算法与数据结构”中,我在每一篇都会有一个C程序,用于实现算法和数据 ...
- PGA
Server Process PGA 1.PGA作用 2.PGA構成 1)private sql area 2)session memory 3)sql ...
- COALESCE NVL NVL2 DECODE
1 COALESCE 語法:COALESCE(expr1, expr2, ..., exprn) n>=2 作用:COALESCE returns the first non-null expr ...
- Unity3d 提示 "The scripts file name does not match the name of the class defined in the script!"的解决办法
有两个原因,一个是文件的名称和类名不一致 第二个原因是有命名空间, 排除应该是可以修复的
- ios编码转换 国标 UTF-8
我们知道,使用NSURLConnection的代理方法下载网页,存到一个NSData中, NSMutableData *pageData; [pageData appendData:data]; 如果 ...
- 2015腾讯web前端笔试题
1 请实现,鼠标点击页面中的任意标签,alert该标签的名称.(注意兼容性) 2 请指出一下代码的性能问题,并经行优化. var info="腾讯拍拍网(www.paipai.com)是 ...
- CSS3详解:background
CSS3对于background做了一些修改,最明显的一个就是采用设置多背景,不但添加了4个新属性,并且还对目前的属性进行了调整增强. 1.多个背景图片 在css3里面,你可以再一个标签元素里应用多个 ...