java 根据图片的像素RBG值转换成文字符号

本文介绍了一种使用Java将图片的每个像素RGB值映射为不同文字符号的方法,从而将图片转换为由字符组成的艺术形式。通过解析图片,获取每个像素的颜色信息,并依据颜色深浅选用不同的字符来表现,最终生成一个字符画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;

import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;

public class StringDraw
{
    public static void main(String[] args) throws IOException
    {
        String img_file = "d:/1.jpg";
        int w = 50;
        int h = 0;
        String out_file = "d:/a.txt";

        try
        {
            img_file = args[0];
            w = Integer.parseInt(args[1]);
            out_file = args[2];
        } catch (Exception e)
        {
        }
        // 读取图片
        BufferedImage binaryBufferedImage =
                grayPixel(ImageIO.read(new File(img_file)));
        h =
                (int) (((float) binaryBufferedImage.getWidth())
                        / ((float) binaryBufferedImage.getHeight()) * w);
        // 按比例压缩图片
        BufferedImage bfimage =
                new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);

        bfimage.getGraphics().drawImage(
                binaryBufferedImage,
                0,
                0,
                w,
                binaryBufferedImage.getWidth()
                        / binaryBufferedImage.getHeight() * w, null);

        BufferedWriter bw = new BufferedWriter(new FileWriter(out_file));

        for (int i = 0; i < bfimage.getHeight(); i += 1)
        {
            for (int j = 0; j < bfimage.getWidth(); j += 1)
            {
                int pixel = bfimage.getRGB(j, i);
                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = (pixel) & 0xff;

                if (255 > red && 255 > green && 255 > blue)
                {
                    bw.write(".");
                } else
                {
                    bw.write(" ");
                }

            }
            bw.newLine();
        }
        bw.close();
        System.out.println("yes" + bfimage.getRGB(0, 0));
    }

    public static BufferedImage grayPixel(BufferedImage bfimage)
    {
        int h = bfimage.getHeight();
        int w = bfimage.getWidth();

        // 灰度化
        int[][] gray = new int[w][h];
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                int argb = bfimage.getRGB(x, y);
                int r = (argb >> 16) & 0xFF;
                int g = (argb >> 8) & 0xFF;
                int b = (argb >> 0) & 0xFF;
                int grayPixel = (int) ((b * 29 + g * 150 + r * 77 + 128) >> 8);
                gray[x][y] = grayPixel;
            }
        }

        // 二值化
        int threshold = ostu(gray, w, h);
        BufferedImage binaryBufferedImage =
                new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                if (gray[x][y] > threshold)
                {
                    gray[x][y] |= 0x00FFFF;
                } else
                {
                    gray[x][y] &= 0xFF0000;
                }
                binaryBufferedImage.setRGB(x, y, gray[x][y]);
            }
        }
        return binaryBufferedImage;
    }

    public static int ostu(int[][] gray, int w, int h)
    {
        int[] histData = new int[w * h];

        // Calculate histogram
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                int red = 0xFF & gray[x][y];
                histData[red]++;
            }
        }

        // Total number of pixels
        int total = w * h;
        float sum = 0;
        for (int t = 0; t < 256; t++)
            sum += t * histData[t];
        float sumB = 0;
        int wB = 0;
        int wF = 0;
        float varMax = 0;
        int threshold = 0;

        for (int t = 0; t < 256; t++)
        {
            // Weight Background
            wB += histData[t];
            if (wB == 0)
                continue;

            // Weight Foreground
            wF = total - wB;
            if (wF == 0)
                break;

            sumB += (float) (t * histData[t]);

            // Mean Background
            float mB = sumB / wB;

            // Mean Foreground
            float mF = (sum - sumB) / wF;

            // Calculate Between Class Variance
            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);

            // Check if new maximum found
            if (varBetween > varMax)
            {
                varMax = varBetween;
                threshold = t;
            }
        }
        return threshold;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值