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; } }
相关文章推荐
- java实现缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
- Java根据像素比较两张图片的相似度
- java根据图片和文字生成自定义图片
- ImagesUtil java图片操作 缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
- ImagesUtil java图片操作 缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
- java根据图片和文字生成自定义图片
- java文字转换成图片输出
- 重写java中JButton,实现图片下显示文字(根据图片宽度文字自动换行)
- java实现给图片添加水印和文字
- 基于java的图片文件格式转换和线性缩放
- java实现给图片添加水印和文字
- 基于java的图片文件格式转换和线性缩放
- 基于java的图片文件格式转换和线性缩放
- Oracle中根据Date型转换成Java对应的long型毫秒数
- java添加透明的图片和文字水印
- 在java的GUI上放置图片,并在图片上输出半透明文字
- java 将bmp图片转换为png图片
- 基于java的图片文件格式转换和线性缩放
- 一个生成文字图片的JAVA函数
- asp.net 2.0 将文字转换成图片