Created
September 27, 2014 15:45
-
-
Save vivisidea/be33a9a0223c69ee885d to your computer and use it in GitHub Desktop.
bilibili.tv using char to draw a jpeg image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bilibili.tv; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.imageio.ImageIO; | |
/** | |
* convert a jpeg file to character text file | |
* | |
* @author vivi | |
* | |
*/ | |
public class JPGToText {//@formatter:off | |
private final static char[] ASCII_CHARS = { '#', '&', '$', '*', '+', ';', '.', ' ' }; | |
private final static String HTML_TEMP ="<!DOCTYPE>"+ | |
"<html>"+ | |
" <head>"+ | |
" <meta charset=\"utf-8\">"+ | |
" <style type=\"text/css\">"+ | |
" body{"+ | |
" font-family: monospace; "+ | |
" font-size: %dpx; "+ | |
" margin: 0 auto;"+ | |
" }"+ | |
" </style>"+ | |
" </head>"+ | |
" <body>"+ | |
" <pre>%s</pre>"+ | |
" </body>"+ | |
"</html>";//@formatter:on | |
// private final static char[] ASCII_CHARS = { '0', '.', ';', '+', '*', '$', '&', '#' }; | |
/** | |
* | | |
* -+---------x-------------------> | |
* | | |
* | | |
* y | |
* | | |
* @param inputFile input filename | |
* @param charWidth will treat every charWidth*charHeight as a char | |
* @param charHeight will treat every charWidth*charHeight as a char | |
* @throws IOException | |
*/ | |
public static String jpgToTxt2(String inputFile, int charWidth, int charHeight) throws IOException { | |
File f = new File(inputFile); | |
BufferedImage image = ImageIO.read(f); | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
int rgbArray[] = new int[width * height]; // 保存所有的像素 | |
image.getRGB(0, 0, width, height, rgbArray, 0, width); | |
StringBuilder sb = new StringBuilder(); | |
for (int y = 0; y < height; y += charHeight) { | |
for (int x = 0; x < width; x += charWidth) { | |
int rgb = rgbArray[y * width + x]; | |
int r = (rgb >> 16) & 0x000000FF; // the format is 0xalpha,r,g,b | |
int g = (rgb >> 8) & 0x000000FF; | |
int b = rgb & 0x000000FF; | |
int gray = (r * 19595 + g * 38469 + b * 7472) >> 16; | |
int level = gray / (256 / ASCII_CHARS.length); | |
sb.append(ASCII_CHARS[level]); | |
// System.out.println(String.format("r%s,g%s,b%s,gray%s", r, g, b, gray)); | |
} | |
sb.append("\n"); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) throws IOException { | |
// String input = "target/input.txt"; | |
// String output = "target/output.jpg"; | |
// txtToJpg(input, output, 320, 240, 4); | |
// jpgToTxt("target/163693930.jpg", 1); | |
// System.out.println("-=-------------------"); | |
String output = jpgToTxt2("target/163693930.jpg", 2, 4); | |
// String output = jpgToTxt2("target/nerv.jpg", 1, 2); | |
// String output = jpgToTxt2("target/1pixel.jpg", 1, 2); | |
// jpgToTxt2("target/1pixel.jpg", 4, 6); | |
String html = String.format(HTML_TEMP, 9, output); | |
PrintWriter printWriter = new PrintWriter(new File("target/charpic.html")); | |
printWriter.write(html); | |
printWriter.close(); | |
BufferedImage image = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB); | |
Font f = new Font(Font.MONOSPACED, Font.PLAIN, 24); | |
Graphics g = image.getGraphics(); | |
g.setColor(Color.WHITE); | |
g.fillRect(0, 0, 320, 240); | |
g.setColor(Color.BLACK); | |
g.setFont(f); | |
g.drawString("wq.Yang", 100, 100); | |
g.dispose(); | |
// image.getGraphics().drawChars("abc".toCharArray(), 0, 0, 100, 100); | |
// int width = image.getWidth(); | |
// int height = image.getHeight(); | |
// for (int y = 0; y < height; y++) { | |
// for (int x = 0; x < width; x++) { | |
// image.setRGB(x, y, 0x000000FF); | |
// int rgb = image.getRGB(x, y); | |
//// System.out.println(rgb); | |
// } | |
// } | |
ImageIO.write(image, "jpg", new File("target/1pixel.jpg")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment