Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Desktop;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- import javax.swing.text.DefaultStyledDocument;
- import javax.swing.text.SimpleAttributeSet;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyledDocument;
- public class Textificator {
- BufferedImage mainimage;
- String[][] maintext;
- public void Texttifizer(BufferedImage img) throws IOException
- {
- PrintWriter out = null;
- img = resize(img, img.getWidth()/2, img.getHeight()/2);
- try {
- out = new PrintWriter("Out.html");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n<title>Результат</title>\n <link rel=\"stylesheet\" href=\"Out.css\" type=\"text/css\">\n<body style=\"background-color: black;\"><pre style=\"font: 10px/6px monospace; text-align: center;\">");
- maintext = new String[img.getHeight()][img.getWidth()];
- for(int i=0;i<img.getHeight();i=i+3)
- {
- out.write("<br>");
- System.out.println(i);
- for(int j=0;j<img.getWidth();j=j+3)
- {
- if(getColor(j, i, img)!=null)
- {
- Random r = new Random();
- maintext[i][j] = "<font color="+getColor(j, i, img)+">$</font>";
- out.write(maintext[i][j]);
- }
- else
- {
- maintext[i][j] = " ";
- out.write(maintext[i][j]);}
- }
- out.write("</body>\n</html>");
- }
- String htmlFilePath = "Out.html"; // path to your new file
- File htmlFile = new File(htmlFilePath);
- }
- public String getColor(int x, int y, BufferedImage img)
- {
- int rgb = img.getRGB(x, y);
- int r = (rgb >> 16) & 0xFF;
- int g = (rgb >> 8) & 0xFF;
- int b = (rgb & 0xFF);
- int alpha = (rgb>>24) & 0xff;
- if(alpha<100)
- return null;
- System.out.println("R="+r+" G="+g+" B="+b+" Alpha="+alpha);
- Color color = new Color(r, g, b);
- String hex = String.format("#%02x%02x%02x", r, g, b);
- return hex;
- }
- public static BufferedImage resize(BufferedImage img, int newW, int newH) {
- int w = img.getWidth();
- int h = img.getHeight();
- BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
- Graphics2D g = dimg.createGraphics();
- g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
- g.dispose();
- return dimg;
- }
- public void Test()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement