public void createAsciiPic(String path) {
final String base = “@#&$%*o!;.”;// 字符串由复杂到简单
try {
/* 写入Txt文件 */
File writename = new File(“D:\output.txt”); // 相对路径,如果没有则要建立一个新的output.txt文件
writename.createNewFile(); // 创建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(writename,true));
//String path=”D:\timg.jpg”;
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 >=
图片转ascii样式
最新推荐文章于 2022-09-05 13:56:01 发布