import java.awt.Color;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AsciiPic
{
/**
* @param path
* 图片路径
*/
public static void createAsciiPic(final String path) {
final String base = "@#&$%*o!;.";// 字符串由复杂到简单
try {
final BufferedImage image = ImageIO.read(new File(path));
for (int y = 0; y < image.getHeight(); y += 2) {
for (int x = 0; x < image.getWidth(); x++) {
final int pixel = image.getRGB(x, y);
final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
final float gray = 0.299f * r + 0.578f * g + 0.114f * b;
final int index = Math.round(gray * (base.length() + 1) / 255);
System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index)));
}
System.out.println();
}
} catch (final IOException e) {
e.printStackTrace();
}
}

public static String _BitmapConvert(String imgpath) {
StringBuffer _sb = new StringBuffer();
File imgfile = new File(imgpath);
char[] charset = { 'L', '5', '2', '0', 'q', ':', '*', '|', '.', ' ' };// 字符串由复杂到简单
try {
BufferedImage buff = ImageIO.read(imgfile);

int bitmapH = buff.getHeight();
int bitmapW = buff.getWidth();

for (int y = 0; y < bitmapH; y++) {
for (int x = 0; x < bitmapW; x++) {
int rgb = buff.getRGB(x, y);
Color c = new Color(rgb);
int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
_sb.append(charset[(int) ((cc * 10 - 1) / 255)]);
}
_sb.append("\r\n");
}
} catch (Exception e) {
}
return _sb.toString();
}

/**
* test
*
* @param args
*/
public static void main(final String[] args) {
String src = "C:xxx.jpg";
AsciiPic.createAsciiPic(src);
//System.out.println(_BitmapConvert(src));
}

}

原图与效果图

最新文章

  1. Eclipse调试Android App若选择&ldquo;Use same device for future launches&rdquo;就再也无法选择其他设备的问题
  2. linux C++ 获取文件绝对路径
  3. JSTL 的 if else : 有 c:if 没有 else 的处理
  4. 分享一下怎么开发一款图片视频类App,秒拍和prisma
  5. sequential minimal optimization,SMO for SVM, (MATLAB code)
  6. HADOOP :: java.lang.ClassNotFoundException: WordCount
  7. json数据处理技巧(字段带空格、增加字段)
  8. mvc razor页面的邮箱校验
  9. C++ hello world
  10. [Android]mac下开发环境搭建
  11. 如何获取DOM中当前获取焦点的元素
  12. C++—动态内存管理之深入探究new和delete
  13. react 体验 react与vue的比较
  14. CentOS自带定时任务crontab
  15. Cassandra的数据模型的理解
  16. QAbstractItemView区分单双击
  17. SQL注入之Sqli-labs系列第十四关(基于双引号POST报错注入)
  18. Jmeter实现MySQL的增删改查操作
  19. 去掉C#中Guid.NewGuid().ToString()自动生成的短横线
  20. vee-validate

热门文章

  1. 魔力Python--斐波那契数列(全)
  2. &#39;Settings&#39; object has no attribute &#39;FYFQ_URL_test&#39;
  3. DEDECMS 多站用一个站图片
  4. 微信小程序自定义组件实现
  5. Abaqus用Dload子程序实现移动载荷
  6. js:上传图片并预览(https://blog.csdn.net/weixin_38023551/article/details/78318532)
  7. Oracle :多表更新多个字段
  8. Ubuntu 16.04下配置intel opencl环境
  9. python数据类型及其详解
  10. 通过代码启动appium服务