您的位置:首页 > 编程语言 > Java开发

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

2017-01-10 21:36 309 查看
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];
4000

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息