一个用java实现的将图片转成字符画的程序要怎么实现

zhou1988217 2010-09-17 03:37:13
RT:
[url=http://v.youku.com/v_show/id_XMTM5ODM2NTky]

上面是一个人写的一个字符画播放器,很牛逼的。
我估计就是把一断视频转成字符画的。
要怎么实现把一张图片作为输入,输出一个txt文档对应这张图片的字符画。
这个算法要怎么实现,具体思想是什么,请大伙详细请点。
这里是百度百科上的一个字符画的说明。可以看一看。[/url]
...全文
861 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
output1314 2013-04-07
  • 打赏
  • 举报
回复
public static String toHTML(String s) {
  s = s.replaceAll("&", "&");
  s = s.replaceAll(" ", " ");
  s = s.replaceAll(">", ">");
  s = s.replaceAll("<", "<");
  s = s.replaceAll("\"", """);
  s = s.replaceAll("\\\r\\\n", "<br/>");
  s = s.replaceAll("\\\r", "<br/>");
  s = s.replaceAll("\\\n", "<br/>");
  return s;
 }
中 s = s.replaceAll(" ", " ");应该改成 s = s.replaceAll(" ", " ");
qingralf 2010-09-24
  • 打赏
  • 举报
回复
还没结果?我网上搜到了...

#首先在D盘写一个文件"temp.html",如下内容

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>图片转文本</title>
<meta http-equiv="content-type" content="text/html; charset=gbk">
<style type="text/css">
body {
font-family: 宋体; line-height: 0.8em; letter-spacing: 0px; font-size: 8px;
}
</style>
</head>

<body>
${content}
</body>
</html>

#在D盘放一个图片(放小一点的)"a.jpg"

#运行如下JAVA代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;

public class Test {

/** 此处设置灰度字符,此处只用十个字符,可以设置更多 */
private static char[] cs = new char[] { '.', ',', '*', '+', '=', '&', '$', '@', '#', ' ' };

public static void main(String[] args) throws IOException {

// 读取图片
BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));

// 图片转字符串后的数组
char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()];

for (int x = 0; x < bfedimage.getWidth(); x++) {
for (int y = 0; y < bfedimage.getHeight(); y++) {
int rgb = bfedimage.getRGB(x, y);
Color c = new Color(rgb);
// 得到灰度值
int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
css[x][y] = cs[(int) ((cc * 10 - 1) / 255)];
}
}

// 取得模板HTML
String temp = readFile(new File("D:\\temp.html"),"gbk");
StringBuffer sb = new StringBuffer();

// 开始拼接内容
for (int y = 0; y < css[0].length; y++) {
for (int x = 0; x < css.length; x++) {
sb.append(css[x][y]);
}
sb.append("\r\n");
}

System.out.println(sb.toString());
// 生成文件
String content = toHTML(sb.toString());
String filecontent = replaceStrAllNotBack(temp, "${content}", content);
writeFile(new File("D:\\content.html"), filecontent, "gbk");
}

public static String toHTML(String s) {
s = s.replaceAll("&", "&");
s = s.replaceAll(" ", " ");
s = s.replaceAll(">", ">");
s = s.replaceAll("<", "<");
s = s.replaceAll("\"", """);
s = s.replaceAll("\\\r\\\n", "<br/>");
s = s.replaceAll("\\\r", "<br/>");
s = s.replaceAll("\\\n", "<br/>");
return s;
}

public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {
StringBuffer sb = new StringBuffer(str);
int index = 0;
while ((index = sb.indexOf(strSrc, index)) != -1) {
sb.replace(index, index + strSrc.length(), strDes);
index += strDes.length();
}
return sb.toString();
}

/**
* 读文件(使用默认编码)
*
* @param file
* @return 文件内容
* @throws IOException
*/
public static String readFile(File file, String charset) throws IOException {
InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset);
StringBuffer sb = new StringBuffer();
char[] bs = new char[1024];
int i = 0;
while ((i = fr.read(bs)) != -1) {
sb.append(bs, 0, i);
}
fr.close();
return sb.toString();
}

/**
* 写文件
*
* @param file
* @param string
* 字符串
* @param encoding
* 编码
* @return 文件大小
* @throws IOException
*/
public static int writeFile(File file, String string, String encoding) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
byte[] bs = string.getBytes(encoding);
fos.write(bs);
return bs.length;
} finally {
fos.close();
}
}
}



转自:http://zhidao.baidu.com/question/184541144.html
Gogo520520 2010-09-17
  • 打赏
  • 举报
回复
我也不懂 走人
qingralf 2010-09-17
  • 打赏
  • 举报
回复
mark一个.有时间了看看。以前也想做个这样的东西来着。
qingtianliuyun 2010-09-17
  • 打赏
  • 举报
回复
不懂啊,难度很大,希望楼主学习后指导一下
quhe0517 2010-09-17
  • 打赏
  • 举报
回复
顶贴 ,学习
好吃的松子 2010-09-17
  • 打赏
  • 举报
回复
根据颜色深浅逐行转换,深的用@,浅的用.或空格
凉岑玉 2010-09-17
  • 打赏
  • 举报
回复
接分··走人!
dingshikai123 2010-09-17
  • 打赏
  • 举报
回复
不懂 帮顶
zhou1988217 2010-09-17
  • 打赏
  • 举报
回复
可以看一看这个网站photo2text
就是要实现这个网站上的这个效果。怎么用java实现,思想,算法。请详细指点,最好附上源代码。
zhou1988217 2010-09-17
  • 打赏
  • 举报
回复
zhou1988217 2010-09-17
  • 打赏
  • 举报
回复
http://v.youku.com/v_show/id_XMTM5ODM2NTky
这里是一个人写的一个很牛逼的字符画播放器。可以看看,(上面链接出了点错。)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧